Exemplo n.º 1
0
        public void ReadVGA(byte[] data)
        {
            MemoryStream ms         = new MemoryStream(data);
            var          loopsCount = ms.ReadB();

            ms.Position++; // Skip const 80
            var mask = ms.ReadUShortBE();

            ms.Position += 2; // Skip unkown
            var palOffset = ms.ReadUShortBE();

            ushort[] offsets = new ushort[loopsCount];
            for (int i = 0; i < loopsCount; i++)
            {
                offsets[i] = ms.ReadUShortBE();
            }

            for (int i = 0; i < loopsCount; i++)
            {
                var loop = new Loop();
                Loops.Add(loop);

                ms.Position = offsets[i];
                var cellsCount = ms.ReadUShortBE();
                ms.Position += 2; // Skip unknown
                ushort[] cellOffsets = new ushort[cellsCount];
                for (int j = 0; j < cellsCount; j++)
                {
                    cellOffsets[j] = ms.ReadUShortBE();
                }

                for (int j = 0; j < cellsCount; j++)
                {
                    var cell = new Cell(this);
                    loop.Cells.Add(cell);

                    ms.Position = cellOffsets[j];

                    cell.Width            = ms.ReadUShortBE();
                    cell.Height           = ms.ReadUShortBE();
                    cell.X                = ms.ReadB();
                    cell.Y                = ms.ReadB();
                    cell.TransparentColor = ms.ReadB();
                    ms.Position++; // Skip unknown

                    cell.Pixels = new byte[cell.Width * cell.Height];
                    PicImage.ReadImageData(ms, ms, cell.Pixels, cell.TransparentColor);
                }
            }

            ms.Position = palOffset;
            ReadPalette(ms);
        }
Exemplo n.º 2
0
        public void ReadVGA11(byte[] data)
        {
            MemoryStream ms         = new MemoryStream(data);
            var          headerSize = ms.ReadUShortBE();

            if (headerSize < 14)
            {
                throw new FormatException();
            }
            var loopsCount = ms.ReadB();
            var scaleFlags = ms.ReadB();
            var testb      = ms.ReadB();
            //if (testb != 1) throw new FormatException();

            var sci32ScaleRes      = ms.ReadB();
            var totalNumberOfCells = ms.ReadUShortBE();
            var palOffset          = ms.ReadUIntBE();
            var loopHeaderSize     = ms.ReadB();

            if (loopHeaderSize < 16)
            {
                throw new FormatException();
            }

            var cellHeaderSize = ms.ReadB();

            if (cellHeaderSize != 36 && cellHeaderSize != 52)
            {
                throw new FormatException();
            }

            var nativeResolution = ms.ReadUShortBE();

            if (palOffset > 0)
            {
                ms.Position = palOffset;
                ReadPalette(ms);
            }

            for (int l = 0; l < loopsCount; l++)
            {
                var loop = new Loop();
                Loops.Add(loop);

                ms.Position = headerSize + 2 + loopHeaderSize * l;

                var mirrorInfo = ms.ReadB();
                var isMirror   = ms.ReadB();
                var cellCount  = ms.ReadB();
                ms.Position += 9;
                var cellOffset = ms.ReadUIntBE();

                if (mirrorInfo == 255)
                {
                    for (int c = 0; c < cellCount; c++)
                    {
                        var cell = new Cell(this);
                        loop.Cells.Add(cell);

                        ms.Position = cellOffset + cellHeaderSize * c;

                        cell.Width            = ms.ReadUShortBE();
                        cell.Height           = ms.ReadUShortBE();
                        cell.X                = ms.ReadShortBE();
                        cell.Y                = ms.ReadShortBE();
                        cell.TransparentColor = ms.ReadB();

                        var always_0xa        = ms.ReadB();
                        var temp2             = ms.ReadB();
                        var temp3             = ms.ReadB();
                        var totalCellDataSize = ms.ReadUIntBE();
                        var rleCellDataSize   = ms.ReadUIntBE();
                        var paletteOffset     = ms.ReadUIntBE();
                        var offsetRLE         = ms.ReadUIntBE();
                        if (offsetRLE == 0)
                        {
                            throw new FormatException();
                        }
                        var offsetLiteral = ms.ReadUIntBE();
                        var perRowOffsets = ms.ReadUIntBE();
                        if (always_0xa == 0xa || always_0xa == 0x8a)
                        {
                            if (offsetLiteral == 0)
                            {
                                throw new FormatException();
                            }
                        }
                        else
                        {
                            if (offsetLiteral != 0)
                            {
                                throw new FormatException();
                            }
                        }

                        ms.Position = offsetRLE;
                        if (offsetLiteral == 0)
                        {
                            cell.Pixels = ms.ReadBytes(cell.Width * cell.Height);
                        }
                        else
                        {
                            ms.Position = offsetRLE;
                            var msLiteral = new MemoryStream(data);
                            msLiteral.Position = offsetLiteral;
                            cell.Pixels        = new byte[cell.Width * cell.Height];
                            PicImage.ReadImageData(ms, msLiteral, cell.Pixels, cell.TransparentColor);
                        }
                    }
                }
                else
                {
                    foreach (var c in Loops[mirrorInfo].Cells)
                    {
                        var cell = new Cell(this);
                        loop.Cells.Add(cell);
                        cell.Width  = c.Width;
                        cell.Height = c.Height;
                        cell.X      = (short)-c.X;
                        cell.Y      = c.Y;
                        cell.Pixels = new byte[cell.Width * cell.Height];

                        for (int y = 0; y < cell.Height; y++)
                        {
                            for (int x = 0; x < cell.Width; x++)
                            {
                                cell.Pixels[y * cell.Width + x] = c.Pixels[y * cell.Width + (cell.Width - 1 - x)];
                            }
                        }
                    }
                }
            }
        }