1
0
mirror of synced 2024-12-02 11:07:26 +01:00
Switch-Toolbox/Switch_FileFormatsMain/FileFormats/Texture/XTX.cs

314 lines
11 KiB
C#
Raw Normal View History

2018-11-12 02:51:12 +01:00
using System;
using System.Collections.Generic;
using System.IO;
2018-11-12 02:51:12 +01:00
using System.Text;
using System.Drawing;
2018-11-12 02:51:12 +01:00
using System.Threading.Tasks;
using Switch_Toolbox;
using System.Windows.Forms;
using Switch_Toolbox.Library;
using Switch_Toolbox.Library.IO;
2018-11-12 02:51:12 +01:00
namespace FirstPlugin
{
2018-12-02 23:30:46 +01:00
public class XTX : TreeNodeFile, IFileFormat
2018-11-12 02:51:12 +01:00
{
public bool CanSave { get; set; } = false;
public bool FileIsEdited { get; set; } = false;
public bool FileIsCompressed { get; set; } = false;
public string[] Description { get; set; } = new string[] { "XTX" };
public string[] Extension { get; set; } = new string[] { "*.xtx", "*.z" };
public string Magic { get; set; } = "DFvN ";
public CompressionType CompressionType { get; set; } = CompressionType.None;
public byte[] Data { get; set; }
public string FileName { get; set; }
public bool IsActive { get; set; } = false;
public bool UseEditMenu { get; set; } = false;
public string FilePath { get; set; }
2018-11-17 03:13:05 +01:00
public IFileInfo IFileInfo { get; set; }
2018-11-12 02:51:12 +01:00
public Type[] Types
{
get
{
List<Type> types = new List<Type>();
return types.ToArray();
}
}
public void Load()
{
IsActive = true;
2018-12-02 23:30:46 +01:00
Text = FileName;
LoadFile(Data);
2018-11-12 02:51:12 +01:00
}
public void Unload()
{
}
public byte[] Save()
{
return null;
}
public class XTXFormats
{
public enum XTXImageFormat : uint
{
NVN_FORMAT_RGBA8 = 0x00000025,
NVN_FORMAT_RGBA8_SRGB = 0x00000038,
NVN_FORMAT_RGB10A2 = 0x0000003d,
NVN_FORMAT_RGB565 = 0x0000003c,
NVN_FORMAT_RGB5A1 = 0x0000003b,
NVN_FORMAT_RGBA4 = 0x00000039,
NVN_FORMAT_R8 = 0x00000001,
NVN_FORMAT_RG8 = 0x0000000d,
DXT1 = 0x00000042,
DXT3 = 0x00000043,
DXT5 = 0x00000044,
BC4U = 0x00000049,
BC4S = 0x0000004a,
BC5U = 0x0000004b,
BC5S = 0x0000004c,
};
public static uint blk_dims(uint format)
{
switch (format)
{
case (uint)XTXImageFormat.DXT1:
case (uint)XTXImageFormat.DXT3:
case (uint)XTXImageFormat.DXT5:
case (uint)XTXImageFormat.BC4U:
case (uint)XTXImageFormat.BC4S:
case (uint)XTXImageFormat.BC5U:
case (uint)XTXImageFormat.BC5S:
case 0x2d:
return 0x44;
default: return 0x11;
}
}
public static uint bpps(uint format)
{
switch (format)
{
case (uint)XTXImageFormat.NVN_FORMAT_R8:
return 1;
case (uint)XTXImageFormat.NVN_FORMAT_RGBA8:
case (uint)XTXImageFormat.NVN_FORMAT_RGBA8_SRGB:
case (uint)XTXImageFormat.NVN_FORMAT_RGB10A2:
return 4;
case (uint)XTXImageFormat.NVN_FORMAT_RGB565:
case (uint)XTXImageFormat.NVN_FORMAT_RGB5A1:
case (uint)XTXImageFormat.NVN_FORMAT_RGBA4:
case (uint)XTXImageFormat.NVN_FORMAT_RG8:
return 2;
case (uint)XTXImageFormat.DXT1:
case (uint)XTXImageFormat.BC4S:
case (uint)XTXImageFormat.BC4U:
return 8;
case (uint)XTXImageFormat.DXT3:
case (uint)XTXImageFormat.DXT5:
case (uint)XTXImageFormat.BC5U:
case (uint)XTXImageFormat.BC5S:
return 16;
default: return 0x00;
}
}
}
2018-12-02 23:30:46 +01:00
public uint HeaderSize { get; set; }
public uint MajorVersion { get; set; }
public uint MinorVersion { get; set; }
public BlockHeader blockHeader { get; set; }
private const int texHeadBlkType = 2;
private const int dataBlkType = 3;
public void LoadFile(byte[] data)
{
2018-12-02 23:30:46 +01:00
FileReader reader = new FileReader(new MemoryStream(data));
string Signature = reader.ReadString(4, Encoding.ASCII);
if (Signature != "DFvN")
throw new Exception($"Invalid signature {Signature}! Expected DFvN.");
2018-12-02 23:30:46 +01:00
HeaderSize = reader.ReadUInt32();
MajorVersion = reader.ReadUInt32();
MinorVersion = reader.ReadUInt32();
2018-12-02 23:30:46 +01:00
blockHeader = new BlockHeader();
blockHeader.Read(reader);
}
public override void OnClick(TreeView treeview)
{
UpdateEditor();
}
2018-12-02 23:30:46 +01:00
public void UpdateEditor()
{
if (Viewport.Instance.gL_ControlModern1.Visible == false)
PluginRuntime.FSHPDockState = WeifenLuo.WinFormsUI.Docking.DockState.Document;
XTXEditor docked = (XTXEditor)LibraryGUI.Instance.GetContentDocked(new XTXEditor());
if (docked == null)
{
2018-12-02 23:30:46 +01:00
docked = new XTXEditor();
LibraryGUI.Instance.LoadDockContent(docked, PluginRuntime.FSHPDockState);
}
2018-12-02 23:30:46 +01:00
docked.Text = Text;
docked.Dock = DockStyle.Fill;
docked.LoadProperty(blockHeader.textureInfo);
}
2018-12-02 23:30:46 +01:00
public class BlockHeader
{
public uint BlockSize { get; set; }
public UInt64 DataSize { get; set; }
public uint BlockType { get; set; }
public uint GlobalBlockIndex { get; set; }
public uint IncBlockTypeIndex { get; set; }
public TextureInfo textureInfo { get; set; }
public long DataOffset;
public void Read(FileReader reader)
{
2018-12-02 23:30:46 +01:00
string Signature = reader.ReadString(4, Encoding.ASCII);
if (Signature != "HBvN")
throw new Exception($"Invalid signature {Signature}! Expected HBvN.");
BlockSize = reader.ReadUInt32();
DataSize = reader.ReadUInt64();
DataOffset = reader.ReadInt64();
BlockType = reader.ReadUInt32();
GlobalBlockIndex = reader.ReadUInt32();
IncBlockTypeIndex = reader.ReadUInt32();
2018-12-02 23:30:46 +01:00
if (BlockType == texHeadBlkType)
{
2018-12-02 23:30:46 +01:00
textureInfo = new TextureInfo();
textureInfo.Read(reader);
}
2018-12-02 23:30:46 +01:00
if (BlockType == dataBlkType)
{
}
}
2018-12-02 23:30:46 +01:00
}
public class TextureInfo : STGenericTexture
2018-12-02 23:30:46 +01:00
{
public UInt64 DataSize { get; set; }
public uint Alignment { get; set; }
public uint Depth { get; set; }
public uint Target { get; set; }
public XTXFormats.XTXImageFormat Format { get; set; }
public uint MipCount { get; set; }
public uint SliceSize { get; set; }
public uint[] MipOffsets { get; set; }
public BlockHeader DataBlockHeader { get; set; }
public byte[] ImageData;
2018-12-02 23:30:46 +01:00
public void Read(FileReader reader)
{
2018-12-02 23:30:46 +01:00
DataSize = reader.ReadUInt64();
Alignment = reader.ReadUInt32();
Width = reader.ReadUInt32();
Height = reader.ReadUInt32();
Depth = reader.ReadUInt32();
Target = reader.ReadUInt32();
Format = reader.ReadEnum<XTXFormats.XTXImageFormat>(true);
MipCount = reader.ReadUInt32();
SliceSize = reader.ReadUInt32();
long offPos = reader.Position;
MipOffsets = reader.ReadUInt32s((int)MipCount);
reader.Seek(offPos + 68, SeekOrigin.Begin);
byte[] Layout = reader.ReadBytes(8);
byte Sparse = reader.ReadByte();
reader.Seek(3);
long DataBlockOff = reader.Position;
DataBlockHeader = new BlockHeader();
DataBlockHeader.Read(reader);
reader.Seek(DataBlockOff + DataBlockHeader.DataOffset, SeekOrigin.Begin);
long datastart = reader.Position;
ImageData = reader.ReadBytes((int)DataSize);
2018-12-02 23:30:46 +01:00
if (ImageData.Length == 0)
throw new System.Exception("Empty data size!");
2018-12-02 23:30:46 +01:00
reader.Seek(DataBlockOff + DataBlockHeader.DataOffset + (long)DataBlockHeader.DataSize, SeekOrigin.Begin);
BlockHeader EndBlockHeader = new BlockHeader();
EndBlockHeader.Read(reader);
}
2018-12-02 23:30:46 +01:00
public void LoadTexture()
{
Surfaces.Clear();
2018-12-02 23:30:46 +01:00
Console.WriteLine(Format);
2018-12-02 23:30:46 +01:00
uint blk_dim = XTXFormats.blk_dims((uint)((int)Format >> 8));
uint blkWidth = blk_dim >> 4;
uint blkHeight = blk_dim & 0xF;
2018-12-02 23:30:46 +01:00
uint blockHeight = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;
2018-12-02 23:30:46 +01:00
int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;
2018-12-02 23:30:46 +01:00
int TileMode = 0;
2018-12-02 23:30:46 +01:00
uint bpp = XTXFormats.bpps((uint)Format);
List<byte[]> mips = new List<byte[]>();
2018-12-02 23:30:46 +01:00
int blockHeightShift = 0;
for (int mipLevel = 0; mipLevel < MipOffsets.Length; mipLevel++)
2018-12-02 23:30:46 +01:00
{
2018-12-02 23:30:46 +01:00
uint width = (uint)Math.Max(1, Width >> mipLevel);
uint height = (uint)Math.Max(1, Height >> mipLevel);
// uint size = width * height * bpp;
uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp;
byte[] Output = new byte[size];
uint mipOffset;
if (mipLevel != 0)
{
mipOffset = (MipOffsets[mipLevel - 1]);
if (mipLevel == 1)
mipOffset -= (uint)size;
Array.Copy(ImageData, mipOffset, Output, 0, size);
}
else
Output = ImageData;
byte[] output = new byte[size];
2018-12-02 23:30:46 +01:00
Console.WriteLine(mipLevel + " " + size);
if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight)
blockHeightShift += 1;
byte[] result = TegraX1Swizzle.deswizzle(width, height, blkWidth, blkHeight, (int)Target, bpp, (uint)TileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), Output);
2018-12-02 23:30:46 +01:00
//Create a copy and use that to remove uneeded data
byte[] result_ = new byte[size];
Array.Copy(result, 0, result_, 0, size);
mips.Add(result_);
}
Surfaces.Add(new Surface() { mipmaps = mips });
}
}
2018-11-12 02:51:12 +01:00
}
}