//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);
        }
示例#2
0
        //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()));
        }
示例#3
0
        //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);
        }
        //Compress aBitmap into byte array
        public static BitStreamFIFO Compress(AccessibleBitmapBitwise source, int bitLayer)
        {
            //Initialize
            List <int> distances    = new List <int>();                   //A list containing all the lenghts of same bits
            int        tempDistance = -1;                                 //The length of one run of bits with the same value, while it is not saved yet: -1 becouse it will be increased before the first check
            bool       lastVal      = source.GetPixelBit(0, 0, bitLayer); //The bit value of the last checked pixel, to compare with the current pixel: set value to the value of the first pixel so the first check will succeed

            //Loop trough all lines of pixels
            for (int y = 0; y < source.height; y++)
            {
                //Loop trough all pixels in this line
                for (int x = 0; x < source.width; x++)
                {
                    //Take value of the current pixel
                    bool currentBool = source.GetPixelBit(x, y, bitLayer);

                    //If the value of the bit of this pixel matches the value of the bit of the previous pixel
                    if (currentBool == lastVal)
                    {
                        //Values are the same, so increase current run
                        tempDistance++;
                    }
                    else
                    {
                        //Values are not the same, so save the run
                        distances.Add(tempDistance);

                        //Set the bit value for the next comparison to the bit value of this pixel
                        lastVal = currentBool;

                        //Reset the run length for the new run
                        tempDistance = 0;
                    }
                }
            }
            //Save the last run becouse this never happens in the loop
            distances.Add(tempDistance);

            //Save the bit value of the first pixel, because the decompressor needs to know this
            bool initialVal = source.GetPixelBit(0, 0, bitLayer);

            //Compress the array of run lengths using different techniques
            BitStreamFIFO bitStream = VaryingIntArrayCompressor.Compress(distances.ToArray());

            //Combine the inititial bit value with the compressed data of the runs, then return the BitStream
            return(BitStreamFIFO.Merge(new BitStreamFIFO(new bool[] { initialVal }), bitStream));
        }
示例#5
0
        //Compress aBitmap into byte array
        public static BitStreamFIFO Compress(AccessibleBitmapBitwise source, int bitLayer)
        {
            //Initialize
            List <int> ints = new List <int>(); //List of all integers generated from the bits of this channel

            bool[] tmpBools = new bool[8];      //The collection of bits that will be converted to an interger
            int    index    = 0;                //The index in tmpBools where a new bit should be inserted

            //Loop trough all lines of pixels
            for (int y = 0; y < source.height; y++)
            {
                //Loop trough all pixels in this line
                for (int x = 0; x < source.width; x++)
                {
                    //Write bit of current pixel to the correct position in tmpBools
                    tmpBools[index] = source.GetPixelBit(x, y, bitLayer);

                    //Increase index for next bit
                    index++;

                    //If index is 8, tmpBools is full
                    if (index == 8)
                    {
                        //Set index to 0, so the next bit will be written to the start
                        index = 0;

                        //Convert tmpBools to an integer & add the result to the list of integers
                        ints.Add(new BitStreamFIFO().BoolArrayToInt(tmpBools));
                    }
                }
            }

            //If index is not 0, it has not been reset at last, so tmpBools should be saved for the last bits it contains
            if (index > 0)
            {
                //Convert tmpBools to an integer & add the result to the list of integers
                ints.Add(new BitStreamFIFO().BoolArrayToInt(tmpBools));
            }

            //Compress the obtained array of integers using different techniques
            BitStreamFIFO bitStream = VaryingIntArrayCompressor.Compress(ints.ToArray());

            //Return the output array
            return(bitStream);
        }
示例#6
0
        //Compress aBitmap into byte array
        public static BitStreamFIFO Compress(AccessibleBitmapBitwise source, int bitLayer)
        {
            //Creates a new BitStreamFIFO object where all bits will be written to
            BitStreamFIFO bitStream = new BitStreamFIFO();

            //Loop trough all lines of pixels
            for (int y = 0; y < source.height; y++)
            {
                //Loop trough all pixels in this line
                for (int x = 0; x < source.width; x++)
                {
                    //Write the bit of this channel from the current pixel to the output BitStream
                    bitStream.Write(source.GetPixelBit(x, y, bitLayer));
                }
            }

            //Return the BitStream
            return(bitStream);
        }
        //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);
        }
        //Compress aBitmap into byte array
        public static BitStreamFIFO Compress(AccessibleBitmapBitwise source, int bitLayer)
        {
            //Initialize vars
            BitStreamFIFO bitStream    = new BitStreamFIFO();
            List <int>    distances    = new List <int>();
            int           tempDistance = -1;
            bool          lastVal      = source.GetPixelBit(0, 0, bitLayer);

            //Iterate trough pixels
            for (int y = 0; y < source.height; y++)
            {
                for (int x = 0; x < source.width; x++)
                {
                    //Take value of pixel & compare with previous value
                    bool currentBool = source.GetPixelBit(x, y, bitLayer);
                    if (currentBool == lastVal)
                    {
                        //Values are the same, so increase current run
                        tempDistance++;
                    }
                    else
                    {
                        //Values are not the same, so save the run and create a new one
                        distances.Add(tempDistance);
                        lastVal      = currentBool;
                        tempDistance = 0;
                    }
                }
            }
            //Save the last run becouse this never happens in the loop
            distances.Add(tempDistance);


            bool initialVal = source.GetPixelBit(0, 0, bitLayer);

            bitStream.Write(initialVal);

            BitStreamFIFO output = BitStreamFIFO.Merge(bitStream, VaryingIntArrayCompressor.Compress(distances.ToArray()));

            return(output);
        }
示例#9
0
        //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)
        {
            //Add the data from this stream 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 bit of this channel from the input BitStream to the current pixel
                    inBitmap.SetPixelBit(x, y, bitLayer, inBits.ReadBool());
                }
            }

            //Set the output BitStream to the remainder of the input BitStream
            restBits = inBits;

            //Return the modified bitmap so the rest of the channels can be added to complete it
            return(inBitmap);
        }
示例#10
0
        //Compress aBitmap into byte array
        public static byte[] Compress(AccessibleBitmapBytewise source, int byteLayer)
        {
            //Loop trough all layers of bits, where possible at the same time
            AccessibleBitmapBitwise aBitmap = new AccessibleBitmapBitwise(source);

            BitStreamFIFO[] byteLayers = new BitStreamFIFO[8];
            Parallel.For(byteLayer * 8, byteLayer * 8 + 8, (z, state) => //for(int z = byteLayer * 8; z < byteLayer * 8 + 8; z++)
            {
                //Compress image using all different compression techniques, where possible at the same time
                BitStreamFIFO[] compressionTechniques = new BitStreamFIFO[4];
                Parallel.For(0, compressionTechniques.Length, (i, state2) =>
                {
                    switch (i)
                    {
                    //Uncompressed (only used if no compression technique is smaller)
                    case 0:
                        compressionTechniques[i] = UncompressedBitmapCompressorBitwise.Compress(aBitmap, z);
                        break;

                    //Compress bit channel as an integer array using several techniques, using 8-bit integers
                    case 1:
                        compressionTechniques[i] = ByteArrayCompressorBitwise.Compress(aBitmap, z);
                        break;

                    //Run length compression: save the length of a sequence of bit values instead of saving them seperately
                    case 2:
                        compressionTechniques[i] = RunLengthEncodingCompressorBitwise.Compress(aBitmap, z);
                        break;

                    //Run length compression vertical: run length compression, but scan the pixels horizontally, becouse with some images this yields better results
                    case 3:
                        compressionTechniques[i] = RunLengthEncodingCompressorVerticalBitwise.Compress(aBitmap, z);
                        break;

                        //To add a compression technique, add a new case like the existing ones and increase the length of new byte[??][]
                    }
                });


                //Choose the smallest compression type

                //Initialize
                int smallestID   = 0;               //The ID of the smallest compression type
                int smallestSize = int.MaxValue;    //The size ofthe smallest compression type: int.MaxValue is assigned to make sure that the first compression to be checked will be smaaller than this value

                //Loop trough all saved compression techniques
                for (int i = 0; i < compressionTechniques.Length; i++)
                {
                    //If the current technique is smaller than the smallest technique which has been checked
                    if (compressionTechniques[i].Length < smallestSize)
                    {
                        //Mark this technique as smallest
                        smallestSize = compressionTechniques[i].Length;
                        smallestID   = i;
                    }
                }

                //Merge the number of the compression type of this layer with corresponding bitStream
                BitStreamFIFO tmpStream = new BitStreamFIFO();
                tmpStream.Write(smallestID, 3);    //This 3-bit integer indicates which technique the decompressor should use, and should be before the image data
                byteLayers[z % 8] = BitStreamFIFO.Merge(tmpStream, compressionTechniques[smallestID]);
            });

            //Combine all bitstreams & convert the result to a byte array
            byte[] outputStream = BitStreamFIFO.Merge(byteLayers).ToByteArray();

            //Return the data of all the bit channels combined
            return(outputStream);
        }