Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TextureAsset
- {
- public struct DDSPixelFormat
- {
- public int dwSize;
- public int dwFlags;
- public int dwFourCC;
- public int dwRGBBitCount;
- public uint dwRBitMask;
- public uint dwGBitMask;
- public uint dwBBitMask;
- public uint dwABitMask;
- public uint caps2;
- }
- public byte[] data;
- public int formatID;
- public ushort width;
- public ushort height;
- public ushort depth;
- public ushort sliceCount;
- public byte firstMip;
- public int mipCount;
- public List<int> mipDataSizes;
- public byte[] chunkid;
- public DDSPixelFormat ddsPF;
- public TextureAsset(byte[] buff)
- {
- data = buff;
- ReadData();
- }
- private void ReadData()
- {
- MemoryStream m = new MemoryStream(data);
- m.Seek(12, 0);
- formatID = Helpers.ReadInt(m);
- m.Seek(6, SeekOrigin.Current);
- width = Helpers.ReadUShort(m);
- height = Helpers.ReadUShort(m);
- depth = Helpers.ReadUShort(m);
- sliceCount = Helpers.ReadUShort(m);
- mipCount = m.ReadByte();
- firstMip = (byte)m.ReadByte();
- chunkid = new byte[16];
- m.Read(chunkid, 0, 16);
- mipDataSizes = new List<int>();
- for (int i = 0; i < Math.Min(mipCount, 14); i++)
- mipDataSizes.Add(Helpers.ReadInt(m));
- SetPixelFormatData(formatID);
- }
- public static Dictionary<int, int> PixelFormatTypes = new Dictionary<int, int>()
- {
- { 0x00, 0x31545844 },
- { 0x01, 0x31545844 },
- { 0x03, 0x35545844 },
- { 0x04, 0x31495441 },
- { 0x10, 0x74 },
- { 0x13, 0x32495441 },
- { 0x14, 0x53354342 },
- { 0x37, 0x31545844 },
- };
- public static int[] KnownFormats = {0x0B, 0x0C, 0x0D };
- public bool isKnownFormat()
- {
- if (PixelFormatTypes.ContainsKey(formatID))
- return true;
- if (KnownFormats.Contains<int>(formatID))
- return true;
- return false;
- }
- private void SetPixelFormatData(int pixelFormatID)
- {
- ddsPF = new DDSPixelFormat();
- ddsPF.caps2 = 0;
- ddsPF.dwSize = 32;
- ddsPF.dwFlags = 4;
- ddsPF.dwFourCC = 0x31545844;
- ddsPF.dwRGBBitCount = 0;
- ddsPF.dwRBitMask = 0;
- ddsPF.dwGBitMask = 0;
- ddsPF.dwBBitMask = 0;
- ddsPF.dwABitMask = 0;
- if (PixelFormatTypes.ContainsKey(pixelFormatID))
- {
- ddsPF.dwFourCC = PixelFormatTypes[pixelFormatID];
- if (pixelFormatID == 0x01)
- ddsPF.dwFlags |= 0x01;
- }
- else
- {
- switch (pixelFormatID)
- {
- case 0x0B:
- ddsPF.dwFourCC = 0x00;
- ddsPF.dwRGBBitCount = 0x20;
- ddsPF.dwRBitMask = 0xFF;
- ddsPF.dwGBitMask = 0xFF00;
- ddsPF.dwBBitMask = 0xFF0000;
- ddsPF.dwABitMask = 0xFF000000;
- ddsPF.dwFlags = 0x41;
- if (pixelFormatID == 0x36)
- ddsPF.caps2 = 0xFE00;
- break;
- case 0x0C:
- ddsPF.dwFourCC = 0x00;
- ddsPF.dwRGBBitCount = 0x08;
- ddsPF.dwABitMask = 0xFF;
- ddsPF.dwFlags = 0x02;
- break;
- case 0x0D:
- ddsPF.dwFourCC = 0x00;
- ddsPF.dwRGBBitCount = 0x10;
- ddsPF.dwRBitMask = 0xFFFF;
- ddsPF.dwFlags = 0x20000;
- break;
- }
- }
- }
- public void WriteTextureHeader(Stream s)
- {
- Helpers.WriteInt(s, 0x20534444);
- Helpers.WriteInt(s, 124);
- Helpers.WriteInt(s, 0x000A100F);
- int factor = (int)Math.Pow(2, firstMip);
- Helpers.WriteInt(s, height / factor);
- Helpers.WriteInt(s, width / factor);
- Helpers.WriteInt(s, mipDataSizes[0]);
- Helpers.WriteInt(s, depth);
- Helpers.WriteInt(s, mipCount - firstMip);
- for (int i = 0; i < 11; i++)
- Helpers.WriteInt(s, 0);
- Helpers.WriteInt(s, ddsPF.dwSize);
- Helpers.WriteInt(s, ddsPF.dwFlags);
- Helpers.WriteInt(s, ddsPF.dwFourCC);
- Helpers.WriteInt(s, ddsPF.dwRGBBitCount);
- Helpers.WriteInt(s, (int)ddsPF.dwRBitMask);
- Helpers.WriteInt(s, (int)ddsPF.dwGBitMask);
- Helpers.WriteInt(s, (int)ddsPF.dwBBitMask);
- Helpers.WriteInt(s, (int)ddsPF.dwABitMask);
- Helpers.WriteInt(s, 0);
- Helpers.WriteInt(s, (int)ddsPF.caps2);
- Helpers.WriteInt(s, 0);
- Helpers.WriteInt(s, 0);
- Helpers.WriteInt(s, 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment