| | | 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.Runtime.CompilerServices; |
| | | 9 | | |
| | | 10 | | namespace Lattice.Math |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// LUT 文件读写器 - 支持版本兼容的 LUT 文件格式 |
| | | 14 | | /// <para>文件格式: [Header(32 bytes)][Data(variable)]</para> |
| | | 15 | | /// </summary> |
| | | 16 | | public static class FPLutFile |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// LUT 文件扩展名 |
| | | 20 | | /// </summary> |
| | | 21 | | public const string FileExtension = ".fplut"; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// 从文件加载 LUT |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="filePath">文件路径</param> |
| | | 27 | | /// <returns>解析后的 LUT 数据</returns> |
| | | 28 | | /// <exception cref="FileNotFoundException">文件不存在</exception> |
| | | 29 | | /// <exception cref="InvalidDataException">文件格式无效</exception> |
| | | 30 | | /// <exception cref="InvalidDataException">版本不兼容</exception> |
| | | 31 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 32 | | public static FPLutData Load(string filePath) |
| | | 33 | | { |
| | | 34 | | if (!File.Exists(filePath)) |
| | | 35 | | throw new FileNotFoundException("LUT file not found", filePath); |
| | | 36 | | |
| | | 37 | | byte[] fileData = File.ReadAllBytes(filePath); |
| | | 38 | | return LoadFromBytes(fileData); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// 从字节数组加载 LUT |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="data">原始字节数据</param> |
| | | 45 | | /// <returns>解析后的 LUT 数据</returns> |
| | | 46 | | public static FPLutData LoadFromBytes(byte[] data) |
| | | 47 | | { |
| | | 48 | | if (data.Length < FPLutHeader.HeaderSize) |
| | | 49 | | throw new InvalidDataException($"Data too small for LUT header: {data.Length} bytes"); |
| | | 50 | | |
| | | 51 | | // 解析头部 |
| | | 52 | | var header = FPLutHeader.FromBytes(data); |
| | | 53 | | |
| | | 54 | | // 验证数据大小 |
| | | 55 | | long expectedDataSize = header.EntryCount * header.EntrySize; |
| | | 56 | | long actualDataSize = data.Length - FPLutHeader.HeaderSize; |
| | | 57 | | |
| | | 58 | | if (actualDataSize < expectedDataSize) |
| | | 59 | | throw new InvalidDataException( |
| | | 60 | | $"LUT data truncated: expected {expectedDataSize} bytes, got {actualDataSize}"); |
| | | 61 | | |
| | | 62 | | // 验证校验和(如果启用) |
| | | 63 | | if (header.Checksum != 0) |
| | | 64 | | { |
| | | 65 | | byte[] payload = new byte[expectedDataSize]; |
| | | 66 | | Buffer.BlockCopy(data, FPLutHeader.HeaderSize, payload, 0, (int)expectedDataSize); |
| | | 67 | | |
| | | 68 | | if (!header.VerifyChecksum(payload)) |
| | | 69 | | throw new InvalidDataException("LUT checksum verification failed"); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | // 提取数据 |
| | | 73 | | long[] longData; |
| | | 74 | | int[] intData; |
| | | 75 | | |
| | | 76 | | if (header.EntrySize == 8) |
| | | 77 | | { |
| | | 78 | | longData = new long[header.EntryCount]; |
| | | 79 | | Buffer.BlockCopy(data, FPLutHeader.HeaderSize, longData, 0, (int)expectedDataSize); |
| | | 80 | | intData = Array.Empty<int>(); |
| | | 81 | | } |
| | | 82 | | else |
| | | 83 | | { |
| | | 84 | | intData = new int[header.EntryCount]; |
| | | 85 | | Buffer.BlockCopy(data, FPLutHeader.HeaderSize, intData, 0, (int)expectedDataSize); |
| | | 86 | | longData = Array.Empty<long>(); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | return new FPLutData(header, longData, intData); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// 保存 LUT 到文件 |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="filePath">目标文件路径</param> |
| | | 96 | | /// <param name="data">LUT 数据</param> |
| | | 97 | | /// <param name="version">文件版本(默认当前版本)</param> |
| | | 98 | | public static void Save(string filePath, FPLutData data, uint? version = null) |
| | | 99 | | { |
| | | 100 | | byte[] bytes = SaveToBytes(data, version); |
| | | 101 | | File.WriteAllBytes(filePath, bytes); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// 将 LUT 数据序列化为字节数组 |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="data">LUT 数据</param> |
| | | 108 | | /// <param name="version">文件版本(默认当前版本)</param> |
| | | 109 | | /// <returns>序列化后的字节数组</returns> |
| | | 110 | | public static byte[] SaveToBytes(FPLutData data, uint? version = null) |
| | | 111 | | { |
| | | 112 | | var header = data.Header; |
| | | 113 | | header.Version = version ?? FPLutHeader.CurrentVersion; |
| | | 114 | | header.FileMagic = FPLutHeader.Magic; |
| | | 115 | | |
| | | 116 | | // 计算校验和 |
| | | 117 | | if (header.EntrySize == 8 && data.LongData.Length > 0) |
| | | 118 | | { |
| | | 119 | | byte[] payload = new byte[data.LongData.Length * 8]; |
| | | 120 | | Buffer.BlockCopy(data.LongData, 0, payload, 0, payload.Length); |
| | | 121 | | header.Checksum = FPLutHeader.ComputeChecksum(payload); |
| | | 122 | | } |
| | | 123 | | else if (header.EntrySize == 4 && data.IntData.Length > 0) |
| | | 124 | | { |
| | | 125 | | byte[] payload = new byte[data.IntData.Length * 4]; |
| | | 126 | | Buffer.BlockCopy(data.IntData, 0, payload, 0, payload.Length); |
| | | 127 | | header.Checksum = FPLutHeader.ComputeChecksum(payload); |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | // 序列化 |
| | | 131 | | byte[] headerBytes = header.ToBytes(); |
| | | 132 | | byte[] result = new byte[FPLutHeader.HeaderSize + |
| | | 133 | | (header.EntrySize == 8 ? data.LongData.Length * 8 : data.IntData.Length * 4)]; |
| | | 134 | | |
| | | 135 | | Buffer.BlockCopy(headerBytes, 0, result, 0, FPLutHeader.HeaderSize); |
| | | 136 | | |
| | | 137 | | if (header.EntrySize == 8 && data.LongData.Length > 0) |
| | | 138 | | { |
| | | 139 | | Buffer.BlockCopy(data.LongData, 0, result, FPLutHeader.HeaderSize, data.LongData.Length * 8); |
| | | 140 | | } |
| | | 141 | | else if (data.IntData.Length > 0) |
| | | 142 | | { |
| | | 143 | | Buffer.BlockCopy(data.IntData, 0, result, FPLutHeader.HeaderSize, data.IntData.Length * 4); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | return result; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// 检查文件是否为有效的 LUT 文件 |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="filePath">文件路径</param> |
| | | 153 | | /// <returns>是否有效</returns> |
| | | 154 | | public static bool IsValidLutFile(string filePath) |
| | | 155 | | { |
| | | 156 | | try |
| | | 157 | | { |
| | | 158 | | if (!File.Exists(filePath)) |
| | | 159 | | return false; |
| | | 160 | | |
| | | 161 | | byte[] data = File.ReadAllBytes(filePath); |
| | | 162 | | if (data.Length < FPLutHeader.HeaderSize) |
| | | 163 | | return false; |
| | | 164 | | |
| | | 165 | | uint magic = BitConverter.ToUInt32(data, 0); |
| | | 166 | | return magic == FPLutHeader.Magic; |
| | | 167 | | } |
| | | 168 | | catch |
| | | 169 | | { |
| | | 170 | | return false; |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// 获取 LUT 文件信息(不加载完整数据) |
| | | 176 | | /// </summary> |
| | | 177 | | /// <param name="filePath">文件路径</param> |
| | | 178 | | /// <returns>文件头信息</returns> |
| | | 179 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 180 | | public static FPLutHeader GetFileInfo(string filePath) |
| | | 181 | | { |
| | | 182 | | byte[] headerData = new byte[FPLutHeader.HeaderSize]; |
| | | 183 | | using (var fs = File.OpenRead(filePath)) |
| | | 184 | | { |
| | | 185 | | fs.Read(headerData, 0, FPLutHeader.HeaderSize); |
| | | 186 | | } |
| | | 187 | | return FPLutHeader.FromBytes(headerData); |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// 批量转换旧版本 LUT 文件 |
| | | 192 | | /// </summary> |
| | | 193 | | /// <param name="sourceDir">源目录</param> |
| | | 194 | | /// <param name="targetDir">目标目录</param> |
| | | 195 | | /// <param name="targetVersion">目标版本</param> |
| | | 196 | | public static void BatchConvert(string sourceDir, string targetDir, uint targetVersion) |
| | | 197 | | { |
| | | 198 | | Directory.CreateDirectory(targetDir); |
| | | 199 | | |
| | | 200 | | foreach (string file in Directory.GetFiles(sourceDir, "*" + FileExtension)) |
| | | 201 | | { |
| | | 202 | | string fileName = Path.GetFileName(file); |
| | | 203 | | string targetPath = Path.Combine(targetDir, fileName); |
| | | 204 | | |
| | | 205 | | try |
| | | 206 | | { |
| | | 207 | | var data = Load(file); |
| | | 208 | | Save(targetPath, data, targetVersion); |
| | | 209 | | } |
| | | 210 | | catch (Exception ex) |
| | | 211 | | { |
| | | 212 | | throw new InvalidOperationException($"Failed to convert {fileName}: {ex.Message}", ex); |
| | | 213 | | } |
| | | 214 | | } |
| | | 215 | | } |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// LUT 数据结构 |
| | | 220 | | /// </summary> |
| | | 221 | | public readonly struct FPLutData |
| | | 222 | | { |
| | | 223 | | /// <summary>文件头</summary> |
| | | 224 | | public readonly FPLutHeader Header; |
| | | 225 | | |
| | | 226 | | /// <summary>64-bit 数据(EntrySize == 8 时使用)</summary> |
| | | 227 | | public readonly long[] LongData; |
| | | 228 | | |
| | | 229 | | /// <summary>32-bit 数据(EntrySize == 4 时使用)</summary> |
| | | 230 | | public readonly int[] IntData; |
| | | 231 | | |
| | | 232 | | public FPLutData(FPLutHeader header, long[] longData, int[] intData) |
| | | 233 | | { |
| | | 234 | | Header = header; |
| | | 235 | | LongData = longData; |
| | | 236 | | IntData = intData; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// 创建 64-bit LUT 数据 |
| | | 241 | | /// </summary> |
| | | 242 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 243 | | public static FPLutData CreateLong(LutType type, long[] data) |
| | | 244 | | { |
| | | 245 | | var header = new FPLutHeader( |
| | | 246 | | FPLutHeader.CurrentVersion, |
| | | 247 | | type, |
| | | 248 | | (uint)data.Length, |
| | | 249 | | 8, // 8 bytes per entry |
| | | 250 | | 0 // checksum computed on save |
| | | 251 | | ); |
| | | 252 | | return new FPLutData(header, data, Array.Empty<int>()); |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// 创建 32-bit LUT 数据 |
| | | 257 | | /// </summary> |
| | | 258 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 259 | | public static FPLutData CreateInt(LutType type, int[] data) |
| | | 260 | | { |
| | | 261 | | var header = new FPLutHeader( |
| | | 262 | | FPLutHeader.CurrentVersion, |
| | | 263 | | type, |
| | | 264 | | (uint)data.Length, |
| | | 265 | | 4, // 4 bytes per entry |
| | | 266 | | 0 // checksum computed on save |
| | | 267 | | ); |
| | | 268 | | return new FPLutData(header, Array.Empty<long>(), data); |
| | | 269 | | } |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | /// <summary> |
| | | 273 | | /// LUT 版本兼容性检查器 |
| | | 274 | | /// </summary> |
| | | 275 | | public static class FPLutCompatibility |
| | | 276 | | { |
| | | 277 | | /// <summary> |
| | | 278 | | /// 检查版本兼容性 |
| | | 279 | | /// </summary> |
| | | 280 | | /// <param name="fileVersion">文件版本</param> |
| | | 281 | | /// <returns>兼容性结果</returns> |
| | | 282 | | public static CompatibilityResult CheckCompatibility(uint fileVersion) |
| | | 283 | | { |
| | 0 | 284 | | uint current = FPLutHeader.CurrentVersion; |
| | 0 | 285 | | uint min = FPLutHeader.MinCompatibleVersion; |
| | | 286 | | |
| | 0 | 287 | | if (fileVersion == current) |
| | 0 | 288 | | return CompatibilityResult.ExactMatch; |
| | | 289 | | |
| | 0 | 290 | | if (fileVersion > current) |
| | 0 | 291 | | return CompatibilityResult.NewerVersion; |
| | | 292 | | |
| | 0 | 293 | | if (fileVersion >= min) |
| | 0 | 294 | | return CompatibilityResult.BackwardCompatible; |
| | | 295 | | |
| | 0 | 296 | | return CompatibilityResult.Incompatible; |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | /// <summary> |
| | | 300 | | /// 获取版本变更说明 |
| | | 301 | | /// </summary> |
| | | 302 | | public static string GetVersionChanges(uint fromVersion, uint toVersion) |
| | | 303 | | { |
| | | 304 | | // 版本变更日志 |
| | 0 | 305 | | if (fromVersion == 0x00010000 && toVersion == 0x00010000) |
| | 0 | 306 | | return "Same version (1.0.0)"; |
| | | 307 | | |
| | 0 | 308 | | return $"Unknown version transition: {FPLutHeader.VersionToString(fromVersion)} -> {FPLutHeader.VersionToStr |
| | | 309 | | } |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// 兼容性结果 |
| | | 314 | | /// </summary> |
| | | 315 | | public enum CompatibilityResult |
| | | 316 | | { |
| | | 317 | | /// <summary>完全匹配</summary> |
| | | 318 | | ExactMatch, |
| | | 319 | | |
| | | 320 | | /// <summary>向后兼容(旧版本)</summary> |
| | | 321 | | BackwardCompatible, |
| | | 322 | | |
| | | 323 | | /// <summary>新版本(需要更新)</summary> |
| | | 324 | | NewerVersion, |
| | | 325 | | |
| | | 326 | | /// <summary>不兼容</summary> |
| | | 327 | | Incompatible |
| | | 328 | | } |
| | | 329 | | } |