//Decompress BitStream into integer array public static int[] Decompress(ref BitStreamFIFO source) { //Read necessary info for decompression int compressionType = source.ReadInt(2); //The compression technique used int lengthSaveLength = source.ReadInt(6) * 8; //The amount of bits needed to save the total length in bits of the compressed data int arrayDataLength = source.ReadInt(lengthSaveLength); //The total length in bits of the compressed data //Create a new BitStream of correct length from the incoming BitStream BitStreamFIFO arrayData = new BitStreamFIFO(source.ReadBoolArray(arrayDataLength)); //Decompress using the correct compression type int[] intArray = new int[0]; switch (compressionType) { //Huffmann case 0: intArray = HuffmanIntArrayCompressor.DeCompress(arrayData); break; //Variable int length case 1: intArray = VaryingIntLengthIntArrayCompressor.Decompress(arrayData); break; //To add a compression technique, add a new case like the existing ones and increase the length of new byte[??][] } //Return the decompressed array of integers return(intArray); }
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()); }
//Decompress byte array into aBitmap with help of width, length and bitdepth public static AccessibleBitmapBytewise Decompress(byte[] inBytes, AccessibleBitmapBytewise inBitmap, out byte[] restBytes, int byteLayer) { //Create a AccessibleBitmapbitwise class from the incoming AccessibleBitmapbytewise class, for better access to individual bits AccessibleBitmapBitwise outputBitmap = new AccessibleBitmapBitwise(inBitmap); //Create a BitStreamFIFO class from the incoming bytes, to feed into the decompression algorithms BitStreamFIFO bitStream = new BitStreamFIFO(inBytes); //Loop trough all bit layers of current byte layer for (int i = byteLayer * 8; i < byteLayer * 8 + 8; i++) { //Read compression type as a 3-bit integer int compressionType = bitStream.ReadInt(3); //Decompress using the correct compression type switch (compressionType) { //Uncompressed case 0: outputBitmap = UncompressedBitmapCompressorBitwise.Decompress(bitStream, outputBitmap, out bitStream, i); break; //Bit channel compressed as 8-bit integers case 1: outputBitmap = ByteArrayCompressorBitwise.Decompress(bitStream, outputBitmap, out bitStream, i); break; //Run length encoding case 2: outputBitmap = RunLengthEncodingCompressorBitwise.Decompress(bitStream, outputBitmap, out bitStream, i); break; //Run length encoding vertical case 3: outputBitmap = RunLengthEncodingCompressorVerticalBitwise.Decompress(bitStream, outputBitmap, out bitStream, i); break; //To add a decompression type add a new case like the existing ones //Unknown compression type: error default: throw new Exception("Unexisting compression type"); } } //Remove the bytes used for this channel from the incoming byte array and pass the rest of them to the next channel restBytes = new byte[bitStream.Length / 8]; Array.Copy(inBytes, inBytes.Length - (bitStream.Length / 8), restBytes, 0, restBytes.Length); //Return the modified bitmap as AccessibleBitmapbytewise so the rest of the channels can be added to complete it return(new AccessibleBitmapBytewise(outputBitmap.GetAccessibleBitmap())); }
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; }