Example #1
0
        public IList <ImageInfo> Load(Stream input)
        {
            using var br = new BinaryReaderX(_file = input, true);

            // Read header
            _header = br.ReadType <BchHeader>();

            if (_header.dataSize == 0)
            {
                return(Array.Empty <ImageInfo>());
            }

            // Read PICA commands
            _picaReaders = new List <PICACommandReader>();
            var gpuStream = new SubStream(input, _header.gpuCommandsOffset, _header.gpuCommandsSize);

            while (gpuStream.Position < gpuStream.Length)
            {
                _picaReaders.Add(new PICACommandReader(gpuStream));
            }

            // Add images
            var result = new List <ImageInfo>();

            foreach (var picaReader in _picaReaders)
            {
                var size = picaReader.getTexUnit0Size();
                if (size.Width == 0 || size.Height == 0)
                {
                    continue;
                }

                // Read main image
                var format   = (int)picaReader.getTexUnit0Format();
                var bitDepth = BchSupport.GetEncodingDefinition().GetColorEncoding(format).BitDepth;

                input.Position = _header.dataOffset + picaReader.getTexUnit0Address();

                var dataLength = size.Width * size.Height * bitDepth / 8;
                var imageData  = br.ReadBytes(dataLength);

                // Read mip maps
                var mipCount = picaReader.getTexUnit0LoD();
                var mipMaps  = new byte[mipCount][];
                for (var i = 1; i <= mipCount; i++)
                {
                    mipMaps[i - 1] = br.ReadBytes((size.Width >> i) * (size.Height >> i) * bitDepth / 8);
                }

                result.Add(mipCount > 0
                    ? new ImageInfo(imageData, mipMaps, format, size)
                    : new ImageInfo(imageData, format, size));
                result[^ 1].RemapPixels.With(context => new CtrSwizzle(context));
Example #2
0
 public BchState()
 {
     _bch = new Bch();
     EncodingDefinition = BchSupport.GetEncodingDefinition();
 }