public ImageInfo Load(Stream input) { using var br = new BinaryReaderX(input); // Read header _header = br.ReadType <BtxHeader>(); // Read name input.Position = _header.nameOffset; var name = br.ReadCStringASCII(); // Read image data var dataLength = _header.width * _header.height * BtxSupport.GetBitDepth(_header.format) / 8; input.Position = _header.dataOffset; var imgData = br.ReadBytes(dataLength); // Read mip levels IList <byte[]> mips = new List <byte[]>(); for (var i = 0; i < _header.mipLevels; i++) { dataLength = (_header.width << (i + 1)) * (_header.height << (i + 1)) * BtxSupport.GetBitDepth(_header.format) / 8; mips.Add(br.ReadBytes(dataLength)); } // Read palette data var paletteLength = Math.Max(0, (int)input.Length - _header.paletteOffset); input.Position = _header.paletteOffset; var paletteData = br.ReadBytes(paletteLength); // Create image info var imageInfo = new ImageInfo(imgData, _header.format, new Size(_header.width, _header.height)) { Name = name }; if (paletteLength > 0) { imageInfo.PaletteData = paletteData; imageInfo.PaletteFormat = 0; } if (_header.mipLevels > 0) { imageInfo.MipMapData = mips; } switch (_header.swizzleMode) { case 1: imageInfo.RemapPixels.With(context => new VitaSwizzle(context)); break; } return(imageInfo); }
public BtxState() { _btx = new BTX(); EncodingDefinition = BtxSupport.GetEncodingDefinition(); }