예제 #1
0
        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
                }
            }
        }
예제 #2
0
파일: Paq1.cs 프로젝트: daywee/PAQ1
        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);
            }
        }
예제 #3
0
파일: Paq1.cs 프로젝트: daywee/PAQ1
        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);
            }
        }