| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | #nullable enable |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Text; |
| | | 9 | | |
| | | 10 | | namespace Lattice.Math |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// LUT 文件版本头 |
| | | 14 | | /// </summary> |
| | | 15 | | public struct FPLutHeader : IEquatable<FPLutHeader> |
| | | 16 | | { |
| | | 17 | | /// <summary>魔数:'FPLT' = 0x464P4C54 (FP = 0x464P, LT = 0x4C54)</summary> |
| | | 18 | | public const uint Magic = 0x464F4C54; // 'F' 'P' 'L' 'T' ASCII |
| | | 19 | | |
| | | 20 | | /// <summary>当前版本:1.0.0 = 0x00010000</summary> |
| | | 21 | | public const uint CurrentVersion = 0x00010000; |
| | | 22 | | |
| | | 23 | | /// <summary>兼容的最旧版本:1.0.0</summary> |
| | | 24 | | public const uint MinCompatibleVersion = 0x00010000; |
| | | 25 | | |
| | | 26 | | /// <summary>魔数(4 bytes)</summary> |
| | | 27 | | public uint FileMagic; |
| | | 28 | | |
| | | 29 | | /// <summary>版本号(4 bytes):主.次.修.预 = 1 byte each</summary> |
| | | 30 | | public uint Version; |
| | | 31 | | |
| | | 32 | | /// <summary>LUT 类型(4 bytes)</summary> |
| | | 33 | | public LutType Type; |
| | | 34 | | |
| | | 35 | | /// <summary>条目数量(4 bytes)</summary> |
| | | 36 | | public uint EntryCount; |
| | | 37 | | |
| | | 38 | | /// <summary>每个条目字节数(4 bytes)</summary> |
| | | 39 | | public uint EntrySize; |
| | | 40 | | |
| | | 41 | | /// <summary>校验和 CRC32(4 bytes)</summary> |
| | | 42 | | public uint Checksum; |
| | | 43 | | |
| | | 44 | | /// <summary>保留字段(16 bytes)</summary> |
| | | 45 | | public ulong Reserved1; |
| | | 46 | | public ulong Reserved2; |
| | | 47 | | |
| | | 48 | | /// <summary>头部总大小:32 bytes</summary> |
| | | 49 | | public const int HeaderSize = 32; |
| | | 50 | | |
| | | 51 | | public FPLutHeader(uint version, LutType type, uint entryCount, uint entrySize, uint checksum) |
| | | 52 | | { |
| | 0 | 53 | | FileMagic = Magic; |
| | 0 | 54 | | Version = version; |
| | 0 | 55 | | Type = type; |
| | 0 | 56 | | EntryCount = entryCount; |
| | 0 | 57 | | EntrySize = entrySize; |
| | 0 | 58 | | Checksum = checksum; |
| | 0 | 59 | | Reserved1 = 0; |
| | 0 | 60 | | Reserved2 = 0; |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// 从字节数组解析头部 |
| | | 65 | | /// </summary> |
| | | 66 | | public static FPLutHeader FromBytes(byte[] data) |
| | | 67 | | { |
| | 0 | 68 | | if (data.Length < HeaderSize) |
| | 0 | 69 | | throw new InvalidDataException($"LUT header too small: {data.Length} bytes"); |
| | | 70 | | |
| | 0 | 71 | | var header = new FPLutHeader |
| | 0 | 72 | | { |
| | 0 | 73 | | FileMagic = BitConverter.ToUInt32(data, 0), |
| | 0 | 74 | | Version = BitConverter.ToUInt32(data, 4), |
| | 0 | 75 | | Type = (LutType)BitConverter.ToUInt32(data, 8), |
| | 0 | 76 | | EntryCount = BitConverter.ToUInt32(data, 12), |
| | 0 | 77 | | EntrySize = BitConverter.ToUInt32(data, 16), |
| | 0 | 78 | | Checksum = BitConverter.ToUInt32(data, 20), |
| | 0 | 79 | | Reserved1 = BitConverter.ToUInt64(data, 24), |
| | 0 | 80 | | Reserved2 = BitConverter.ToUInt64(data, 32) |
| | 0 | 81 | | }; |
| | | 82 | | |
| | 0 | 83 | | header.Validate(); |
| | 0 | 84 | | return header; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// 将头部写入字节数组 |
| | | 89 | | /// </summary> |
| | | 90 | | public byte[] ToBytes() |
| | | 91 | | { |
| | 0 | 92 | | var data = new byte[HeaderSize]; |
| | 0 | 93 | | BitConverter.GetBytes(FileMagic).CopyTo(data, 0); |
| | 0 | 94 | | BitConverter.GetBytes(Version).CopyTo(data, 4); |
| | 0 | 95 | | BitConverter.GetBytes((uint)Type).CopyTo(data, 8); |
| | 0 | 96 | | BitConverter.GetBytes(EntryCount).CopyTo(data, 12); |
| | 0 | 97 | | BitConverter.GetBytes(EntrySize).CopyTo(data, 16); |
| | 0 | 98 | | BitConverter.GetBytes(Checksum).CopyTo(data, 20); |
| | 0 | 99 | | BitConverter.GetBytes(Reserved1).CopyTo(data, 24); |
| | 0 | 100 | | BitConverter.GetBytes(Reserved2).CopyTo(data, 32); |
| | 0 | 101 | | return data; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// 验证头部有效性 |
| | | 106 | | /// </summary> |
| | | 107 | | public void Validate() |
| | | 108 | | { |
| | 0 | 109 | | if (FileMagic != Magic) |
| | 0 | 110 | | throw new InvalidDataException($"Invalid LUT magic: 0x{FileMagic:X8}, expected 0x{Magic:X8}"); |
| | | 111 | | |
| | 0 | 112 | | if (Version < MinCompatibleVersion) |
| | 0 | 113 | | throw new InvalidDataException($"LUT version {VersionToString(Version)} is too old. Minimum: {VersionToS |
| | | 114 | | |
| | 0 | 115 | | if (EntryCount == 0) |
| | 0 | 116 | | throw new InvalidDataException("LUT entry count is zero"); |
| | | 117 | | |
| | 0 | 118 | | if (EntrySize != 4 && EntrySize != 8) |
| | 0 | 119 | | throw new InvalidDataException($"Invalid entry size: {EntrySize}, expected 4 or 8"); |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// 验证数据校验和 |
| | | 124 | | /// </summary> |
| | | 125 | | public bool VerifyChecksum(byte[] data) |
| | | 126 | | { |
| | 0 | 127 | | uint computed = ComputeChecksum(data); |
| | 0 | 128 | | return computed == Checksum; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// 计算 CRC32 校验和 |
| | | 133 | | /// </summary> |
| | | 134 | | public static uint ComputeChecksum(byte[] data) |
| | | 135 | | { |
| | | 136 | | // 简化实现:实际使用 System.IO.Hashing.Crc32 |
| | 0 | 137 | | uint crc = 0xFFFFFFFF; |
| | 0 | 138 | | foreach (byte b in data) |
| | | 139 | | { |
| | 0 | 140 | | crc ^= b; |
| | 0 | 141 | | for (int i = 0; i < 8; i++) |
| | 0 | 142 | | crc = (crc >> 1) ^ (0xEDB88320u & (uint)(-(crc & 1))); |
| | | 143 | | } |
| | 0 | 144 | | return ~crc; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// 版本号转字符串 |
| | | 149 | | /// </summary> |
| | | 150 | | public static string VersionToString(uint version) |
| | | 151 | | { |
| | 0 | 152 | | byte major = (byte)(version >> 24); |
| | 0 | 153 | | byte minor = (byte)(version >> 16); |
| | 0 | 154 | | byte patch = (byte)(version >> 8); |
| | 0 | 155 | | byte pre = (byte)version; |
| | | 156 | | |
| | 0 | 157 | | if (pre == 0) |
| | 0 | 158 | | return $"{major}.{minor}.{patch}"; |
| | 0 | 159 | | return $"{major}.{minor}.{patch}-pre{pre}"; |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | public bool Equals(FPLutHeader other) => |
| | 0 | 163 | | FileMagic == other.FileMagic && |
| | 0 | 164 | | Version == other.Version && |
| | 0 | 165 | | Type == other.Type; |
| | | 166 | | |
| | 0 | 167 | | public override bool Equals(object? obj) => obj is FPLutHeader other && Equals(other); |
| | 0 | 168 | | public override int GetHashCode() => HashCode.Combine(FileMagic, Version, Type); |
| | 0 | 169 | | public override string ToString() => $"FPLut v{VersionToString(Version)}, {Type}, {EntryCount} entries"; |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// LUT 类型枚举 |
| | | 174 | | /// </summary> |
| | | 175 | | public enum LutType : uint |
| | | 176 | | { |
| | | 177 | | Unknown = 0, |
| | | 178 | | SinCos = 1, |
| | | 179 | | Tan = 2, |
| | | 180 | | Asin = 3, |
| | | 181 | | Acos = 4, |
| | | 182 | | Atan = 5, |
| | | 183 | | Sqrt = 6, |
| | | 184 | | Exp = 7, |
| | | 185 | | Log = 8, |
| | | 186 | | Custom = 0xFFFFFFFF |
| | | 187 | | } |
| | | 188 | | } |