/// <summary>
        /// Decodes the texture from the specified stream.
        /// </summary>
        /// <param name="stream">The stream, where the texture should be decoded from. Cannot be null.</param>
        /// <returns>The decoded image.</returns>
        public Texture DecodeTexture(Stream stream)
        {
            this.ReadFileHeader(stream);

            if (this.ktxHeader.Width == 0)
            {
                throw new UnknownTextureFormatException("Width cannot be 0");
            }

            int width = (int)this.ktxHeader.Width;
            int height = (int)this.ktxHeader.Height;

            // Skip over bytesOfKeyValueData, if any is present.
            stream.Position += this.ktxHeader.BytesOfKeyValueData;

            var ktxProcessor = new KtxProcessor(this.ktxHeader);

            if (this.ktxHeader.NumberOfFaces == 6)
            {
                CubemapTexture cubeMapTexture = ktxProcessor.DecodeCubeMap(stream, width, height);
                return cubeMapTexture;
            }

            var texture = new FlatTexture();
            MipMap[] mipMaps = ktxProcessor.DecodeMipMaps(stream, width, height, this.ktxHeader.NumberOfMipmapLevels);
            texture.MipMaps.AddRange(mipMaps);
            return texture;
        }
Beispiel #2
0
        /// <summary>
        /// Decodes the texture from the specified stream.
        /// </summary>
        /// <param name="stream">The stream, where the texture should be decoded from. Cannot be null.</param>
        /// <returns>The decoded image.</returns>
        public Texture DecodeTexture(Stream stream)
        {
            this.ReadFileHeader(stream);

            if (this.ktxHeader.PixelWidth == 0)
            {
                throw new UnknownTextureFormatException("Width cannot be 0");
            }

            int width  = (int)this.ktxHeader.PixelWidth;
            int height = (int)this.ktxHeader.PixelHeight;

            var levelIndices = new LevelIndex[this.ktxHeader.LevelCount];

            for (int i = 0; i < levelIndices.Length; i++)
            {
                stream.Read(this.buffer, 0, 24);
                LevelIndex levelIndex = MemoryMarshal.Cast <byte, LevelIndex>(this.buffer)[0];
                levelIndices[i] = levelIndex;
            }

            if (this.ktxHeader.SupercompressionScheme != 0)
            {
                throw new NotSupportedException("SupercompressionSchemes are not yet supported");
            }

            var ktxProcessor = new Ktx2Processor(this.ktxHeader);

            if (this.ktxHeader.FaceCount == 6)
            {
                CubemapTexture cubeMapTexture = ktxProcessor.DecodeCubeMap(stream, width, height, levelIndices);
                return(cubeMapTexture);
            }

            var texture = new FlatTexture();

            MipMap[] mipMaps = ktxProcessor.DecodeMipMaps(stream, width, height, levelIndices);
            texture.MipMaps.AddRange(mipMaps);

            return(texture);
        }
Beispiel #3
0
        /// <summary>
        /// Decodes the texture from the specified stream.
        /// </summary>
        /// <param name="stream">The stream, where the texture should be decoded from. Cannot be null.</param>
        /// <returns>The decoded image.</returns>
        public Texture DecodeTexture(Stream stream)
        {
            try
            {
                this.ReadFileHeader(stream);

                if (this.ddsHeader.Width == 0 || this.ddsHeader.Height == 0)
                {
                    throw new UnknownTextureFormatException("Width or height cannot be 0");
                }

                var ddsProcessor = new DdsProcessor(this.ddsHeader, this.ddsDxt10header);

                int width  = (int)this.ddsHeader.Width;
                int height = (int)this.ddsHeader.Height;
                int count  = this.ddsHeader.TextureCount();

                if (this.ddsHeader.IsVolumeTexture())
                {
                    int depths = this.ddsHeader.ComputeDepth();

                    var texture  = new VolumeTexture();
                    var surfaces = new FlatTexture[depths];

                    for (int i = 0; i < count; i++)
                    {
                        for (int depth = 0; depth < depths; depth++)
                        {
                            if (i == 0)
                            {
                                surfaces[depth] = new FlatTexture();
                            }

                            MipMap[] mipMaps = ddsProcessor.DecodeDds(stream, width, height, 1);
                            surfaces[depth].MipMaps.AddRange(mipMaps);
                        }

                        depths >>= 1;
                        width  >>= 1;
                        height >>= 1;
                    }

                    texture.Slices.AddRange(surfaces);
                    return(texture);
                }
                else if (this.ddsHeader.IsCubemap())
                {
                    DdsSurfaceType[] faces = this.ddsHeader.GetExistingCubemapFaces();

                    var texture = new CubemapTexture();
                    for (int face = 0; face < faces.Length; face++)
                    {
                        MipMap[] mipMaps = ddsProcessor.DecodeDds(stream, width, height, count);
                        if (faces[face] == DdsSurfaceType.CubemapPositiveX)
                        {
                            texture.PositiveX.MipMaps.AddRange(mipMaps);
                        }

                        if (faces[face] == DdsSurfaceType.CubemapNegativeX)
                        {
                            texture.NegativeX.MipMaps.AddRange(mipMaps);
                        }

                        if (faces[face] == DdsSurfaceType.CubemapPositiveY)
                        {
                            texture.PositiveY.MipMaps.AddRange(mipMaps);
                        }

                        if (faces[face] == DdsSurfaceType.CubemapNegativeY)
                        {
                            texture.NegativeY.MipMaps.AddRange(mipMaps);
                        }

                        if (faces[face] == DdsSurfaceType.CubemapPositiveZ)
                        {
                            texture.PositiveZ.MipMaps.AddRange(mipMaps);
                        }

                        if (faces[face] == DdsSurfaceType.CubemapNegativeZ)
                        {
                            texture.NegativeZ.MipMaps.AddRange(mipMaps);
                        }
                    }

                    return(texture);
                }
                else
                {
                    var      texture = new FlatTexture();
                    MipMap[] mipMaps = ddsProcessor.DecodeDds(stream, width, height, count);
                    texture.MipMaps.AddRange(mipMaps);
                    return(texture);
                }
            }
            catch (IndexOutOfRangeException e)
            {
                throw new TextureFormatException("Dds image does not have a valid format.", e);
            }
        }
 public void Clear(Vector4d value, FlatTexture target, int level = 0)
 {
     Clear(value, target.Levels[level]);
 }
Beispiel #5
0
 public void Clear(Vector4d value, FlatTexture target, int level = 0)
 {
     Clear(value, target.Levels[level]);
 }