public void WriteEncoding(BitOutputStream output, BitArrayWrapper encoding) { foreach (bool bit in encoding) { output.WriteBit(bit ? 1 : 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()); }
public BitArrayWrapper Read2ndOrderCodeWord(BitInputStream input) { bool done = false; int prevbit = 0; int curbit = 0; int bcounter = -1; BitArrayWrapper tempSet = new BitArrayWrapper(); while (done == false) { curbit = input.ReadBit(); bcounter++; if ((curbit == 1) & (prevbit == 1)) { done = true; } else if ((curbit == 1) & (prevbit == 0)) { tempSet.Set(bcounter); prevbit = curbit; } else { prevbit = curbit; } } return(tempSet); }
public int Decode3rdOrderCodeWord(BitArrayWrapper bits) { if (bits.Length() == 0) { return(-1); } int value = 0; int rem = 0; if (bits.Get(bits.Length() - 5)) { rem = 1; } for (int i = 0; i < bits.Length(); i++) { if (bits.Get(i)) { value += _fibonacciSequenceOrder3[i]; } } value = (value * 2) + rem; return(value); }
public int Decode2ndOrderCodeWord(BitArrayWrapper bits) { if (bits.Length() == 0) { return(-1); } int value = 0; for (int i = 0; i < bits.Length(); i++) { if (bits.Get(i)) { value += _fibonacciSequenceOrder2[i]; } } return(value); }
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; } } } } } } }