kiwidog

CTexture

Dec 18th, 2012
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using EndianIO;
  8. using zlib;
  9. namespace IceEditor.Editor
  10. {
  11.     class CTexture
  12.     {
  13.         enum D3DFORMAT : uint
  14.         {
  15.             D3DFMT_UNKNOWN              =  0,
  16.  
  17.             D3DFMT_R8G8B8               = 20,
  18.             D3DFMT_A8R8G8B8             = 21,
  19.             D3DFMT_X8R8G8B8             = 22,
  20.             D3DFMT_R5G6B5               = 23,
  21.             D3DFMT_X1R5G5B5             = 24,
  22.             D3DFMT_A1R5G5B5             = 25,
  23.             D3DFMT_A4R4G4B4             = 26,
  24.             D3DFMT_R3G3B2               = 27,
  25.             D3DFMT_A8                   = 28,
  26.             D3DFMT_A8R3G3B2             = 29,
  27.             D3DFMT_X4R4G4B4             = 30,
  28.             D3DFMT_A2B10G10R10          = 31,
  29.             D3DFMT_A8B8G8R8             = 32,
  30.             D3DFMT_X8B8G8R8             = 33,
  31.             D3DFMT_G16R16               = 34,
  32.             D3DFMT_A2R10G10B10          = 35,
  33.             D3DFMT_A16B16G16R16         = 36,
  34.  
  35.             D3DFMT_A8P8                 = 40,
  36.             D3DFMT_P8                   = 41,
  37.  
  38.             D3DFMT_L8                   = 50,
  39.             D3DFMT_A8L8                 = 51,
  40.             D3DFMT_A4L4                 = 52,
  41.  
  42.             D3DFMT_V8U8                 = 60,
  43.             D3DFMT_L6V5U5               = 61,
  44.             D3DFMT_X8L8V8U8             = 62,
  45.             D3DFMT_Q8W8V8U8             = 63,
  46.             D3DFMT_V16U16               = 64,
  47.             D3DFMT_A2W10V10U10          = 67,
  48.  
  49.             D3DFMT_UYVY                 = 0x59565955,
  50.             D3DFMT_R8G8_B8G8            = 0x47424752,
  51.             D3DFMT_YUY2                 = 0x32595559,
  52.             D3DFMT_G8R8_G8B8            = 0x42475247,
  53.             D3DFMT_DXT1                 = 0x31545844,
  54.             D3DFMT_DXT2                 = 0x32545844,
  55.             D3DFMT_DXT3                 = 0x33545844,
  56.             D3DFMT_DXT4                 = 0x34545844,
  57.             D3DFMT_DXT5                 = 0x35545844,
  58.  
  59.             D3DFMT_D16_LOCKABLE         = 70,
  60.             D3DFMT_D32                  = 71,
  61.             D3DFMT_D15S1                = 73,
  62.             D3DFMT_D24S8                = 75,
  63.             D3DFMT_D24X8                = 77,
  64.             D3DFMT_D24X4S4              = 79,
  65.             D3DFMT_D16                  = 80,
  66.  
  67.             D3DFMT_D32F_LOCKABLE        = 82,
  68.             D3DFMT_D24FS8               = 83,
  69.  
  70.             D3DFMT_D32_LOCKABLE         = 84,
  71.             D3DFMT_S8_LOCKABLE          = 85,
  72.  
  73.             D3DFMT_L16                  = 81,
  74.  
  75.             D3DFMT_VERTEXDATA           =100,
  76.             D3DFMT_INDEX16              =101,
  77.             D3DFMT_INDEX32              =102,
  78.  
  79.             D3DFMT_Q16W16V16U16         =110,
  80.  
  81.             D3DFMT_MULTI2_ARGB8         = 0x3154454D,
  82.  
  83.             D3DFMT_R16F                 = 111,
  84.             D3DFMT_G16R16F              = 112,
  85.             D3DFMT_A16B16G16R16F        = 113,
  86.  
  87.             D3DFMT_R32F                 = 114,
  88.             D3DFMT_G32R32F              = 115,
  89.             D3DFMT_A32B32G32R32F        = 116,
  90.  
  91.             D3DFMT_CxV8U8               = 117,
  92.  
  93.             D3DFMT_A1                   = 118,
  94.             D3DFMT_A2B10G10R10_XR_BIAS  = 119,
  95.             D3DFMT_BINARYBUFFER         = 199,
  96.  
  97.             D3DFMT_FORCE_DWORD          =0x7fffffff
  98.         }
  99.         enum DDSFLAGS : uint
  100.         {
  101.             DDSD_CAPS = 0x1,
  102.             DDSD_HEIGHT = 0x2,
  103.             DDSD_WIDTH = 0x4,
  104.             DDSD_PITCH = 0x8,
  105.             DDSD_PIXELFORMAT = 0x1000,
  106.             DDSD_MIPMAPCOUNT = 0x20000,
  107.             DDSD_LINEARSIZE = 0x80000,
  108.             DDSD_DEPTH = 0x800000
  109.         }
  110.  
  111.         enum DDSCAPS : uint
  112.         {
  113.             DDSCAPS_COMPLEX = 0x8,
  114.             DDSCAPS_MIPMAP = 0x400000,
  115.             DDSCAPS_TEXTURE = 0x1000 // Required
  116.         }
  117.  
  118.         enum DDSCAPS2 : uint
  119.         {
  120.             DDSCAPS2_CUBEMAP = 0x200,
  121.             DDSCAPS2_CUBEMAP_POSITIVEX = 0x400,
  122.             DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800,
  123.             DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000,
  124.             DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000,
  125.             DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000,
  126.             DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000,
  127.             DDSCAPS2_VOLUME = 0x200000
  128.         }
  129.  
  130.         enum DDSPXLFMTFLAGS : uint
  131.         {
  132.             DDPF_ALPHAPIXELS = 0x1,
  133.             DDPF_ALPHA = 0x2,
  134.             DDPF_FOURCC = 0x4,
  135.             DDPF_RGB = 0x40,
  136.             DDPF_YUV = 0x200,
  137.             DDPF_LUMINANCE = 0x20000
  138.         }
  139.  
  140.         class DDSPixelFormat
  141.         {
  142.             public uint m_size;
  143.             public DDSPXLFMTFLAGS m_flags;
  144.             public D3DFORMAT m_fourCC; // DXT1
  145.             public uint m_rgbBitCount;
  146.             public uint m_rBitMask; // Red
  147.             public uint m_gBitMask; // Green
  148.             public uint m_bBitMask; // Blue
  149.             public uint m_aBitMask; // Alpha
  150.  
  151.             public DDSPixelFormat()
  152.             {
  153.                 m_size = 32;
  154.             }
  155.         }
  156.  
  157.         class DDSHeader
  158.         {
  159.             public uint m_magic; // "DDS " or 0x44445320
  160.             public uint m_size; // Always 124
  161.             public uint m_flags; //
  162.             public uint m_height;
  163.             public uint m_width;
  164.             public uint m_pitchOrLinearSize; // Big Endian
  165.             public uint m_depth; // 0?
  166.             public uint m_mipMapCount; // 1?
  167.             public byte[] m_reserved; // Size=0x2C, its unused so we can put text here
  168.             public DDSPixelFormat m_fmt;
  169.             public uint m_caps;
  170.             public uint m_caps2;
  171.             public uint m_caps3;
  172.             public uint m_caps4;
  173.             public uint m_reserved2; // Unused
  174.  
  175.             public DDSHeader()
  176.             {
  177.                 m_magic = 0x20534444; // "DDS "
  178.                 m_size = 124;
  179.                 m_reserved2 = 0;
  180.                 m_fmt = new DDSPixelFormat();
  181.             }
  182.         }
  183.  
  184.         class ImageChunk
  185.         {
  186.             public uint m_uncompressedSize; // Non Compressed Size
  187.             public uint m_compressedSize;
  188.             public byte[] m_data;
  189.  
  190.             public ImageChunk(Stream stream)
  191.             {
  192.                 EndianReader br = new EndianReader(stream, EndianType.BigEndian);
  193.                 m_uncompressedSize = br.ReadUInt32();
  194.                 m_compressedSize = br.ReadUInt32();
  195.                 m_data = br.ReadBytes((int)m_compressedSize);
  196.             }
  197.         }
  198.  
  199.         List<ImageChunk> m_chunks = new List<ImageChunk>();
  200.  
  201.         /// <summary>
  202.         /// Reads out of a chunk file, uses the resource data to generate a working DDS file
  203.         /// Assumes that the only type used in Frostbite 2 is DXT1
  204.         /// </summary>
  205.         /// <param name="chunkFile">File path to the chunk file (resource.id chunk)</param>
  206.         /// <param name="outputFile">File path to the final output .dds file</param>
  207.         public void GenerateDDSFile(string chunkFile, string outputFile, ITexture info)
  208.         {
  209.             EndianReader br = new EndianReader(new FileStream(chunkFile, FileMode.Open, FileAccess.Read), EndianType.BigEndian);
  210.             while (br.BaseStream.Position < br.BaseStream.Length)
  211.             {
  212.                 m_chunks.Add(new ImageChunk(br.BaseStream));
  213.             }
  214.             br.Close();
  215.  
  216.             BinaryWriter bw = new BinaryWriter(new FileStream(outputFile, FileMode.Create, FileAccess.Write));
  217.             // Generate the Header and PixelFormatData
  218.             DDSHeader dds = new DDSHeader();
  219.             dds.m_flags = (uint)(DDSFLAGS.DDSD_CAPS | DDSFLAGS.DDSD_HEIGHT | DDSFLAGS.DDSD_WIDTH | DDSFLAGS.DDSD_PIXELFORMAT); // Default, others can be added later
  220.             dds.m_height = info.m_height;
  221.             dds.m_width = info.m_width;
  222.             dds.m_pitchOrLinearSize = info.m_pitch;
  223.             dds.m_depth = info.m_depth;
  224.             dds.m_mipMapCount = 15;
  225.             dds.m_reserved = new byte[4*11];
  226.             dds.m_fmt.m_flags = DDSPXLFMTFLAGS.DDPF_FOURCC;
  227.  
  228.             // Texture Specific Settings
  229.             switch ((TextureFormat)info.m_format)
  230.             {
  231.                 case TextureFormat.TextureFormat_DXT1:
  232.                     dds.m_fmt.m_fourCC = D3DFORMAT.D3DFMT_DXT1;
  233.                     break;
  234.                 case TextureFormat.TextureFormat_DXT3:
  235.                     dds.m_fmt.m_fourCC = D3DFORMAT.D3DFMT_DXT3;
  236.                     break;
  237.                 case TextureFormat.TextureFormat_DXT5:
  238.                     dds.m_fmt.m_fourCC = D3DFORMAT.D3DFMT_DXT5;
  239.                    
  240.                     break;
  241.                 default:
  242.                     break;
  243.             }
  244.             // General Texture Settings
  245.             switch ((TextureFormat)info.m_format)
  246.             {
  247.                 case TextureFormat.TextureFormat_DXT1:
  248.                 case TextureFormat.TextureFormat_DXT3:
  249.                 case TextureFormat.TextureFormat_DXT5:
  250.                     dds.m_fmt.m_flags |= DDSPXLFMTFLAGS.DDPF_RGB;
  251.                     dds.m_fmt.m_rgbBitCount = 32;
  252.                     dds.m_fmt.m_rBitMask = 0x000000FF;
  253.                     dds.m_fmt.m_gBitMask = 0x0000FF00;
  254.                     dds.m_fmt.m_bBitMask = 0x00FF0000;
  255.                     dds.m_fmt.m_aBitMask = 0xFF000000;
  256.                     break;
  257.                 default:
  258.                     break;
  259.             }            
  260.  
  261.             dds.m_caps = (uint)DDSCAPS.DDSCAPS_TEXTURE;
  262.             dds.m_caps2 = 0;
  263.             dds.m_caps3 = 0;
  264.             dds.m_caps4 = 0;
  265.  
  266.             bw.Write(dds.m_magic);
  267.             bw.Write(dds.m_size);
  268.             bw.Write(dds.m_flags);
  269.             bw.Write(dds.m_height);
  270.             bw.Write(dds.m_width);
  271.             bw.Write(dds.m_pitchOrLinearSize);
  272.             bw.Write(dds.m_depth);
  273.             bw.Write(dds.m_mipMapCount);
  274.             bw.Write(dds.m_reserved);
  275.             bw.Write(dds.m_fmt.m_size);
  276.             bw.Write((uint)dds.m_fmt.m_flags);
  277.             bw.Write((uint)dds.m_fmt.m_fourCC);
  278.             bw.Write(dds.m_fmt.m_rgbBitCount);
  279.             bw.Write(dds.m_fmt.m_rBitMask);
  280.             bw.Write(dds.m_fmt.m_gBitMask);
  281.             bw.Write(dds.m_fmt.m_bBitMask);
  282.             bw.Write(dds.m_fmt.m_aBitMask);
  283.             bw.Write(dds.m_caps);
  284.             bw.Write(dds.m_caps2);
  285.             bw.Write(dds.m_caps3);
  286.             bw.Write(dds.m_caps4);
  287.             bw.Write(dds.m_reserved2);
  288.             // Loop through all the chunks
  289.             foreach (ImageChunk img in m_chunks)
  290.             {
  291.                 if (img.m_compressedSize == img.m_uncompressedSize)
  292.                     bw.Write(img.m_data);
  293.                 else
  294.                 {
  295.                     Stream m_final = new MemoryStream();
  296.                     ZOutputStream outStream = new ZOutputStream(m_final);
  297.                     MemoryStream compressedData = new MemoryStream(img.m_data);
  298.                     try
  299.                     {
  300.                         CResource.CopyStream(compressedData, outStream);
  301.                     }
  302.                     finally
  303.                     {
  304.                         compressedData.Close();
  305.                         //outStream.Close();
  306.                     }
  307.                     m_final.Position = 0;
  308.                     BinaryReader brx = new BinaryReader(m_final);
  309.                     bw.Write(brx.ReadBytes((int)brx.BaseStream.Length));
  310.                     brx.Close();
  311.                 }
  312.             }
  313.             bw.Close();
  314.         }
  315.     }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment