Example #1
0
        public static TextureCube DDSFromStreamEXT(
            GraphicsDevice graphicsDevice,
            Stream stream
            )
        {
            TextureCube result;

            // Begin BinaryReader, ignoring a tab!
            using (BinaryReader reader = new BinaryReader(stream))
            {
                int           width, height, levels;
                SurfaceFormat format;
                Texture.ParseDDS(
                    reader,
                    out format,
                    out width,
                    out height,
                    out levels
                    );

                // Allocate/Load texture
                result = new TextureCube(
                    graphicsDevice,
                    width,
                    levels > 1,
                    format
                    );

                byte[] tex = null;
                if (stream is MemoryStream &&
                    ((MemoryStream)stream).TryGetBuffer(out tex))
                {
                    for (int face = 0; face < 6; face += 1)
                    {
                        for (int i = 0; i < levels; i += 1)
                        {
                            int mipLevelSize = Texture.CalculateDDSLevelSize(
                                width >> i,
                                width >> i,
                                format
                                );
                            result.SetData(
                                (CubeMapFace)face,
                                i,
                                null,
                                tex,
                                (int)stream.Seek(0, SeekOrigin.Current),
                                mipLevelSize
                                );
                            stream.Seek(
                                mipLevelSize,
                                SeekOrigin.Current
                                );
                        }
                    }
                }
                else
                {
                    for (int face = 0; face < 6; face += 1)
                    {
                        for (int i = 0; i < levels; i += 1)
                        {
                            tex = reader.ReadBytes(Texture.CalculateDDSLevelSize(
                                                       width >> i,
                                                       width >> i,
                                                       format
                                                       ));
                            result.SetData(
                                (CubeMapFace)face,
                                i,
                                null,
                                tex,
                                0,
                                tex.Length
                                );
                        }
                    }
                }

                // End BinaryReader
            }

            // Finally.
            return(result);
        }
Example #2
0
        public static Texture2D DDSFromStreamEXT(
            Stream stream
            )
        {
            Texture2D result;

            // Begin BinaryReader, ignoring a tab!
            using (BinaryReader reader = new BinaryReader(stream))
            {
                int           width, height, levels, levelSize, blockSize;
                SurfaceFormat format;
                Texture.ParseDDS(
                    reader,
                    out format,
                    out width,
                    out height,
                    out levels,
                    out levelSize,
                    out blockSize
                    );

                // Allocate/Load texture
                result = new Texture2D(
                    width,
                    height,
                    levels > 1,
                    format
                    );

                byte[] tex = null;
                if (stream is MemoryStream &&
                    ((MemoryStream)stream).TryGetBuffer(out tex))
                {
                    for (int i = 0; i < levels; i += 1)
                    {
                        result.SetData(
                            i,
                            null,
                            tex,
                            (int)stream.Seek(0, SeekOrigin.Current),
                            levelSize
                            );
                        stream.Seek(
                            levelSize,
                            SeekOrigin.Current
                            );
                        levelSize = Math.Max(
                            levelSize >> 2,
                            blockSize
                            );
                    }
                }
                else
                {
                    for (int i = 0; i < levels; i += 1)
                    {
                        tex = reader.ReadBytes(levelSize);
                        result.SetData(
                            i,
                            null,
                            tex,
                            0,
                            tex.Length
                            );
                        levelSize = Math.Max(
                            levelSize >> 2,
                            blockSize
                            );
                    }
                }

                // End BinaryReader
            }

            // Finally.
            return(result);
        }
Example #3
0
        public static Texture2D DDSFromStreamEXT(
            GraphicsDevice graphicsDevice,
            Stream stream
            )
        {
            Texture2D result;

            // Begin BinaryReader, ignoring a tab!
            using (BinaryReader reader = new BinaryReader(stream))
            {
                int           width, height, levels;
                bool          isCube;
                SurfaceFormat format;
                Texture.ParseDDS(
                    reader,
                    out format,
                    out width,
                    out height,
                    out levels,
                    out isCube
                    );

                if (isCube)
                {
                    throw new FormatException("This file contains cube map data!");
                }

                // Allocate/Load texture
                result = new Texture2D(
                    graphicsDevice,
                    width,
                    height,
                    levels > 1,
                    format
                    );

                byte[] tex = null;
                if (stream is MemoryStream &&
                    ((MemoryStream)stream).TryGetBuffer(out tex))
                {
                    for (int i = 0; i < levels; i += 1)
                    {
                        int levelSize = Texture.CalculateDDSLevelSize(
                            width >> i,
                            height >> i,
                            format
                            );
                        result.SetData(
                            i,
                            null,
                            tex,
                            (int)stream.Seek(0, SeekOrigin.Current),
                            levelSize
                            );
                        stream.Seek(
                            levelSize,
                            SeekOrigin.Current
                            );
                    }
                }
                else
                {
                    for (int i = 0; i < levels; i += 1)
                    {
                        tex = reader.ReadBytes(Texture.CalculateDDSLevelSize(
                                                   width >> i,
                                                   height >> i,
                                                   format
                                                   ));
                        result.SetData(
                            i,
                            null,
                            tex,
                            0,
                            tex.Length
                            );
                    }
                }

                // End BinaryReader
            }

            // Finally.
            return(result);
        }