Guest User

Untitled

a guest
Apr 11th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. public class TextureAsset
  2.     {
  3.         public int formatID;
  4.         public ushort width;
  5.         public ushort height;
  6.         public ushort depth;
  7.         public ushort sliceCount;
  8.         public byte firstMip;
  9.         public int mipCount;
  10.         public List<int> mipDataSizes;
  11.         public byte[] chunkid;
  12.         public byte[] data;
  13.  
  14.         public TextureAsset(byte[] buff)
  15.         {
  16.             data = buff;
  17.             ReadData();
  18.         }
  19.  
  20.         private void ReadData()
  21.         {
  22.             MemoryStream m = new MemoryStream(data);
  23.             m.Seek(12, 0);
  24.             formatID = Helpers.ReadInt(m);
  25.             m.Seek(6, SeekOrigin.Current);
  26.             width = Helpers.ReadUShort(m);
  27.             height = Helpers.ReadUShort(m);
  28.             depth = Helpers.ReadUShort(m);
  29.             sliceCount = Helpers.ReadUShort(m);
  30.             mipCount = m.ReadByte();
  31.             firstMip = (byte)m.ReadByte();
  32.             chunkid = new byte[16];
  33.             m.Read(chunkid, 0, 16);
  34.             mipDataSizes = new List<int>();
  35.             for (int i = 0; i < Math.Min(mipCount, 14); i++)
  36.                 mipDataSizes.Add(Helpers.ReadInt(m));
  37.         }
  38.  
  39.         public static int[] KnownFormats = {0x36, 0x37};
  40.  
  41.         public bool isKnownFormat()
  42.         {
  43.             if (KnownFormats.Contains<int>(formatID))
  44.                 return true;
  45.             return false;
  46.         }
  47.  
  48.         public void WriteMainData(Stream s, int headerSize, bool hasPitch, bool compressed, bool usesMips, bool isDepth)
  49.         {
  50.             Helpers.WriteInt(s, 0x20534444);
  51.             Helpers.WriteInt(s, headerSize);
  52.             int flags = 0x00001007;
  53.             if (hasPitch && !compressed)
  54.                 flags |= 0x8;
  55.             if (isDepth)
  56.                 flags |= 0x20000;
  57.             if (hasPitch && compressed)
  58.                 flags |= 0x80000;
  59.             Helpers.WriteInt(s, flags);
  60.             int factor = (int)Math.Pow(2, firstMip);
  61.             Helpers.WriteInt(s, height / factor);
  62.             Helpers.WriteInt(s, width / factor);
  63.             Helpers.WriteInt(s, mipDataSizes[0]);
  64.             Helpers.WriteInt(s, depth);
  65.             Helpers.WriteInt(s, mipCount - firstMip);
  66.             for (int i = 0; i < 11; i++)
  67.                 Helpers.WriteInt(s, 0);
  68.         }
  69.  
  70.         public int MakePixelFlags(bool alpha, bool fourCC, bool rgb, bool luminance, bool bump)
  71.         {
  72.             int flags = 0;
  73.             if (alpha)      flags |= 0x2;
  74.             if (fourCC)     flags |= 0x4;
  75.             if (rgb)        flags |= 0x40;
  76.             if (luminance)  flags |= 0x20000;
  77.             if (bump)       flags |= 0x80000;
  78.             return flags;
  79.         }
  80.  
  81.         public void WritePixelFormat(Stream s, int size, int flags, int fourCC, int bitCount, int rMask, int gMask, int bMask, int aMask)
  82.         {
  83.             Helpers.WriteInt(s, size);
  84.             Helpers.WriteInt(s, flags);
  85.             Helpers.WriteInt(s, fourCC);
  86.             Helpers.WriteInt(s, bitCount);
  87.             Helpers.WriteInt(s, rMask);
  88.             Helpers.WriteInt(s, gMask);
  89.             Helpers.WriteInt(s, bMask);
  90.             Helpers.WriteInt(s, aMask);
  91.         }
  92.  
  93.         public void WriteDDSHeader(Stream s)
  94.         {
  95.             int flags;
  96.             switch (formatID)
  97.             {
  98.                 case 0x36:
  99.                 case 0x37:
  100.                     WriteMainData(s, 124, true, false, true, false);
  101.                     flags = MakePixelFlags(false, true, false, false, false);
  102.                     WritePixelFormat(s, 32, flags, 0x31545844, 0, 0, 0, 0, 0);
  103.                     break;
  104.             }
  105.             Helpers.WriteInt(s, 0);
  106.             Helpers.WriteInt(s, 0);
  107.             Helpers.WriteInt(s, 0);
  108.             Helpers.WriteInt(s, 0);
  109.             Helpers.WriteInt(s, 0);
  110.         }
  111.     }
Advertisement
Add Comment
Please, Sign In to add comment