Esempio n. 1
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 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 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 AccessibleBitmapBytewise Decompress(byte[] inBytes, AccessibleBitmapBytewise inBitmap, out byte[] restBytes, int byteLayer)
        {
            //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 byte of this channel from the correct position in the input array to the current pixel
                    inBitmap.SetPixelByte(x, y, byteLayer, inBytes[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[inBytes.Length - (inBitmap.width * inBitmap.height)];
            Array.Copy(inBytes, inBitmap.width * inBitmap.height, restBytes, 0, restBytes.Length);

            //Return the modified bitmap so the rest of the channels can be added to complete it
            return(inBitmap);
        }