Beispiel #1
0
 public IMG(Stream stream)
 {
     using (BinaryReader reader = new BinaryReader(stream))
     {
         byte[] buffer2;
         if (!reader.ReadBytes(4).SequenceEqual <byte>(_defaultHeader))
         {
             throw new InvalidDataException("Invalid header.");
         }
         reader.ReadInt32();
         byte num = reader.ReadByte();
         if (num > 0x20)
         {
             string str = reader.ReadString(BinaryStringFormat.ZeroTerminated);
             this._description = ((char)num) + str;
             num = reader.ReadByte();
         }
         byte b = reader.ReadByte();
         this._compressed = b.GetBit(6);
         bool    bit        = b.GetBit(7);
         Color[] colorArray = null;
         if (bit)
         {
             short num3 = reader.ReadInt16();
             colorArray    = new Color[num3 + 1];
             colorArray[0] = Color.Black;
             for (int i = 1; i <= num3; i++)
             {
                 colorArray[i] = Color.FromArgb(reader.ReadByte(), reader.ReadByte(), reader.ReadByte());
             }
         }
         short width  = reader.ReadInt16();
         short height = reader.ReadInt16();
         if (num == 8)
         {
             if (colorArray == null)
             {
                 throw new InvalidDataException("Image is palettized, but does not specify a palette.");
             }
             this._bitmap = new System.Drawing.Bitmap(width, height, PixelFormat.Format8bppIndexed);
             ColorPalette palette = this._bitmap.Palette;
             for (int j = 0; j < colorArray.Length; j++)
             {
                 palette.Entries[j] = colorArray[j];
             }
             this._bitmap.Palette = palette;
         }
         if (this._compressed)
         {
             buffer2 = new byte[width * height];
             Compression.Decompress(reader, ref buffer2);
         }
         else
         {
             buffer2 = reader.ReadBytes(width * height);
         }
         BitmapData bitmapdata = this._bitmap.LockBits(new Rectangle(Point.Empty, this._bitmap.Size), ImageLockMode.WriteOnly, this._bitmap.PixelFormat);
         Marshal.Copy(buffer2, 0, bitmapdata.Scan0, buffer2.Length);
         this._bitmap.UnlockBits(bitmapdata);
     }
 }
Beispiel #2
0
 public IMG(BinaryReader b, ref List <Sprite> iSprite, ref List <Frame> iFrame)
 {
     byte[] buffer;
     this.Description = new List <byte>();
     this.Signature   = b.ReadInt32();
     this.FileLength  = b.ReadInt32();
     this.BitRate     = b.ReadByte();
     if ((this.BitRate != 1) && (this.BitRate != 8))
     {
         System.IO.Stream baseStream = b.BaseStream;
         baseStream.Position -= 1L;
         for (int i = 0; i < 0x100; i++)
         {
             this.Description.Add(b.ReadByte());
             if (this.Description[i] == 0)
             {
                 break;
             }
         }
         this.BitRate = b.ReadByte();
     }
     this.Flags = b.ReadByte();
     if ((this.Flags == 0x80) || (this.Flags == 0xc0))
     {
         this.FlagPalette = true;
     }
     if ((this.Flags == 0x40) || (this.Flags == 0xc0))
     {
         this.FlagCompressed = true;
     }
     if (this.FlagPalette)
     {
         this.PaletteCount = b.ReadInt16();
         this.Palette      = new byte[this.PaletteCount, 3];
         for (int j = 0; j < this.PaletteCount; j++)
         {
             this.Palette[j, 0] = b.ReadByte();
             this.Palette[j, 1] = b.ReadByte();
             this.Palette[j, 2] = b.ReadByte();
         }
     }
     else if (this.BitRate == 1)
     {
         this.PaletteCount = 1;
         this.Palette      = new byte[, ] {
             { 0xff, 0xff, 0xff }
         };
     }
     this.Width  = b.ReadInt16();
     this.Height = b.ReadInt16();
     this.Size   = this.Width * this.Height;
     if (this.BitRate == 1)
     {
         this.Size /= 8;
     }
     if (this.FlagCompressed)
     {
         buffer = new byte[this.Size];
         Compression.Decompress(b, ref buffer);
     }
     else
     {
         buffer = b.ReadBytes(this.Size);
     }
     iFrame.Add(new Frame(this.Palette, 0, 0, 0, 0, this.Width, this.Height, buffer));
 }
Beispiel #3
0
        public SPR(BinaryReader b, ref List <Sprite> iSprite, ref List <Frame> iFrame)
        {
            this.Signature  = b.ReadInt32();
            this.FileLength = b.ReadInt32();
            this.BitRate    = b.ReadByte();
            if ((this.BitRate != 1) && (this.BitRate != 8))
            {
                System.IO.Stream baseStream = b.BaseStream;
                baseStream.Position -= 1L;
                for (int i = 0; i < 0x100; i++)
                {
                    this.Description.Add(b.ReadByte());
                    if (this.Description[i] == 0)
                    {
                        break;
                    }
                }
                this.BitRate = b.ReadByte();
            }
            this.Flags = b.ReadByte();
            if ((this.Flags == 0x80) || (this.Flags == 0xc0))
            {
                this.FlagPalette = true;
            }
            if ((this.Flags == 0x40) || (this.Flags == 0xc0))
            {
                this.FlagCompressed = true;
            }
            if (this.FlagPalette)
            {
                this.PaletteCount = b.ReadInt16();
                this.Palette      = new byte[this.PaletteCount, 3];
                for (int j = 0; j < this.PaletteCount; j++)
                {
                    this.Palette[j, 0] = b.ReadByte();
                    this.Palette[j, 1] = b.ReadByte();
                    this.Palette[j, 2] = b.ReadByte();
                }
            }
            if (this.FlagCompressed)
            {
                this.Streams = b.ReadInt16();
                if (this.Description.Count == 0)
                {
                    int num3 = (4 - (((int)b.BaseStream.Position) % 4)) % 4;
                    for (int num4 = 0; num4 < num3; num4++)
                    {
                        b.ReadByte();
                    }
                }
                Stream[] streamArray = new Stream[this.Streams];
                for (int k = 0; k < this.Streams; k++)
                {
                    streamArray[k].CompressedStart    = b.ReadInt32();
                    streamArray[k].Unknown            = b.ReadInt32();
                    streamArray[k].DecompressedLength = b.ReadInt32();
                }
                this.Unknown4       = b.ReadInt16();
                this.FrameRate      = b.ReadInt16();
                this.AnimationFlags = b.ReadInt16();
                this.Width          = b.ReadInt16();
                this.Height         = b.ReadInt16();
                this.FrameCount     = b.ReadInt16();
                for (int m = 0; m < this.FrameCount; m++)
                {
                    short position = b.ReadInt16();
                    b.ReadByte();
                    byte stream = b.ReadByte();
                    iFrame.Add(new Frame(this.Palette, stream, position, b.ReadInt16(), b.ReadInt16(), b.ReadInt16(), b.ReadInt16()));
                }
                for (int n = 0; n < this.Streams; n++)
                {
                    streamArray[n].Data = new byte[streamArray[n].DecompressedLength];
                    Compression.Decompress(b, ref streamArray[n].Data);
                }
                for (int num10 = 0; num10 < this.FrameCount; num10++)
                {
                    for (int num11 = 0; num11 < iFrame[num10].Size; num11++)
                    {
                        //if (streamArray[iFrame[num10].Stream].Data[(iFrame[num10].Position - 1) + num11] <= streamArray[iFrame[num10].Stream].Data.Length)
                        iFrame[num10].Data[num11] = streamArray[iFrame[num10].Stream].Data[(iFrame[num10].Position - 1) + num11];
                    }
                }
            }
            else
            {
                this.Unknown4       = b.ReadInt16();
                this.FrameRate      = b.ReadInt16();
                this.AnimationFlags = b.ReadInt16();
                this.Width          = b.ReadInt16();
                this.Height         = b.ReadInt16();
                this.FrameCount     = b.ReadInt16();
                int num12 = (4 - (((int)b.BaseStream.Position) % 4)) % 4;
                for (int num13 = 0; num13 < num12; num13++)
                {
                    b.ReadByte();
                }
                for (int num14 = 0; num14 < this.FrameCount; num14++)
                {
                    iFrame.Add(new Frame(this.Palette, 0, (short)b.ReadInt32(), b.ReadInt16(), b.ReadInt16(), b.ReadInt16(), b.ReadInt16()));
                }
                for (int num15 = 0; num15 < this.FrameCount; num15++)
                {
                    iFrame[num15].Data = b.ReadBytes(iFrame[num15].Size);
                }
            }
            iSprite.Add(new Sprite(this.Palette, this.AnimationFlags, this.Width, this.Height, 0, this.FrameCount, 0, (byte)this.FrameRate));
            iSprite[iSprite.Count - 1].CalculateCrop(ref iFrame);
            if (this.Description.Count != 0)
            {
                iSprite[iSprite.Count - 1].Description = this.Description;
            }

            Sprites = iSprite;
            Frames  = iFrame;
        }