Guest User

Untitled

a guest
Apr 10th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.34 KB | None | 0 0
  1. public class TextureAsset
  2.     {
  3.         public struct DDSPixelFormat
  4.         {
  5.             public int dwSize;
  6.             public int dwFlags;
  7.             public int dwFourCC;
  8.             public int dwRGBBitCount;
  9.             public uint dwRBitMask;
  10.             public uint dwGBitMask;
  11.             public uint dwBBitMask;
  12.             public uint dwABitMask;
  13.             public uint caps2;
  14.         }
  15.  
  16.         public byte[] data;
  17.         public int formatID;
  18.         public ushort width;
  19.         public ushort height;
  20.         public ushort depth;
  21.         public ushort sliceCount;
  22.         public byte firstMip;
  23.         public int mipCount;
  24.         public List<int> mipDataSizes;
  25.         public byte[] chunkid;
  26.         public DDSPixelFormat ddsPF;
  27.  
  28.         public TextureAsset(byte[] buff)
  29.         {
  30.             data = buff;
  31.             ReadData();
  32.         }
  33.  
  34.         private void ReadData()
  35.         {
  36.             MemoryStream m = new MemoryStream(data);
  37.             m.Seek(12, 0);
  38.             formatID = Helpers.ReadInt(m);
  39.             m.Seek(6, SeekOrigin.Current);
  40.             width = Helpers.ReadUShort(m);
  41.             height = Helpers.ReadUShort(m);
  42.             depth = Helpers.ReadUShort(m);
  43.             sliceCount = Helpers.ReadUShort(m);
  44.             mipCount = m.ReadByte();
  45.             firstMip = (byte)m.ReadByte();
  46.             chunkid = new byte[16];
  47.             m.Read(chunkid, 0, 16);
  48.             mipDataSizes = new List<int>();
  49.             for (int i = 0; i < Math.Min(mipCount, 14); i++)
  50.                 mipDataSizes.Add(Helpers.ReadInt(m));
  51.             SetPixelFormatData(formatID);
  52.         }
  53.  
  54.         public static Dictionary<int, int> PixelFormatTypes = new Dictionary<int, int>()
  55.         {
  56.             { 0x00, 0x31545844 },
  57.             { 0x01, 0x31545844 },
  58.             { 0x03, 0x35545844 },
  59.             { 0x04, 0x31495441 },
  60.             { 0x10, 0x74 },
  61.             { 0x13, 0x32495441 },
  62.             { 0x14, 0x53354342 },
  63.             { 0x37, 0x31545844 },
  64.         };
  65.  
  66.         public static int[] KnownFormats = {0x0B, 0x0C, 0x0D };
  67.  
  68.         public bool isKnownFormat()
  69.         {
  70.             if (PixelFormatTypes.ContainsKey(formatID))
  71.                 return true;
  72.             if (KnownFormats.Contains<int>(formatID))
  73.                 return true;
  74.             return false;
  75.         }
  76.  
  77.         private void SetPixelFormatData(int pixelFormatID)
  78.         {
  79.             ddsPF = new DDSPixelFormat();
  80.             ddsPF.caps2 = 0;
  81.             ddsPF.dwSize = 32;
  82.             ddsPF.dwFlags = 4;
  83.             ddsPF.dwFourCC = 0x31545844;
  84.             ddsPF.dwRGBBitCount = 0;
  85.             ddsPF.dwRBitMask = 0;
  86.             ddsPF.dwGBitMask = 0;
  87.             ddsPF.dwBBitMask = 0;
  88.             ddsPF.dwABitMask = 0;
  89.             if (PixelFormatTypes.ContainsKey(pixelFormatID))
  90.             {
  91.                 ddsPF.dwFourCC = PixelFormatTypes[pixelFormatID];
  92.                 if (pixelFormatID == 0x01)
  93.                     ddsPF.dwFlags |= 0x01;
  94.             }
  95.             else
  96.             {
  97.                 switch (pixelFormatID)
  98.                 {
  99.                     case 0x0B:
  100.                         ddsPF.dwFourCC = 0x00;
  101.                         ddsPF.dwRGBBitCount = 0x20;
  102.                         ddsPF.dwRBitMask = 0xFF;
  103.                         ddsPF.dwGBitMask = 0xFF00;
  104.                         ddsPF.dwBBitMask = 0xFF0000;
  105.                         ddsPF.dwABitMask = 0xFF000000;
  106.                         ddsPF.dwFlags = 0x41;
  107.                         if (pixelFormatID == 0x36)
  108.                             ddsPF.caps2 = 0xFE00;
  109.                         break;
  110.                     case 0x0C:
  111.                         ddsPF.dwFourCC = 0x00;
  112.                         ddsPF.dwRGBBitCount = 0x08;
  113.                         ddsPF.dwABitMask = 0xFF;
  114.                         ddsPF.dwFlags = 0x02;
  115.                         break;
  116.                     case 0x0D:
  117.                         ddsPF.dwFourCC = 0x00;
  118.                         ddsPF.dwRGBBitCount = 0x10;
  119.                         ddsPF.dwRBitMask = 0xFFFF;
  120.                         ddsPF.dwFlags = 0x20000;
  121.                         break;
  122.                 }
  123.             }
  124.         }
  125.  
  126.         public void WriteTextureHeader(Stream s)
  127.         {
  128.             Helpers.WriteInt(s, 0x20534444);
  129.             Helpers.WriteInt(s, 124);
  130.             Helpers.WriteInt(s, 0x000A100F);
  131.             int factor = (int)Math.Pow(2, firstMip);
  132.             Helpers.WriteInt(s, height / factor);
  133.             Helpers.WriteInt(s, width / factor);
  134.             Helpers.WriteInt(s, mipDataSizes[0]);
  135.             Helpers.WriteInt(s, depth);
  136.             Helpers.WriteInt(s, mipCount - firstMip);
  137.             for (int i = 0; i < 11; i++)
  138.                 Helpers.WriteInt(s, 0);
  139.             Helpers.WriteInt(s, ddsPF.dwSize);
  140.             Helpers.WriteInt(s, ddsPF.dwFlags);
  141.             Helpers.WriteInt(s, ddsPF.dwFourCC);
  142.             Helpers.WriteInt(s, ddsPF.dwRGBBitCount);
  143.             Helpers.WriteInt(s, (int)ddsPF.dwRBitMask);
  144.             Helpers.WriteInt(s, (int)ddsPF.dwGBitMask);
  145.             Helpers.WriteInt(s, (int)ddsPF.dwBBitMask);
  146.             Helpers.WriteInt(s, (int)ddsPF.dwABitMask);
  147.             Helpers.WriteInt(s, 0);
  148.             Helpers.WriteInt(s, (int)ddsPF.caps2);
  149.             Helpers.WriteInt(s, 0);
  150.             Helpers.WriteInt(s, 0);
  151.             Helpers.WriteInt(s, 0);
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment