public TilesetHeader(BinaryReader reader)
 {
     sectionHead = new SectionHeader(reader);
     tagCount    = reader.ReadUInt32();
     pixelWidth  = reader.ReadUInt32();
     pixelHeight = reader.ReadUInt32();
     bitDepth    = reader.ReadUInt32();
     flags       = reader.ReadUInt32();
 }
        private static void ValidatePaletteHeader(SectionHeader paletteHeader)
        {
            if (paletteHeader.tag != TilesetCommon.DefaultTagData)
            {
                TilesetCommon.throwReadError("Palette Header Tag", paletteHeader.tag, TilesetCommon.DefaultTagData);
            }

            if (paletteHeader.length != TilesetCommon.DefaultPaletteHeaderSize)
            {
                TilesetCommon.throwReadError("Palette Header Section Size", paletteHeader.length, TilesetCommon.DefaultPaletteHeaderSize);
            }
        }
        private static void ValidateFileSignatureHeader(SectionHeader fileSignatureHeader)
        {
            if (fileSignatureHeader.tag != TagFileSignature)
            {
                TilesetCommon.throwReadError("File Signature", fileSignatureHeader.tag, TagFileSignature);
            }

            if (fileSignatureHeader.length == 0)
            {
                throw new System.Exception("Tileset property File Length reads " +
                                           fileSignatureHeader.length + ", which is too small.");
            }
        }
        private static void ValidatePixelHeader(SectionHeader pixelHeader, uint height)
        {
            if (pixelHeader.tag != TilesetCommon.DefaultTagData)
            {
                TilesetCommon.throwReadError("Pixel Header Tag", pixelHeader.tag, TilesetCommon.DefaultTagData);
            }

            uint expectedLength = CalculatePixelHeaderLength(height);

            if (pixelHeader.length != expectedLength)
            {
                TilesetCommon.throwReadError("Pixel Header Length", pixelHeader.length, expectedLength);
            }
        }
        // Write tileset in Outpost 2's custom bitmap format.
        // To write tileset in standard bitmap format, use BitmapFile::WriteIndexed
        public static void WriteCustomTileset(Stream streamToWrite, BitmapFile tileset)
        {
            tileset = tileset.Clone();

            ValidateTileset(tileset);

            // OP2 Custom Tileset assumes a positive height and TopDown Scan Line (Contradicts Windows Bitmap File Format)
            if (tileset.GetScanLineOrientation() == ScanLineOrientation.BottomUp)
            {
                tileset.InvertScanLines();
            }
            uint absoluteHeight = (uint)tileset.AbsoluteHeight();

            SectionHeader fileSignature = new SectionHeader(TagFileSignature, CalculatePbmpSectionSize(absoluteHeight));
            TilesetHeader tilesetHeader = TilesetHeader.Create(absoluteHeight / TilesetHeader.DefaultPixelHeightMultiple);
            PpalHeader    ppalHeader    = PpalHeader.Create();

            SectionHeader paletteHeader = new SectionHeader(TilesetCommon.DefaultTagData, TilesetCommon.DefaultPaletteHeaderSize);

            SwapPaletteRedAndBlue(tileset.palette);

            SectionHeader pixelHeader = new SectionHeader(TilesetCommon.DefaultTagData, CalculatePixelHeaderLength(absoluteHeight));

            using (BinaryWriter writer = new BinaryWriter(streamToWrite, System.Text.Encoding.UTF8, true))
            {
                fileSignature.Serialize(writer);
                tilesetHeader.Serialize(writer);
                ppalHeader.Serialize(writer);
                paletteHeader.Serialize(writer);
                for (int i = 0; i < tileset.palette.Length; ++i)
                {
                    tileset.palette[i].Serialize(writer);
                }
                pixelHeader.Serialize(writer);
                writer.Write(tileset.pixels);
            }
        }
 public PpalHeader(BinaryReader reader)
 {
     ppal     = new SectionHeader(reader);
     head     = new SectionHeader(reader);
     tagCount = reader.ReadUInt32();
 }