Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using EndianIO;
- using zlib;
- namespace IceEditor.Editor
- {
- class CTexture
- {
- enum D3DFORMAT : uint
- {
- D3DFMT_UNKNOWN = 0,
- D3DFMT_R8G8B8 = 20,
- D3DFMT_A8R8G8B8 = 21,
- D3DFMT_X8R8G8B8 = 22,
- D3DFMT_R5G6B5 = 23,
- D3DFMT_X1R5G5B5 = 24,
- D3DFMT_A1R5G5B5 = 25,
- D3DFMT_A4R4G4B4 = 26,
- D3DFMT_R3G3B2 = 27,
- D3DFMT_A8 = 28,
- D3DFMT_A8R3G3B2 = 29,
- D3DFMT_X4R4G4B4 = 30,
- D3DFMT_A2B10G10R10 = 31,
- D3DFMT_A8B8G8R8 = 32,
- D3DFMT_X8B8G8R8 = 33,
- D3DFMT_G16R16 = 34,
- D3DFMT_A2R10G10B10 = 35,
- D3DFMT_A16B16G16R16 = 36,
- D3DFMT_A8P8 = 40,
- D3DFMT_P8 = 41,
- D3DFMT_L8 = 50,
- D3DFMT_A8L8 = 51,
- D3DFMT_A4L4 = 52,
- D3DFMT_V8U8 = 60,
- D3DFMT_L6V5U5 = 61,
- D3DFMT_X8L8V8U8 = 62,
- D3DFMT_Q8W8V8U8 = 63,
- D3DFMT_V16U16 = 64,
- D3DFMT_A2W10V10U10 = 67,
- D3DFMT_UYVY = 0x59565955,
- D3DFMT_R8G8_B8G8 = 0x47424752,
- D3DFMT_YUY2 = 0x32595559,
- D3DFMT_G8R8_G8B8 = 0x42475247,
- D3DFMT_DXT1 = 0x31545844,
- D3DFMT_DXT2 = 0x32545844,
- D3DFMT_DXT3 = 0x33545844,
- D3DFMT_DXT4 = 0x34545844,
- D3DFMT_DXT5 = 0x35545844,
- D3DFMT_D16_LOCKABLE = 70,
- D3DFMT_D32 = 71,
- D3DFMT_D15S1 = 73,
- D3DFMT_D24S8 = 75,
- D3DFMT_D24X8 = 77,
- D3DFMT_D24X4S4 = 79,
- D3DFMT_D16 = 80,
- D3DFMT_D32F_LOCKABLE = 82,
- D3DFMT_D24FS8 = 83,
- D3DFMT_D32_LOCKABLE = 84,
- D3DFMT_S8_LOCKABLE = 85,
- D3DFMT_L16 = 81,
- D3DFMT_VERTEXDATA =100,
- D3DFMT_INDEX16 =101,
- D3DFMT_INDEX32 =102,
- D3DFMT_Q16W16V16U16 =110,
- D3DFMT_MULTI2_ARGB8 = 0x3154454D,
- D3DFMT_R16F = 111,
- D3DFMT_G16R16F = 112,
- D3DFMT_A16B16G16R16F = 113,
- D3DFMT_R32F = 114,
- D3DFMT_G32R32F = 115,
- D3DFMT_A32B32G32R32F = 116,
- D3DFMT_CxV8U8 = 117,
- D3DFMT_A1 = 118,
- D3DFMT_A2B10G10R10_XR_BIAS = 119,
- D3DFMT_BINARYBUFFER = 199,
- D3DFMT_FORCE_DWORD =0x7fffffff
- }
- enum DDSFLAGS : uint
- {
- DDSD_CAPS = 0x1,
- DDSD_HEIGHT = 0x2,
- DDSD_WIDTH = 0x4,
- DDSD_PITCH = 0x8,
- DDSD_PIXELFORMAT = 0x1000,
- DDSD_MIPMAPCOUNT = 0x20000,
- DDSD_LINEARSIZE = 0x80000,
- DDSD_DEPTH = 0x800000
- }
- enum DDSCAPS : uint
- {
- DDSCAPS_COMPLEX = 0x8,
- DDSCAPS_MIPMAP = 0x400000,
- DDSCAPS_TEXTURE = 0x1000 // Required
- }
- enum DDSCAPS2 : uint
- {
- DDSCAPS2_CUBEMAP = 0x200,
- DDSCAPS2_CUBEMAP_POSITIVEX = 0x400,
- DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800,
- DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000,
- DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000,
- DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000,
- DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000,
- DDSCAPS2_VOLUME = 0x200000
- }
- enum DDSPXLFMTFLAGS : uint
- {
- DDPF_ALPHAPIXELS = 0x1,
- DDPF_ALPHA = 0x2,
- DDPF_FOURCC = 0x4,
- DDPF_RGB = 0x40,
- DDPF_YUV = 0x200,
- DDPF_LUMINANCE = 0x20000
- }
- class DDSPixelFormat
- {
- public uint m_size;
- public DDSPXLFMTFLAGS m_flags;
- public D3DFORMAT m_fourCC; // DXT1
- public uint m_rgbBitCount;
- public uint m_rBitMask; // Red
- public uint m_gBitMask; // Green
- public uint m_bBitMask; // Blue
- public uint m_aBitMask; // Alpha
- public DDSPixelFormat()
- {
- m_size = 32;
- }
- }
- class DDSHeader
- {
- public uint m_magic; // "DDS " or 0x44445320
- public uint m_size; // Always 124
- public uint m_flags; //
- public uint m_height;
- public uint m_width;
- public uint m_pitchOrLinearSize; // Big Endian
- public uint m_depth; // 0?
- public uint m_mipMapCount; // 1?
- public byte[] m_reserved; // Size=0x2C, its unused so we can put text here
- public DDSPixelFormat m_fmt;
- public uint m_caps;
- public uint m_caps2;
- public uint m_caps3;
- public uint m_caps4;
- public uint m_reserved2; // Unused
- public DDSHeader()
- {
- m_magic = 0x20534444; // "DDS "
- m_size = 124;
- m_reserved2 = 0;
- m_fmt = new DDSPixelFormat();
- }
- }
- class ImageChunk
- {
- public uint m_uncompressedSize; // Non Compressed Size
- public uint m_compressedSize;
- public byte[] m_data;
- public ImageChunk(Stream stream)
- {
- EndianReader br = new EndianReader(stream, EndianType.BigEndian);
- m_uncompressedSize = br.ReadUInt32();
- m_compressedSize = br.ReadUInt32();
- m_data = br.ReadBytes((int)m_compressedSize);
- }
- }
- List<ImageChunk> m_chunks = new List<ImageChunk>();
- /// <summary>
- /// Reads out of a chunk file, uses the resource data to generate a working DDS file
- /// Assumes that the only type used in Frostbite 2 is DXT1
- /// </summary>
- /// <param name="chunkFile">File path to the chunk file (resource.id chunk)</param>
- /// <param name="outputFile">File path to the final output .dds file</param>
- public void GenerateDDSFile(string chunkFile, string outputFile, ITexture info)
- {
- EndianReader br = new EndianReader(new FileStream(chunkFile, FileMode.Open, FileAccess.Read), EndianType.BigEndian);
- while (br.BaseStream.Position < br.BaseStream.Length)
- {
- m_chunks.Add(new ImageChunk(br.BaseStream));
- }
- br.Close();
- BinaryWriter bw = new BinaryWriter(new FileStream(outputFile, FileMode.Create, FileAccess.Write));
- // Generate the Header and PixelFormatData
- DDSHeader dds = new DDSHeader();
- dds.m_flags = (uint)(DDSFLAGS.DDSD_CAPS | DDSFLAGS.DDSD_HEIGHT | DDSFLAGS.DDSD_WIDTH | DDSFLAGS.DDSD_PIXELFORMAT); // Default, others can be added later
- dds.m_height = info.m_height;
- dds.m_width = info.m_width;
- dds.m_pitchOrLinearSize = info.m_pitch;
- dds.m_depth = info.m_depth;
- dds.m_mipMapCount = 15;
- dds.m_reserved = new byte[4*11];
- dds.m_fmt.m_flags = DDSPXLFMTFLAGS.DDPF_FOURCC;
- // Texture Specific Settings
- switch ((TextureFormat)info.m_format)
- {
- case TextureFormat.TextureFormat_DXT1:
- dds.m_fmt.m_fourCC = D3DFORMAT.D3DFMT_DXT1;
- break;
- case TextureFormat.TextureFormat_DXT3:
- dds.m_fmt.m_fourCC = D3DFORMAT.D3DFMT_DXT3;
- break;
- case TextureFormat.TextureFormat_DXT5:
- dds.m_fmt.m_fourCC = D3DFORMAT.D3DFMT_DXT5;
- break;
- default:
- break;
- }
- // General Texture Settings
- switch ((TextureFormat)info.m_format)
- {
- case TextureFormat.TextureFormat_DXT1:
- case TextureFormat.TextureFormat_DXT3:
- case TextureFormat.TextureFormat_DXT5:
- dds.m_fmt.m_flags |= DDSPXLFMTFLAGS.DDPF_RGB;
- dds.m_fmt.m_rgbBitCount = 32;
- dds.m_fmt.m_rBitMask = 0x000000FF;
- dds.m_fmt.m_gBitMask = 0x0000FF00;
- dds.m_fmt.m_bBitMask = 0x00FF0000;
- dds.m_fmt.m_aBitMask = 0xFF000000;
- break;
- default:
- break;
- }
- dds.m_caps = (uint)DDSCAPS.DDSCAPS_TEXTURE;
- dds.m_caps2 = 0;
- dds.m_caps3 = 0;
- dds.m_caps4 = 0;
- bw.Write(dds.m_magic);
- bw.Write(dds.m_size);
- bw.Write(dds.m_flags);
- bw.Write(dds.m_height);
- bw.Write(dds.m_width);
- bw.Write(dds.m_pitchOrLinearSize);
- bw.Write(dds.m_depth);
- bw.Write(dds.m_mipMapCount);
- bw.Write(dds.m_reserved);
- bw.Write(dds.m_fmt.m_size);
- bw.Write((uint)dds.m_fmt.m_flags);
- bw.Write((uint)dds.m_fmt.m_fourCC);
- bw.Write(dds.m_fmt.m_rgbBitCount);
- bw.Write(dds.m_fmt.m_rBitMask);
- bw.Write(dds.m_fmt.m_gBitMask);
- bw.Write(dds.m_fmt.m_bBitMask);
- bw.Write(dds.m_fmt.m_aBitMask);
- bw.Write(dds.m_caps);
- bw.Write(dds.m_caps2);
- bw.Write(dds.m_caps3);
- bw.Write(dds.m_caps4);
- bw.Write(dds.m_reserved2);
- // Loop through all the chunks
- foreach (ImageChunk img in m_chunks)
- {
- if (img.m_compressedSize == img.m_uncompressedSize)
- bw.Write(img.m_data);
- else
- {
- Stream m_final = new MemoryStream();
- ZOutputStream outStream = new ZOutputStream(m_final);
- MemoryStream compressedData = new MemoryStream(img.m_data);
- try
- {
- CResource.CopyStream(compressedData, outStream);
- }
- finally
- {
- compressedData.Close();
- //outStream.Close();
- }
- m_final.Position = 0;
- BinaryReader brx = new BinaryReader(m_final);
- bw.Write(brx.ReadBytes((int)brx.BaseStream.Length));
- brx.Close();
- }
- }
- bw.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment