//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);
        }
Exemple #2
0
        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;
        }