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 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);
        }
Beispiel #3
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;
        }
        //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);
        }
Beispiel #5
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);
        }