public static int[] DeCompress(BitStreamFIFO source) { //dictionary decompile //1: add '0' to end //2: read byte; attach current code to byte; remove al '1' from end; replace last '0' with '1' int minBits = source.ReadByte(); int dictionaryLength = source.ReadInt(minBits) + 1; //Console.WriteLine(minBits + " " + dictionaryLength); SortedDictionary <string, int> dictionary = new SortedDictionary <string, int>(); List <int> dicNumbers = new List <int>(); List <bool[]> dicCodes = new List <bool[]>(); string tmpCode = ""; while (dictionary.Count < dictionaryLength) { if (source.ReadBool()) { tmpCode += "1"; } else { dictionary.Add(tmpCode, source.ReadInt(minBits)); if (tmpCode.Contains('1')) { while (tmpCode.Last() == '0') { tmpCode = tmpCode.Remove(tmpCode.Length - 1, 1); } tmpCode = tmpCode.Remove(tmpCode.Length - 1, 1); tmpCode += '0'; } } } /*for(int i = 0; i < dicNumbers.Count; i++) * { * Console.WriteLine(dicNumbers[i] + " " + BoolArrString(dicCodes[i])); * } * Console.WriteLine();*/ List <int> outputList = new List <int>(); string tmpRead = ""; while (source.Length > 0) { tmpRead += source.ReadBool()?'1':'0'; int foundVal = 0; if (dictionary.TryGetValue(tmpRead, out foundVal)) { outputList.Add(foundVal); tmpRead = ""; } } return(outputList.ToArray()); }
//Decompress BitStream into integer array public static int[] Decompress(BitStreamFIFO source) { //Read necessary info from BitStream int bitDepth = source.ReadByte(); //The default bits to read values int[] specialValues = new int[source.ReadByte()]; //The amount of values that trigger an increase of bits to read //Read all values that trigger an increase of bits to read for (int i = 0; i < specialValues.Length; i++) { specialValues[i] = source.ReadInt(bitDepth); } //Create a list of ints as output List <int> outputList = new List <int>(); //Read data while the input stream contains data while (source.Length >= bitDepth) { //Read an integer using the default amount of bits int tmpLengthTmp = source.ReadInt(bitDepth); //If the current value is a value that triggers an increase of bits to read if (specialValues.Contains(tmpLengthTmp)) { //Read a new value with the correct amount of bits int extraLength = Array.IndexOf(specialValues, tmpLengthTmp) + 1; //Replace the current value with the new value tmpLengthTmp = source.ReadInt(bitDepth + extraLength); } //Add the current value to the output outputList.Add(tmpLengthTmp); } //Return output as an array of integers return(outputList.ToArray()); }
public static AccessibleBitmap Decompress(byte[] source, int width, int height, int pixelBytes) { BitStreamFIFO bs = new BitStreamFIFO(source); // Convert the image into a bitstream bool verticallyCompressed = bs.ReadBool(); // Store if image was vertically compressed or not int maxBitCount = bs.ReadByte(); // Get the highest bitcount value AccessibleBitmap bmp = new AccessibleBitmap(width, height, pixelBytes); // Create new bitmap to write all pixels to // Ints to keep track of coords int x = 0; int y = 0; // Loop while there are still bits to read while (bs.Length > maxBitCount) { int counterValue = bs.ReadInt(maxBitCount); // Get the counter value of the next pixel value byte[] pixel = bs.ReadByteArray(pixelBytes); // Get the pixel value for (int i = 0; i < counterValue; i++) { bmp.SetPixel(x, y, pixel); if (verticallyCompressed) { y++; if (y >= height) { x++; y = 0; } }else { x++; if (x >= width) { y++; x = 0; } } } } // Return the bitmap return bmp; }