public BLP(BinaryReader br) { _buffer = br.ReadBytes((int)br.BaseStream.Length); br.BaseStream.Position = 0; // Checking for correct Magic-Code if (br.ReadUInt32() != 0x32504c42) { throw new Exception("Invalid BLP Format."); } // Reading type Version = br.ReadUInt32(); if (Version != 1) { throw new Exception($"Invalid version."); } ColorEncoding = br.ReadEnum <ColorFileFormat>(); AlphaSize = br.ReadByte(); PreferredFormat = br.ReadEnum <BLPPixelFormat>(); HasMips = br.ReadByte(); Width = br.ReadInt32(); Height = br.ReadInt32(); MipOffsets = br.ReadStructArray <uint>(16); MipSizes = br.ReadStructArray <uint>(16); // fix miptable sizes int mipCount = Math.Max(MipOffsets.Count(x => x != 0) - 1, 0); Array.Resize(ref MipOffsets, mipCount); Array.Resize(ref MipSizes, mipCount); // When Encoding is 1, there is no image compression and we have to read a color palette if (ColorEncoding == ColorFileFormat.Palette) { _paletteBGRA = br.ReadStructArray <CRGBA>(256); } else { _paletteBGRA = new CRGBA[256]; } }
/// <summary> /// Initializes a new instance of the <see cref="Warcraft.BLP.BLPHeader"/> class. /// This constructor creates a header from input data read from a BLP file. /// Usually, this is 148 bytes. /// </summary> /// <param name="inData">ExtendedData.</param> public BLPHeader(byte[] inData) { using (MemoryStream ms = new MemoryStream(inData)) { using (BinaryReader br = new BinaryReader(ms)) { this.Signature = new string(br.ReadChars(4)); if (Enum.TryParse(this.Signature, out this.Format)) { if (this.Format == BLPFormat.BLP2) { this.Version = (uint)br.ReadInt32(); } if (this.Format == BLPFormat.BLP2) { this.CompressionType = (TextureCompressionType)br.ReadByte(); } else { this.CompressionType = (TextureCompressionType)br.ReadUInt32(); } if (this.Format == BLPFormat.BLP2) { this.AlphaBitDepth = br.ReadByte(); } else { this.AlphaBitDepth = br.ReadUInt32(); } // BLP0 & BLP1 stores the resolution here if (this.Format < BLPFormat.BLP2) { this.Resolution = new Resolution(br.ReadUInt32(), br.ReadUInt32()); } if (this.Format == BLPFormat.BLP2) { this.PixelFormat = (BLPPixelFormat)br.ReadByte(); } else { this.PixelFormat = (BLPPixelFormat)br.ReadUInt32(); } if (this.Format == BLPFormat.BLP2) { this.MipMapType = br.ReadByte(); } else { this.MipMapType = br.ReadUInt32(); } // BLP2 stores the resolution here if (this.Format == BLPFormat.BLP2) { this.Resolution = new Resolution(br.ReadUInt32(), br.ReadUInt32()); } this.MipMapOffsets = new List <uint>(); for (int i = 0; i < 16; ++i) { uint offset = br.ReadUInt32(); if (offset > 0) { this.MipMapOffsets.Add(offset); } } this.MipMapSizes = new List <uint>(); for (int i = 0; i < 16; ++i) { uint size = br.ReadUInt32(); if (size > 0) { this.MipMapSizes.Add(size); } } } else { throw new FileLoadException("The provided data did not have a BLP signature."); } } } }
/// <summary> /// Initializes a new instance of the <see cref="Warcraft.BLP.BLPHeader"/> class. /// This constructor creates a header from input data read from a BLP file. /// Usually, this is 148 bytes. /// </summary> /// <param name="InData">Data.</param> public BLPHeader(byte[] InData) { using (MemoryStream ms = new MemoryStream(InData)) { using (BinaryReader br = new BinaryReader(ms)) { this.Signature = new string(br.ReadChars(4)); if (Enum.TryParse(this.Signature, out this.Format)) { if (this.Format == BLPFormat.BLP2) { this.Version = (uint)br.ReadInt32(); } if (this.Format == BLPFormat.BLP2) { this.CompressionType = (TextureCompressionType)br.ReadByte(); } else { this.CompressionType = (TextureCompressionType)br.ReadUInt32(); } if (this.Format == BLPFormat.BLP2) { this.AlphaBitDepth = br.ReadByte(); } else { this.AlphaBitDepth = br.ReadUInt32(); } // BLP0 & BLP1 stores the resolution here if (this.Format < BLPFormat.BLP2) { this.Resolution = new Resolution(br.ReadUInt32(), br.ReadUInt32()); } if (this.Format == BLPFormat.BLP2) { this.PixelFormat = (BLPPixelFormat)br.ReadByte(); } else { this.PixelFormat = (BLPPixelFormat)br.ReadUInt32(); } if (this.Format == BLPFormat.BLP2) { this.MipMapType = br.ReadByte(); } else { this.MipMapType = br.ReadUInt32(); } // BLP2 stores the resolution here if (this.Format == BLPFormat.BLP2) { this.Resolution = new Resolution(br.ReadUInt32(), br.ReadUInt32()); } this.MipMapOffsets = new List<uint>(); for (int i = 0; i < 16; ++i) { uint offset = br.ReadUInt32(); if (offset > 0) { this.MipMapOffsets.Add(offset); } } this.MipMapSizes = new List<uint>(); for (int i = 0; i < 16; ++i) { uint size = br.ReadUInt32(); if (size > 0) { this.MipMapSizes.Add(size); } } } else { throw new FileLoadException("The provided data did not have a BLP signature."); } } } }