示例#1
0
        public GlobalColorTable(Stream inputStream, int quantityOfColors)
        {
            Colors = new Pixel[quantityOfColors];

            for (int i = 0; i < quantityOfColors; i++)
            {
                Colors[i] = new Pixel(StaticSecondaryMethods.ReadBytes(inputStream, 3));
            }
        }
示例#2
0
        public LogicalScreenDescriptor(Stream inputStream)
        {
            VERSION_HEADER = StaticSecondaryMethods.manyBytesToString(StaticSecondaryMethods.ReadBytes(inputStream, 6));
            WIDTH          = (ushort)((int)(StaticSecondaryMethods.ReadBytes(inputStream, 1)[0]) + ((int)(StaticSecondaryMethods.ReadBytes(inputStream, 1)[0]) << 8));
            HEIGHT         = (ushort)((int)(StaticSecondaryMethods.ReadBytes(inputStream, 1)[0]) + ((int)(StaticSecondaryMethods.ReadBytes(inputStream, 1)[0]) << 8));

            byte temp = StaticSecondaryMethods.ReadBytes(inputStream, 1)[0];

            GlobalColorTableFlag = Convert.ToBoolean(StaticSecondaryMethods.ReadBits(temp, 0, 1));
            Cr             = StaticSecondaryMethods.ReadBits(temp, 1, 3);
            S              = Convert.ToBoolean(StaticSecondaryMethods.ReadBits(temp, 4, 1));
            PIXEL          = StaticSecondaryMethods.ReadBits(temp, 5, 3);
            BackColorIndex = StaticSecondaryMethods.ReadBytes(inputStream, 1)[0];
            AspectRatio    = StaticSecondaryMethods.ReadBytes(inputStream, 1)[0];

            colorsQuantity = (int)Math.Pow(2, PIXEL + 1);
        }
示例#3
0
        public byte transparentColorIndex;      // индекс цвета задника

        public GraphicManagment(Stream s)
        {
            size = StaticSecondaryMethods.ReadBytes(s, 1)[0];

            byte[] block = StaticSecondaryMethods.ReadBytes(s, size);

            processingMethod      = StaticSecondaryMethods.ReadBits(block[0], 3, 3);
            userInputFlag         = Convert.ToBoolean(StaticSecondaryMethods.ReadBits(block[0], 6, 1));
            transparentColorFlag  = Convert.ToBoolean(StaticSecondaryMethods.ReadBits(block[0], 7, 1));
            animationPing         = (ushort)(block[1] + block[2] * 16);
            transparentColorIndex = block[3];

            if (StaticSecondaryMethods.ReadBytes(s, 1)[0] != 0x00)
            {
                throw new Exception("Неправильная попытка чтения блока граф-го управ-я");
            }
        }
示例#4
0
        //========================================================================================

        void init()
        {
            //          чтение логического дискриптора ну и задание списков
            logicalScreenDescriptor = new LogicalScreenDescriptor(MainStream);
            imageBlocks             = new List <ImageBlock>();
            ImagesByPoints          = new List <Pixel[, ]>();
            ImagesButRlImages       = new List <Bitmap>();

            //          чтение глобальной таблицы цветов при наличии
            if (logicalScreenDescriptor.GlobalColorTableFlag)
            {
                globalColorTable = new GlobalColorTable(MainStream, logicalScreenDescriptor.colorsQuantity);
            }

            //          Чтение всех структур
            byte marker = StaticSecondaryMethods.ReadBytes(MainStream, 1)[0];

            while (marker != (byte)Markers.END_BLOCK)
            {
                if (marker == (byte)Markers.EXTENSIONS)
                {
                    marker = StaticSecondaryMethods.ReadBytes(MainStream, 1)[0];
                    if (marker == (byte)Markers.GRAPHIC_MANAGMENT)
                    {
                        graphicManagment = new GraphicManagment(MainStream);
                    }
                }
                if (marker == (byte)Markers.IMAGE_BLOCK)
                {
                    imageBlocks.Add(new ImageBlock(MainStream));
                }
                marker = StaticSecondaryMethods.ReadBytes(MainStream, 1)[0];
            }

            for (int i = 0; i < imageBlocks.Count; i++)
            {
                ImagesByPoints.Add(StaticSecondaryMethods.LZWDecoder(
                                       globalColorTable.Colors,
                                       imageBlocks[i].data, imageBlocks[i].LZWMinSize,
                                       imageBlocks[i].width, imageBlocks[i].height));
                ImagesButRlImages.Add(StaticSecondaryMethods.ConvertPointsToBitmap(ImagesByPoints[i]));
            }

            size = new Size(ImagesByPoints[0].GetLength(0), ImagesByPoints[0].GetLength(0));
        }//          инициализирует всё вот то, что в гифе есть
示例#5
0
        public ImageBlock(Stream s)
        {
            row     = (ushort)((int)(StaticSecondaryMethods.ReadBytes(s, 1)[0]) + ((int)(StaticSecondaryMethods.ReadBytes(s, 1)[0]) << 8));
            collumn = (ushort)((int)(StaticSecondaryMethods.ReadBytes(s, 1)[0]) + ((int)(StaticSecondaryMethods.ReadBytes(s, 1)[0]) << 8));
            width   = (ushort)((int)(StaticSecondaryMethods.ReadBytes(s, 1)[0]) + ((int)(StaticSecondaryMethods.ReadBytes(s, 1)[0]) << 8));
            height  = (ushort)((int)(StaticSecondaryMethods.ReadBytes(s, 1)[0]) + ((int)(StaticSecondaryMethods.ReadBytes(s, 1)[0]) << 8));
            byte temp = StaticSecondaryMethods.ReadBytes(s, 1)[0];

            localColorTableUse    = Convert.ToBoolean(StaticSecondaryMethods.ReadBits(temp, 0, 1));
            throughtStringsRender = Convert.ToBoolean(StaticSecondaryMethods.ReadBits(temp, 1, 1));
            localColorTableSort   = Convert.ToBoolean(StaticSecondaryMethods.ReadBits(temp, 2, 1));
            localColorTablePIXEL  = StaticSecondaryMethods.ReadBits(temp, 5, 3);
            LZWMinSize            = StaticSecondaryMethods.ReadBytes(s, 1)[0];
            size = StaticSecondaryMethods.ReadBytes(s, 1)[0];

            while (size != 0)
            {
                data = StaticSecondaryMethods.ReadBytes(s, size);
                size = StaticSecondaryMethods.ReadBytes(s, 1)[0];
            }
        }
        static public Pixel[,] LZWDecoder(Pixel[] colors, byte[] data, byte LZWminSize, ushort width, ushort height)
        {
            // введение важного
            Pixel[,] result = new Pixel[width, height];
            List <Pixel[]> entry = new List <Pixel[]>();

            Pixel[] output;
            // минимальная длина она такая
            LZWminSize++;
            int    counter    = 0;
            int    readedBits = 0;
            ushort input      = 0;

            // введение последней важной частички
            if (LZWminSize > 8)
            {
                input += data[counter];
                counter++;
                readedBits = LZWminSize - 8;
                input     += (ushort)(StaticSecondaryMethods.ReadBits(data[counter], 8 - readedBits, readedBits) << 8);
            }
            else
            {
                input      = StaticSecondaryMethods.ReadBits(data[0], 8 - LZWminSize, LZWminSize);
                readedBits = LZWminSize;
            }
            Console.WriteLine($"Circle:{0}\tinput {input} counter {counter} cb{readedBits}");

            // вспомогательное
            List <Pixel[]> sRes = new List <Pixel[]>();

            Pixel[] flatRes;

            // заполнение первоначальное
            for (int i = 0; i < colors.Length; i++)
            {
                entry.Add(new Pixel[] { colors[i] });
            }
            entry.Add(null);
            entry.Add(null);

            int clearCode = colors.Length;
            int exitCode  = colors.Length + 1;

            long circleCount = 0;

            // цикол
            while (true)
            {
                // проверки на особые состояния
                if (input == clearCode)
                {
                    entry = new List <Pixel[]>();
                    for (int i = 0; i < colors.Length; i++)
                    {
                        entry.Add(new Pixel[1] {
                            colors[i]
                        });
                    }
                    entry.Add(null);
                    entry.Add(null);
                    clearCode = entry.Count - 2;
                    exitCode  = entry.Count - 1;
                }
                else if (input == exitCode)
                {
                    break;
                }
                else
                {
                    // работа
                    Console.WriteLine($"Circle:{0}\tinput {input} counter {counter} cb{readedBits} lzw {LZWminSize}");
                    output = entry[input];
                    entry.Add(output);

                    if (circleCount > 0 && entry[entry.Count - 2] != null)
                    {
                        entry[entry.Count - 2] = ConCatArrays(entry[entry.Count - 2], new Pixel[1] {
                            output[0]
                        });
                    }

                    if (input == (entry.Count - 2))
                    {
                        entry[entry.Count - 1] = entry[entry.Count - 2];
                    }

                    sRes.Add(entry[input]);
                }

                // считывание
                if (LZWminSize < 8)
                {
                    if (readedBits + LZWminSize > 8)
                    {
                        readedBits = 8 - readedBits;
                        input      = StaticSecondaryMethods.ReadBits(data[counter], 0, readedBits);
                        counter++;
                        byte temp = StaticSecondaryMethods.ReadBits(data[counter], 8 - (LZWminSize - readedBits), LZWminSize - readedBits);
                        input     += (byte)(temp << readedBits);
                        readedBits = LZWminSize - readedBits;
                    }
                    else
                    {
                        input       = StaticSecondaryMethods.ReadBits(data[counter], 8 - readedBits - LZWminSize, LZWminSize);
                        readedBits += LZWminSize;
                    }
                }
                else
                {
                    input = StaticSecondaryMethods.ReadBits(data[counter], 0, 8 - readedBits);
                    counter++;
                    int shift      = 8 - readedBits;
                    int leftToRead = LZWminSize - shift;

                    if (leftToRead > 8)
                    {
                        input      += (ushort)(data[counter] << readedBits);
                        shift      += 8;
                        leftToRead -= 8;
                        counter++;
                        throw new Exception("no");
                    }

                    input     += (ushort)(StaticSecondaryMethods.ReadBits(data[counter], 8 - leftToRead, leftToRead) << shift);
                    readedBits = leftToRead;
                }

                circleCount++;
                // увеличение лзв если надо
                if (entry.Count >= Math.Pow(2, LZWminSize))
                {
                    LZWminSize++;
                }
            }

            int x = 0;
            int y = 0;

            flatRes = ListFlatter(sRes);

            foreach (Pixel p in flatRes)
            {
                result[x, y] = p;
                x++;
                if (x == width)
                {
                    x = 0;
                    y++;
                }
            }

            return(result);
        }