//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 BitStreamFIFO class from the incoming bytes, becouse the decompressor for integer arrays uses it BitStreamFIFO layerBits = new BitStreamFIFO(inBytes); //Decompress the incoming data to an array of integers int[] layerByteArray = VaryingIntArrayCompressor.Decompress(ref layerBits); //Add the data from the array of integers to the incoming bitmap //Loop trough all lines of pixels for (int y = 0; y < inBitmap.height; y++) { //Loop trough all pixels in this line for (int x = 0; x < inBitmap.width; x++) { //Write the integer of this channel from the correct position in the input array to the current pixel inBitmap.SetPixelByte(x, y, byteLayer, (byte)layerByteArray[y * inBitmap.width + x]); } } //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[layerBits.Length / 8]; Array.Copy(inBytes, inBytes.Length - restBytes.Length, restBytes, 0, restBytes.Length); //Return the modified bitmap so the rest of the channels can be added to complete it return(inBitmap); }
//Decompress byte array into aBitmap with help of width, length and bitdepth public static AccessibleBitmapBitwise Decompress(BitStreamFIFO inBits, AccessibleBitmapBitwise inBitmap, out BitStreamFIFO restBits, int bitLayer) { //Read necessary info from BitStream bool currentVal = inBits.ReadBool(); Queue <int> runs = new Queue <int>(VaryingIntArrayCompressor.Decompress(ref inBits)); int pixelsToGo = runs.Dequeue() + 1; //Iterate trough all pixels for (int y = 0; y < inBitmap.height; y++) { for (int x = 0; x < inBitmap.width; x++) { //Set the bit of the current pixel to the value of the current run inBitmap.SetPixelBit(x, y, bitLayer, currentVal); //Decrease the length of the current run & check if the end has bin reached pixelsToGo--; if (pixelsToGo == 0 && (x * y != (inBitmap.height - 1) * (inBitmap.width - 1))) { //Read the new run length from the BitStream & reverse the run bit pixelsToGo = runs.Dequeue() + 1; //Toggle bit value currentVal = !currentVal; } } } //Return rest of bits & return bitmap restBits = inBits; return(inBitmap); }
//Decompress byte array into aBitmap with help of width, length and bitdepth public static AccessibleBitmap Decompress(byte[] source, int width, int height, int pixelBytes) { //Create a BitStream from the input bytes becouse the decompress functions need this as input BitStreamFIFO bitStream = new BitStreamFIFO(source); //Create an empty bitmap with the correct dimensions, where the decompressed pixels will be written to AccessibleBitmap aBitmap = new AccessibleBitmap(width, height, pixelBytes); //Decompress the BitStream to a queue of integers for the lengths Queue <int> pixels = new Queue <int>(VaryingIntArrayCompressor.Decompress(ref bitStream)); //Decompress the BitStream to a queue of integers for the pixel values Queue <int> runs = new Queue <int>(VaryingIntArrayCompressor.Decompress(ref bitStream)); //Initialize int pixelsToGo = runs.Dequeue() + 1; //The amount of pixels that should be written before the next run starts byte[] currentPixel = new byte[pixelBytes]; //The pixel value of the current run: initialize with the correct amount of channels //Loop trough all channels in the pixel of the current length for (int i = 0; i < pixelBytes; i++) { //Take a byte from the queue of pixel values to put in the current channel currentPixel[i] = (byte)pixels.Dequeue(); } //Loop trough all lines of pixels for (int y = 0; y < height; y++) { //Loop trough all pixels in this line for (int x = 0; x < width; x++) { //Set the bit of the current pixel to the value of the current run aBitmap.SetPixel(x, y, currentPixel); //Decrease the length of the current run pixelsToGo--; //If the end of the run has been reached if (pixelsToGo == 0 && (x * y != (height - 1) * (width - 1))) { //Read the new run length from the BitStream pixelsToGo = runs.Dequeue() + 1; //Loop trough all channels in the pixel of the current length for (int i = 0; i < pixelBytes; i++) { //Take a byte from the queue of pixel values to put in the current channel currentPixel[i] = (byte)pixels.Dequeue(); } } } } //Return the image as aBitmap return(aBitmap); }
//Decompress byte array into aBitmap with help of width, length and bitdepth public static AccessibleBitmap Decompress(byte[] source, int width, int height, int pixelBytes) { BitStreamFIFO layerBits = new BitStreamFIFO(source); int[] byteArray = VaryingIntArrayCompressor.Decompress(ref layerBits); //Create aBitmap from pixel data AccessibleBitmap aBitmap = new AccessibleBitmap(width, height, pixelBytes); aBitmap.SetRawPixelBytes(Array.ConvertAll(byteArray, Convert.ToByte)); return(aBitmap); }
//Decompress byte array into aBitmap with help of width, length and bitdepth public static AccessibleBitmapBitwise Decompress(BitStreamFIFO inBits, AccessibleBitmapBitwise inBitmap, out BitStreamFIFO restBits, int bitLayer) { //Decompress the incoming data to an array of integers int[] ints = VaryingIntArrayCompressor.Decompress(ref inBits); //Initialize int intIndex = 0; //The index in the array of intergers, from where the next integer should be taken bool[] tmpBools = new bool[8]; //The array of bits from which bits will be read to be applied to pixels int boolIndex = 8; //The index in the array of bits, from where the next bit should be taken //Add the data from the array of integers to the incoming bitmap //Loop trough all lines of pixels for (int y = 0; y < inBitmap.height; y++) { //Loop trough all pixels in this line for (int x = 0; x < inBitmap.width; x++) { //If index is 8, all bits are used, so tmpBools should be renewed by using the next integer in the array of integers if (boolIndex == 8) { //Reset the index to 0, so the next bit will be read from the start boolIndex = 0; //Convert the next integer in the array to a bool array, and write it to tmpBools tmpBools = new BitStreamFIFO().IntToBoolArray(ints[intIndex], 8); //Increase the index of the integer array, so the next integer will be read correctly intIndex++; } //Write the bit of this channel from the correct position in the array of bits to the current pixel inBitmap.SetPixelBit(x, y, bitLayer, tmpBools[boolIndex]); //Increase index for next bit boolIndex++; } } //Set the output BitStream to the remaining bits of the input BitStream restBits = inBits; //Return the modified AccessibleBitmapBitwise so the rest of the channels can be added to complete it return(inBitmap); }
//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 BitStream from the input bytes becouse the decompress functions need this as input BitStreamFIFO bitStream = new BitStreamFIFO(inBytes); //Decompress the BitStream to a queue of integers for the lengths Queue <int> pixels = new Queue <int>(VaryingIntArrayCompressor.Decompress(ref bitStream)); //Decompress the BitStream to a queue of integers for the pixel values Queue <int> runs = new Queue <int>(VaryingIntArrayCompressor.Decompress(ref bitStream)); //Initialize int pixelsToGo = runs.Dequeue() + 1; //The amount of pixels that should be written before the next run starts byte currentPixel = (byte)pixels.Dequeue(); //The pixel value of the current run: initialize with the first pixel value //Loop trough all lines of pixels for (int y = 0; y < inBitmap.height; y++) { //Loop trough all pixels in this line for (int x = 0; x < inBitmap.width; x++) { //Set the bit of the current pixel to the value of the current run inBitmap.SetPixelByte(x, y, byteLayer, currentPixel); //Decrease the length of the current run pixelsToGo--; //If the end of the run has been reached if (pixelsToGo == 0 && (x * y != (inBitmap.height - 1) * (inBitmap.width - 1))) { //Read the new run length from the BitStream pixelsToGo = runs.Dequeue() + 1; //Take a byte from the queue of pixel values to put in the current channel currentPixel = (byte)pixels.Dequeue(); } } } //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 - restBytes.Length, restBytes, 0, restBytes.Length); //Return the image as aBitmap return(inBitmap); }
//Decompress byte array into aBitmap with help of width, length and bitdepth public static AccessibleBitmapBitwise Decompress(BitStreamFIFO inBits, AccessibleBitmapBitwise inBitmap, out BitStreamFIFO restBits, int bitLayer) { //Read necessary info from BitStream bool currentVal = inBits.ReadBool(); //The bit value of the first run //Decompress the BitStream to a queue of integers Queue <int> runs = new Queue <int>(VaryingIntArrayCompressor.Decompress(ref inBits)); //Initialize int pixelsToGo = runs.Dequeue() + 1; //The amount of pixels that should be written before the next run starts //Loop trough all lines of pixels for (int y = 0; y < inBitmap.height; y++) { //Loop trough all pixels in this line for (int x = 0; x < inBitmap.width; x++) { //Set the bit of the current pixel to the value of the current run inBitmap.SetPixelBit(x, y, bitLayer, currentVal); //Decrease the length of the current run pixelsToGo--; //If the end of the run has been reached if (pixelsToGo == 0 && (x * y != (inBitmap.height - 1) * (inBitmap.width - 1))) { //Read the new run length from the BitStream pixelsToGo = runs.Dequeue() + 1; //Toggle bit value, because a bit can just have 2 values, and this run cannot have the same value as the previous run currentVal = !currentVal; } } } //Set the output BitStream to the remaining bits of the input BitStream restBits = inBits; //Return the modified AccessibleBitmapBitwise so the rest of the channels can be added to complete it return(inBitmap); }