示例#1
0
        public ITexImage ReadFrom(
            BinaryReader reader,
            ITexImageContainer container,
            TexFormat texFormat)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            if (!texFormat.IsValid())
            {
                throw new EnumNotValidException <TexFormat>(texFormat);
            }

            var mipmapCount = reader.ReadInt32();

            if (mipmapCount > Constants.MaximumMipmapCount)
            {
                throw new UnsafeTexException(
                          $"Mipmap count exceeds limit: {mipmapCount}/{Constants.MaximumMipmapCount}");
            }

            var readFunction = PickMipmapReader(container.ImageContainerVersion);
            var format       = TexMipmapFormatGetter.GetFormatForTex(container.ImageFormat, texFormat);
            var image        = new TexImage();

            for (var i = 0; i < mipmapCount; i++)
            {
                var mipmap = readFunction(reader);
                mipmap.Format = format;

                if (DecompressMipmapBytes)
                {
                    _texMipmapDecompressor.DecompressMipmap(mipmap);
                }

                image.Mipmaps.Add(mipmap);
            }

            return(image);
        }
示例#2
0
        protected void ReadBytes(BinaryReader reader, TexMipmap mipmap)
        {
            if (!ReadMipmapBytes)
            {
                reader.BaseStream.Seek(mipmap.BytesCount, SeekOrigin.Current);
                return;
            }

            mipmap.Bytes = new byte[mipmap.BytesCount];

            var bytesRead = reader.Read(mipmap.Bytes, 0, mipmap.BytesCount);

            if (bytesRead != mipmap.BytesCount)
            {
                throw new Exception("Failed to read bytes from stream while reading mipmap");
            }

            if (DecompressMipmapBytes)
            {
                _texMipmapDecompressor.DecompressMipmap(mipmap);
            }
        }