Ejemplo n.º 1
0
        /** <summary> Reads the palette from the graphics data. </summary> */
        public Palette ReadPalette(BinaryReader reader, long startPosition, int index)
        {
            int        i     = index;
            ImageEntry entry = imageDirectory.entries[i];

            if (entry.Flags.HasFlag(ImageFlags.DirectBitmap))
            {
            }
            else if (entry.Flags.HasFlag(ImageFlags.PaletteEntries))
            {
                Palette palette = new Palette(entry);
                reader.BaseStream.Position = startPosition + entry.StartAddress;

                // Read each color
                for (int j = 0; j < entry.Width; j++)
                {
                    // Yes, the colors are in the order blue, green, red
                    byte blue  = reader.ReadByte();
                    byte green = reader.ReadByte();
                    byte red   = reader.ReadByte();

                    palette.Colors[j] = Color.FromArgb(red, green, blue);
                }
                return(palette);
            }
            return(null);
        }
Ejemplo n.º 2
0
 /** <summary> Constructs a palette image with the specified dimensions and compression type. </summary> */
 public PaletteImage(Size size, Point offset, ImageFlags compressionType)
 {
     this.pixels        = new byte[size.Width, size.Height];
     this.entry         = new ImageEntry();
     this.entry.Flags   = compressionType;
     this.entry.Width   = (short)size.Width;
     this.entry.Height  = (short)size.Height;
     this.entry.XOffset = (short)offset.X;
     this.entry.YOffset = (short)offset.Y;
 }
Ejemplo n.º 3
0
 /** <summary> Constructs a palette image with the specified dimensions and compression type. </summary> */
 public PaletteImage(int width, int height, int xOffset, int yOffset, ImageFlags compressionType)
 {
     this.pixels        = new byte[width, height];
     this.entry         = new ImageEntry();
     this.entry.Flags   = compressionType;
     this.entry.Width   = (short)width;
     this.entry.Height  = (short)height;
     this.entry.XOffset = (short)xOffset;
     this.entry.YOffset = (short)yOffset;
 }
Ejemplo n.º 4
0
 /** <summary> Constructs a palette image with the specified dimensions. </summary> */
 public PaletteImage(Size size)
 {
     this.pixels        = new byte[size.Width, size.Height];
     this.entry         = new ImageEntry();
     this.entry.Flags   = ImageFlags.DirectBitmap;
     this.entry.Width   = (short)size.Width;
     this.entry.Height  = (short)size.Height;
     this.entry.XOffset = 0;
     this.entry.YOffset = 0;
 }
Ejemplo n.º 5
0
 /** <summary> Constructs a palette image with the specified dimensions. </summary> */
 public PaletteImage(int width, int height)
 {
     this.pixels        = new byte[width, height];
     this.entry         = new ImageEntry();
     this.entry.Flags   = ImageFlags.DirectBitmap;
     this.entry.Width   = (short)width;
     this.entry.Height  = (short)height;
     this.entry.XOffset = 0;
     this.entry.YOffset = 0;
 }
Ejemplo n.º 6
0
        //========= CONSTRUCTORS =========
        #region Constructors

        /** <summary> Constructs the default palette image. </summary> */
        public PaletteImage()
        {
            this.pixels        = new byte[1, 1];
            this.entry         = new ImageEntry();
            this.entry.Flags   = ImageFlags.DirectBitmap;
            this.entry.Width   = 1;
            this.entry.Height  = 1;
            this.entry.XOffset = 0;
            this.entry.YOffset = 0;
        }
Ejemplo n.º 7
0
 /** <summary> Constructs a palette with the specified colors and offset. </summary> */
 public Palette(Color[] colors, int offset)
 {
     this.colors        = colors;
     this.entry         = new ImageEntry();
     this.entry.Flags   = ImageFlags.PaletteEntries;
     this.entry.Width   = (short)colors.Length;
     this.entry.Height  = 0;
     this.entry.XOffset = (short)offset;
     this.entry.YOffset = 0;
 }
Ejemplo n.º 8
0
 /** <summary> Constructs a palette with the specified number of colors and offset. </summary> */
 public Palette(int numColors, int offset)
 {
     this.colors        = new Color[numColors];
     this.entry         = new ImageEntry();
     this.entry.Flags   = ImageFlags.PaletteEntries;
     this.entry.Width   = (short)numColors;
     this.entry.Height  = 0;
     this.entry.XOffset = (short)offset;
     this.entry.YOffset = 0;
 }
Ejemplo n.º 9
0
        //========= CONSTRUCTORS =========
        #region Constructors

        /** <summary> Constructs the default palette. </summary> */
        public Palette()
        {
            this.colors        = new Color[1];
            this.entry         = new ImageEntry();
            this.entry.Flags   = ImageFlags.PaletteEntries;
            this.entry.Width   = 1;
            this.entry.Height  = 0;
            this.entry.XOffset = 0;
            this.entry.YOffset = 0;
        }
Ejemplo n.º 10
0
        /** <summary> Reads the image directory. </summary> */
        public void ReadCSG1(BinaryReader reader)
        {
            //int count = reader.ReadInt32();
            //this.ScanLineLength = reader.ReadInt32();

            //for (int i = 0; i < count; i++) {
            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                ImageEntry entry = new ImageEntry();
                entry.Read(reader);
                this.entries.Add(entry);
            }
        }
Ejemplo n.º 11
0
        //=========== READING ============
        #region Reading

        /** <summary> Reads the image directory. </summary> */
        public void Read(BinaryReader reader, bool quickLoad = false)
        {
            int count = reader.ReadInt32();

            this.ScanLineLength = reader.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                ImageEntry entry = new ImageEntry();
                entry.Read(reader);
                if (!quickLoad || i < 168)
                {
                    this.entries.Add(entry);
                }
            }
        }
Ejemplo n.º 12
0
 /** <summary> Constructs a palette image from the specified image entry. </summary> */
 internal PaletteImage(ImageEntry entry)
 {
     this.pixels = new byte[entry.Width, entry.Height];
     this.entry  = entry;
 }
Ejemplo n.º 13
0
        /** <summary> Writes the graphics data. </summary> */
        public void Write(BinaryWriter writer)
        {
            long startPosition = writer.BaseStream.Position;

            for (int i = 0; i < imageDirectory.NumEntries; i++)
            {
                ImageEntry entry = imageDirectory.entries[i];
                entry.StartAddress = (uint)(writer.BaseStream.Position - startPosition);

                if (entry.Flags.HasFlag(ImageFlags.DirectBitmap))
                {
                    if (!entry.Flags.HasFlag(ImageFlags.CompactedBitmap))
                    {
                        if (entry.Flags.HasFlag(ImageFlags.LandTile))
                        {
                            // Don't know what this flag does, but images can still be written normally.
                        }
                        PaletteImage paletteImage = this.paletteImages[i];

                        // Write each row
                        for (int y = 0; y < entry.Height; y++)
                        {
                            // Write each pixel in the row
                            for (int x = 0; x < entry.Width; x++)
                            {
                                writer.Write(paletteImage.Pixels[x, y]);
                            }
                        }
                    }
                    else
                    {
                        if (entry.Flags.HasFlag(ImageFlags.LandTile))
                        {
                            // Don't know what this flag does, but images can still be written normally.
                        }
                        PaletteImage paletteImage = this.paletteImages[i];

                        List <ScanLine> scanLines  = new List <ScanLine>();
                        ushort[]        rowOffsets = new ushort[entry.Height];
                        ushort          rowOffset  = (ushort)(entry.Height * 2);

                        // Write the scan lines in every row and figure out the scan line row offsets
                        for (int y = 0; y < (int)entry.Height; y++)
                        {
                            rowOffsets[y] = rowOffset;

                            ScanLine scanLine = new ScanLine();
                            scanLine.Row = (short)y;

                            // Continue until the next row
                            while ((scanLine.Count & 0x80) == 0x00)
                            {
                                // Reset the scan line count
                                scanLine.Count = 0;

                                // Find each scan line and then check if there's another one in the row
                                bool finishedScanLine = false;
                                bool lastScanLine     = true;
                                for (int x = 0; x + (int)scanLine.Offset < (int)entry.Width; x++)
                                {
                                    if (!finishedScanLine)
                                    {
                                        if (scanLine.Count == 0)
                                        {
                                            // If the scan line hasn't started yet, increment the offset
                                            if (paletteImage.Pixels[x + scanLine.Offset, y] == 0x00)
                                            {
                                                scanLine.Offset++;
                                                x--;
                                            }
                                            else
                                            {
                                                scanLine.Count = 1;
                                            }
                                        }
                                        else if (paletteImage.Pixels[x + scanLine.Offset, y] == 0x00 || x == 0x7F)
                                        {
                                            // If the next pixel is transparent or the scan line is as big as possible, finish the line
                                            finishedScanLine = true;
                                        }
                                        else
                                        {
                                            // Increment the scan line byte count
                                            scanLine.Count++;
                                        }
                                    }
                                    else if (paletteImage.Pixels[x + scanLine.Offset, y] != 0x00)
                                    {
                                        // There is another scan line after this
                                        lastScanLine = false;
                                        break;
                                    }
                                }
                                // Set the end flag if the scan line is the last in the row
                                if (lastScanLine)
                                {
                                    scanLine.Count |= 0x80;
                                }
                                // If the row has all transparent pixels, set the offset to 0
                                if (scanLine.Count == 0)
                                {
                                    scanLine.Offset = 0;
                                    scanLine.Count  = 0;
                                }

                                rowOffset += (ushort)(2 + (scanLine.Count & 0x7F));
                                scanLines.Add(scanLine);

                                // Increment the scan line count
                                if (!lastScanLine)
                                {
                                    scanLine.Offset += (byte)(scanLine.Count & 0x7F);
                                }
                            }
                        }

                        // Write the row offsets
                        for (int j = 0; j < entry.Height; j++)
                        {
                            writer.Write(rowOffsets[j]);
                        }

                        // Write the scan lines
                        for (int j = 0; j < scanLines.Count; j++)
                        {
                            writer.Write(scanLines[j].Count);
                            writer.Write(scanLines[j].Offset);
                            for (int k = 0; k < (int)(scanLines[j].Count & 0x7F); k++)
                            {
                                try {
                                    writer.Write(paletteImage.Pixels[k + scanLines[j].Offset, scanLines[j].Row]);
                                }
                                catch (Exception) {
                                }
                            }
                        }
                    }
                }
                else if (entry.Flags.HasFlag(ImageFlags.PaletteEntries))
                {
                    Palette palette = this.palettes[i];

                    // Write each color
                    for (int j = 0; j < entry.Width; j++)
                    {
                        // Yes, the colors are in the order blue, green, red
                        writer.Write(palette.Colors[j].B);
                        writer.Write(palette.Colors[j].G);
                        writer.Write(palette.Colors[j].R);
                    }
                }
            }
        }
Ejemplo n.º 14
0
        /** <summary> Reads the graphics data. </summary> */
        public void Read(BinaryReader reader)
        {
            long startPosition = reader.BaseStream.Position;

            for (int i = 0; i < imageDirectory.NumEntries; i++)
            {
                ImageEntry entry = imageDirectory.entries[i];
                if (entry.Flags.HasFlag(ImageFlags.DirectBitmap))
                {
                    if (!entry.Flags.HasFlag(ImageFlags.CompactedBitmap))
                    {
                        if (entry.Flags.HasFlag(ImageFlags.LandTile))
                        {
                            // Don't know what this flag does, but images can still be read normally.
                        }
                        PaletteImage paletteImage = new PaletteImage(entry);
                        reader.BaseStream.Position = startPosition + entry.StartAddress;

                        // Read each row
                        for (int y = 0; y < entry.Height; y++)
                        {
                            for (int x = 0; x < entry.Width; x++)
                            {
                                byte b = reader.ReadByte();
                                paletteImage.Pixels[x, y] = b;
                            }
                        }
                        this.paletteImages.Add(paletteImage);
                        this.palettes.Add(null);
                        this.numImages++;
                    }
                    else
                    {
                        if (entry.Flags.HasFlag(ImageFlags.LandTile))
                        {
                            // Don't know what this flag does, but images can still be read normally.
                        }
                        PaletteImage paletteImage = new PaletteImage(entry);
                        uint[]       rowOffsets   = new uint[entry.Height];
                        reader.BaseStream.Position = startPosition + entry.StartAddress;

                        // Read the row offsets
                        for (int j = 0; j < entry.Height; j++)
                        {
                            rowOffsets[j] = reader.ReadUInt16();
                        }

                        // Read the scan lines in each row
                        for (int j = 0; j < entry.Height; j++)
                        {
                            reader.BaseStream.Position = startPosition + entry.StartAddress + rowOffsets[j];
                            byte b1 = 0;
                            byte b2 = 0;

                            // A MSB of 1 means the last scan line in a row
                            while ((b1 & 0x80) == 0)
                            {
                                // Read the number of bytes of data
                                b1 = reader.ReadByte();
                                // Read the offset from the left edge of the image
                                b2 = reader.ReadByte();
                                for (int k = 0; k < (int)(b1 & 0x7F); k++)
                                {
                                    byte b3 = reader.ReadByte();
                                    paletteImage.Pixels[(int)b2 + k, j] = b3;
                                }
                            }
                        }
                        this.paletteImages.Add(paletteImage);
                        this.palettes.Add(null);
                        this.numImages++;
                    }
                }
                else if (entry.Flags.HasFlag(ImageFlags.PaletteEntries))
                {
                    Palette palette = new Palette(entry);
                    reader.BaseStream.Position = startPosition + entry.StartAddress;

                    // Read each color
                    for (int j = 0; j < entry.Width; j++)
                    {
                        // Yes, the colors are in the order blue, green, red
                        byte blue  = reader.ReadByte();
                        byte green = reader.ReadByte();
                        byte red   = reader.ReadByte();

                        palette.Colors[j] = Color.FromArgb(red, green, blue);
                    }
                    this.paletteImages.Add(null);
                    this.palettes.Add(palette);
                    this.numPalettes++;
                }
            }
        }
Ejemplo n.º 15
0
        /** <summary> Reads the palette image from the graphics data. </summary> */
        public PaletteImage ReadPaletteImage(BinaryReader reader, long startPosition, int index)
        {
            int        i     = index;
            ImageEntry entry = imageDirectory.entries[i];

            if (entry.Flags.HasFlag(ImageFlags.DirectBitmap))
            {
                if (!entry.Flags.HasFlag(ImageFlags.CompactedBitmap))
                {
                    if (entry.Flags.HasFlag(ImageFlags.LandTile))
                    {
                        // Don't know what this flag does, but images can still be read normally.
                    }
                    PaletteImage paletteImage = new PaletteImage(entry);
                    reader.BaseStream.Position = startPosition + entry.StartAddress;

                    // Read each row
                    for (int y = 0; y < entry.Height; y++)
                    {
                        for (int x = 0; x < entry.Width; x++)
                        {
                            byte b = reader.ReadByte();
                            paletteImage.Pixels[x, y] = b;
                        }
                    }
                    return(paletteImage);
                }
                else
                {
                    if (entry.Flags.HasFlag(ImageFlags.LandTile))
                    {
                        // Don't know what this flag does, but images can still be read normally.
                    }
                    PaletteImage paletteImage = new PaletteImage(entry);
                    uint[]       rowOffsets   = new uint[entry.Height];
                    reader.BaseStream.Position = startPosition + entry.StartAddress;

                    // Read the row offsets
                    for (int j = 0; j < entry.Height; j++)
                    {
                        rowOffsets[j] = reader.ReadUInt16();
                    }

                    // Read the scan lines in each row
                    for (int j = 0; j < entry.Height; j++)
                    {
                        reader.BaseStream.Position = startPosition + entry.StartAddress + rowOffsets[j];
                        byte b1 = 0;
                        byte b2 = 0;

                        // A MSB of 1 means the last scan line in a row
                        while ((b1 & 0x80) == 0)
                        {
                            // Read the number of bytes of data
                            b1 = reader.ReadByte();
                            // Read the offset from the left edge of the image
                            b2 = reader.ReadByte();
                            for (int k = 0; k < (int)(b1 & 0x7F); k++)
                            {
                                byte b3 = reader.ReadByte();
                                paletteImage.Pixels[(int)b2 + k, j] = b3;
                            }
                        }
                    }
                    return(paletteImage);
                }
            }
            else if (entry.Flags.HasFlag(ImageFlags.PaletteEntries))
            {
            }
            return(null);
        }
Ejemplo n.º 16
0
 /** <summary> Constructs a palette with the specified image entry. </summary> */
 internal Palette(ImageEntry entry)
 {
     this.colors = new Color[entry.Width];
     this.entry  = entry;
 }