Пример #1
0
 public void WriteEncoding(BitOutputStream output, BitArrayWrapper encoding)
 {
     foreach (bool bit in encoding)
     {
         output.WriteBit(bit ? 1 : 0);
     }
 }
Пример #2
0
        public int Encode2ndOrderCodeWord(BitOutputStream output, int n)
        {
            BitArrayWrapper encoding = new BitArrayWrapper();
            int             temp     = n;
            int             i        = Fibonacci.FibonacciSize - 1;

            while (i > -1)
            {
                if (_fibonacciSequenceOrder2[i] < temp)
                {
                    encoding.Set(i);
                    temp = temp - _fibonacciSequenceOrder2[i];
                }
                else if (_fibonacciSequenceOrder2[i] == temp)
                {
                    encoding.Set(i);
                    i = 0;
                }
                i--;
            }

            encoding.Set(encoding.Length());

            if (output != null)
            {
                WriteEncoding(output, encoding);
            }

            return(encoding.Length());
        }
Пример #3
0
        public void PairEncoder(BitOutputStream output, int length, int position)
        {
            if (length > _maxRepeat)
            {
                _maxRepeat = length;
            }

            if (length > 0)
            {
                long codelength = _encoder.Encode2ndOrderCodeWord(null, length) +
                                  _encoder.Encode3rdOrderCodeWord(null, position);
                long code2 = 0;
                long i;
                for (i = position; i < position + length; i++)
                {
                    if (_buffer[i - 1] < 0)
                    {
                        code2 = code2 + _letterLengths[_buffer[i - 1] + 256];
                    }
                    else
                    {
                        code2 = code2 + _letterLengths[_buffer[i - 1]];
                    }
                }

                if (codelength < code2)
                {
                    _encoder.Encode2ndOrderCodeWord(output, length);
                    _encoder.Encode3rdOrderCodeWord(output, position);

                    String s = "(" + length + ")(" + position + ")";
                    Console.WriteLine(s);
                }
                else
                {
                    for (i = position; i < position + length; i++)
                    {
                        _encoder.Encode2ndOrderCodeWord(output, 0);
                        if (_buffer[i - 1] >= 0)
                        {
                            _encoder.Encode3rdOrderCodeWord(output, _buffer[i - 1]);
                        }
                        else
                        {
                            _encoder.Encode3rdOrderCodeWord(output, _buffer[i - 1] + 256);
                        }
                    }
                }
            }
            else
            {
                _encoder.Encode2ndOrderCodeWord(output, length);
                _encoder.Encode3rdOrderCodeWord(output, position);
                String s = "(" + length + ")(" + position + ")";
                Console.WriteLine(s);
            }
        }
Пример #4
0
        public void Compress(Stream input, Stream output, int windowSize)
        {
            Oracle oracle;
            int    size;
            int    bsize        = (int)input.Length;
            int    fileLeft     = 0;
            int    a            = -1;
            int    lastPosition = 0;
            char   s;

            BitArrayWrapper Bits = new BitArrayWrapper();

            float meter    = 0;
            float oldm     = 0;
            long  bcounter = 0;

            using (BitOutputStream bitOutput = new BitOutputStream(output))
            {
                //First output the original file size, and the buffer size.
                _encoder.Encode3rdOrderCodeWord(bitOutput, bsize);
                _encoder.Encode3rdOrderCodeWord(bitOutput, windowSize);
                oracle = new Oracle();
                while (input.Position < input.Length)
                {
                    oracle.AddState(0);
                    size = (int)input.Length - (int)input.Position;

                    if (size <= windowSize)
                    {
                        _Sp     = new int[size + 2];
                        _lrs    = new int[size + 2];
                        _buffer = new byte[size + 1];
                        input.Read(_buffer, 0, (int)size);
                        fileLeft = size;
                    }
                    else
                    {
                        _Sp      = new int[windowSize + 2];
                        _lrs     = new int[windowSize + 2];
                        fileLeft = windowSize;
                        _buffer  = new byte[windowSize + 1];
                        input.Read(_buffer, 0, (int)windowSize);
                    }

                    _Sp[0] = -1;
                    int k = 0;
                    int j;
                    int s1;
                    int pi1;
                    int endto;
                    lastPosition = 0;

                    if (fileLeft == size)
                    {
                        endto = fileLeft + 1;
                    }
                    else
                    {
                        endto = fileLeft;
                    }

                    for (int i = 1; i <= endto; i++)
                    {
                        bcounter++;

                        oracle.AddState(i);

                        s = (char)_buffer[i - 1];

                        Console.WriteLine(_buffer[i - 1]);

                        j   = _Sp[i - 1];
                        pi1 = i - 1;
                        while ((j > -1) & (oracle.FindTransition(oracle.GetState(j), _buffer, s) == -2))
                        {
                            //Just keep track of the external transitions
                            oracle.AddTransition(j, i);
                            pi1 = j;
                            j   = _Sp[j];
                        }
                        if (j == -1)
                        {
                            s1 = 0;
                        }
                        else
                        {
                            s1 = oracle.FindTransition(oracle.GetState(j), _buffer, s);
                        }
                        _Sp[i]  = s1;
                        _lrs[i] = LengthRepeatedSuffix(pi1, _Sp[i]);

                        if ((i == fileLeft) & (lastPosition <= i - 1))
                        {
                            long z = 0;
                            for (z = (lastPosition); z < fileLeft; z++)
                            {
                                if (_buffer[z] >= 0)
                                {
                                    PairEncoder(bitOutput, 0, _buffer[z]);
                                }
                                else
                                {
                                    PairEncoder(bitOutput, 0, _buffer[z] + 256);
                                }
                            }
                            lastPosition = i - 1;
                        }
                        else if ((_lrs[i] < (i - lastPosition)))
                        {
                            if (_lrs[lastPosition + 1] == 0)
                            {
                                if (_buffer[lastPosition] >= 0)
                                {
                                    PairEncoder(bitOutput, 0, _buffer[lastPosition]);
                                }
                                else
                                {
                                    PairEncoder(bitOutput, 0, _buffer[lastPosition] + 256);
                                }

                                lastPosition = lastPosition + 1;
                            }
                            else
                            {
                                if (_lrs[i - 1] > (i - 1) - lastPosition)
                                {
                                    k = GetBetterPosition(i - 1, lastPosition);
                                    PairEncoder(bitOutput, (i - 1) - lastPosition, _Sp[k] - (i - 1) + lastPosition + 1);
                                    lastPosition = i - 1;
                                }
                                else
                                {
                                    PairEncoder(bitOutput, (i - 1) - lastPosition, _Sp[i - 1] - (i - 1) + lastPosition + 1);
                                    lastPosition = i - 1;
                                }
                            }
                        }
                    }
                }
            }
        }