Exemplo n.º 1
0
 /// <summary>Save DDS to stream</summary>
 /// <param name="stream">Stream to be written to</param>
 /// <param name="keepOpen">Keep the stream open after writing</param>
 public void SaveToDDS(Stream stream, bool keepOpen = false)
 {
     if (PayloadRequired)
     {
         if (Payload == null)
         {
             throw new Exceptions.TexturePayloadMissingException();
         }
         Payload.SaveToDDS(Header, stream, keepOpen);
     }
     else
     {
         using (BinaryWriter ddsWriter = new BinaryWriter(stream, Encoding.Default, keepOpen)) {
             TextureTypes.DDSHeader dds = Header.ToDDSHeader();
             ddsWriter.Write(dds);
             if (dds.Format.FourCC == 0x30315844)
             {
                 TextureTypes.DDS_HEADER_DXT10 d10 = new TextureTypes.DDS_HEADER_DXT10 {
                     Format    = Header.Format,
                     Dimension = TextureTypes.D3D10_RESOURCE_DIMENSION.TEXTURE2D,
                     Misc      = (uint)(Header.IsCubemap() ? 0x4 : 0),
                     Size      = (uint)(Header.IsCubemap() ? 1 : Header.Surfaces),
                     Misc2     = 0
                 };
                 ddsWriter.Write(d10);
             }
             ddsWriter.Write(Data, 0, (int)Header.DataSize);
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>Save DDS to stream</summary>
        /// <param name="stream">Stream to be written to</param>
        /// <param name="keepOpen">Keep the stream open after writing</param>
        /// <param name="mips"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="surfaces"></param>
        public void SaveToDDS(Stream stream, bool keepOpen, int?mips, uint?width = null, uint?height = null, uint?surfaces = null)
        {
            if (PayloadRequired && Payloads[0] == null)
            {
                throw new Exceptions.TexturePayloadMissingException();
            }
            using (BinaryWriter ddsWriter = new BinaryWriter(stream, Encoding.Default, keepOpen)) {
                TextureTypes.DDSHeader dds = Header.ToDDSHeader(mips ?? Header.MipCount, width ?? Header.Width, height ?? Header.Height, surfaces ?? Header.Surfaces);
                ddsWriter.Write(dds);
                if (dds.Format.FourCC == 0x30315844)
                {
                    var dimension = TextureTypes.D3D10_RESOURCE_DIMENSION.UNKNOWN;
                    if (Header.HasFlag(Flags.Tex1D))
                    {
                        dimension = TextureTypes.D3D10_RESOURCE_DIMENSION.TEXTURE1D;
                    }
                    else if (Header.HasFlag(Flags.Tex2D))
                    {
                        dimension = TextureTypes.D3D10_RESOURCE_DIMENSION.TEXTURE2D;
                    }
                    else if (Header.HasFlag(Flags.Tex3D))
                    {
                        dimension = TextureTypes.D3D10_RESOURCE_DIMENSION.TEXTURE3D;
                    }
                    else if (Header.HasFlag(Flags.Cube))
                    {
                        // cubemaps are just 2d textures
                        dimension = TextureTypes.D3D10_RESOURCE_DIMENSION.TEXTURE2D;
                    }

                    TextureTypes.DDS_HEADER_DXT10 d10 = new TextureTypes.DDS_HEADER_DXT10 {
                        Format    = Header.Format,
                        Dimension = dimension,
                        Misc      = (uint)(Header.IsCubemap ? 0x4 : 0), // 4 = D3D11_RESOURCE_MISC_TEXTURECUBE
                        Size      = surfaces ?? (Header.IsCubemap ? Header.Surfaces / 6u : Header.Surfaces),
                    };
                    ddsWriter.Write(d10);
                }

                if (PayloadRequired)
                {
                    foreach (var payload in Payloads.Where(x => x != null))
                    {
                        payload.SaveToDDSData(Header, ddsWriter);
                    }
                }
                else
                {
                    ddsWriter.Write(Data, 0, (int)Header.DataSize);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>Save DDS to stream</summary>
        /// <param name="parentHeader">Parent teTexture header</param>
        /// <param name="stream">Stream to be written to</param>
        /// <param name="keepOpen">Keep the stream open after writing</param>
        public void SaveToDDS(teTexture.TextureHeader parentHeader, Stream stream, bool keepOpen = false)
        {
            using (BinaryWriter ddsWriter = new BinaryWriter(stream, Encoding.Default, keepOpen)) {
                TextureTypes.DDSHeader dds = parentHeader.ToDDSHeader();
                ddsWriter.Write(dds);
                if (dds.Format.FourCC == (int)TextureTypes.TextureType.Unknown)
                {
                    TextureTypes.DDS_HEADER_DXT10 d10 = new TextureTypes.DDS_HEADER_DXT10 {
                        Format    = parentHeader.Format,
                        Dimension = TextureTypes.D3D10_RESOURCE_DIMENSION.TEXTURE2D,
                        Misc      = (uint)(parentHeader.IsCubemap() ? 0x4 : 0),
                        Size      = (uint)(parentHeader.IsCubemap() ? 1 : parentHeader.Surfaces),
                        Misc2     = 0
                    };
                    ddsWriter.Write(d10);
                }
                if (RawData != null)
                {
                    stream.Write(RawData, 0, (int)Header.ImageSize);
                    return;
                }
                for (int i = 0; i < Size; ++i)
                {
                    if (parentHeader.Format > 72)
                    {
                        ddsWriter.Write(Color3[i]);
                        ddsWriter.Write(Color4[i]);
                        ddsWriter.Write(Color5[i]);
                    }

                    if (parentHeader.Format < 80)
                    {
                        ddsWriter.Write(Color1[i]);
                        ddsWriter.Write(Color2[i]);
                    }
                }
            }
        }
Exemplo n.º 4
0
            public ulong         Unk24;         // 24

            public TextureTypes.DDSHeader ToDDSHeader(int mips, uint width, uint height, uint surfaces)
            {
                TextureTypes.DDSHeader ret = new TextureTypes.DDSHeader {
                    Magic       = 0x20534444,
                    Size        = 124,
                    Flags       = 0x1 | 0x2 | 0x4 | 0x1000 | 0x20000,
                    Height      = height,
                    Width       = width,
                    LinearSize  = 0,
                    Depth       = 0,
                    MipmapCount = (uint)mips,
                    Format      = GetTextureType().ToPixelFormat(),
                    Caps1       = 0x1000,
                    Caps2       = 0,
                    Caps3       = 0,
                    Caps4       = 0,
                    Reserved2   = 0
                };
                if (surfaces > 1 || IsArray)
                {
                    ret.Caps1  = 0x8 | 0x1000;
                    ret.Format = TextureTypes.TextureType.Unknown.ToPixelFormat();
                }

                if (IsCubemap)
                {
                    ret.Caps2 = 0xFE00;
                }

                // todo: wtf
                if (MipCount > 1 && (PayloadCount == 1 || IsCubemap))
                {
                    ret.MipmapCount = MipCount;
                    ret.Caps1       = 0x8 | 0x1000 | 0x400000;
                }

                return(ret);
            }
Exemplo n.º 5
0
            public TextureTypes.DDSHeader ToDDSHeader()
            {
                TextureTypes.DDSHeader ret = new TextureTypes.DDSHeader {
                    Magic       = 0x20534444,
                    Size        = 124,
                    Flags       = 0x1 | 0x2 | 0x4 | 0x1000 | 0x20000,
                    Height      = Height,
                    Width       = Width,
                    LinearSize  = 0,
                    Depth       = 0,
                    MipmapCount = 1,
                    Format      = GetTextureType().ToPixelFormat(),
                    Caps1       = 0x1000,
                    Caps2       = 0,
                    Caps3       = 0,
                    Caps4       = 0,
                    Reserved2   = 0
                };
                if (Surfaces > 1)
                {
                    ret.Caps1  = 0x8 | 0x1000;
                    ret.Format = TextureTypes.TextureType.Unknown.ToPixelFormat();
                }

                if (IsCubemap())
                {
                    ret.Caps2 = 0xFE00;
                }

                if (Mips > 1 && (PayloadCount == 1 || IsCubemap()))
                {
                    ret.MipmapCount = Mips;
                    ret.Caps1       = 0x8 | 0x1000 | 0x400000;
                }

                return(ret);
            }