< Summary

Information
Class: Lattice.Math.FPLutFile
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/Math/FPLutFile.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 73
Coverable lines: 73
Total lines: 329
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 34
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Load(...)0%620%
LoadFromBytes(...)0%110100%
Save(...)100%210%
SaveToBytes(...)0%272160%
IsValidLutFile(...)0%2040%
GetFileInfo(...)100%210%
BatchConvert(...)0%620%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/Math/FPLutFile.cs

#LineLine coverage
 1// Copyright (c) 2026 Sideline Authors. All rights reserved.
 2// Licensed under GPL-3.0.
 3
 4#nullable enable
 5
 6using System;
 7using System.IO;
 8using System.Runtime.CompilerServices;
 9
 10namespace 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        {
 034            if (!File.Exists(filePath))
 035                throw new FileNotFoundException("LUT file not found", filePath);
 36
 037            byte[] fileData = File.ReadAllBytes(filePath);
 038            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        {
 048            if (data.Length < FPLutHeader.HeaderSize)
 049                throw new InvalidDataException($"Data too small for LUT header: {data.Length} bytes");
 50
 51            // 解析头部
 052            var header = FPLutHeader.FromBytes(data);
 53
 54            // 验证数据大小
 055            long expectedDataSize = header.EntryCount * header.EntrySize;
 056            long actualDataSize = data.Length - FPLutHeader.HeaderSize;
 57
 058            if (actualDataSize < expectedDataSize)
 059                throw new InvalidDataException(
 060                    $"LUT data truncated: expected {expectedDataSize} bytes, got {actualDataSize}");
 61
 62            // 验证校验和(如果启用)
 063            if (header.Checksum != 0)
 64            {
 065                byte[] payload = new byte[expectedDataSize];
 066                Buffer.BlockCopy(data, FPLutHeader.HeaderSize, payload, 0, (int)expectedDataSize);
 67
 068                if (!header.VerifyChecksum(payload))
 069                    throw new InvalidDataException("LUT checksum verification failed");
 70            }
 71
 72            // 提取数据
 73            long[] longData;
 74            int[] intData;
 75
 076            if (header.EntrySize == 8)
 77            {
 078                longData = new long[header.EntryCount];
 079                Buffer.BlockCopy(data, FPLutHeader.HeaderSize, longData, 0, (int)expectedDataSize);
 080                intData = Array.Empty<int>();
 81            }
 82            else
 83            {
 084                intData = new int[header.EntryCount];
 085                Buffer.BlockCopy(data, FPLutHeader.HeaderSize, intData, 0, (int)expectedDataSize);
 086                longData = Array.Empty<long>();
 87            }
 88
 089            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        {
 0100            byte[] bytes = SaveToBytes(data, version);
 0101            File.WriteAllBytes(filePath, bytes);
 0102        }
 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        {
 0112            var header = data.Header;
 0113            header.Version = version ?? FPLutHeader.CurrentVersion;
 0114            header.FileMagic = FPLutHeader.Magic;
 115
 116            // 计算校验和
 0117            if (header.EntrySize == 8 && data.LongData.Length > 0)
 118            {
 0119                byte[] payload = new byte[data.LongData.Length * 8];
 0120                Buffer.BlockCopy(data.LongData, 0, payload, 0, payload.Length);
 0121                header.Checksum = FPLutHeader.ComputeChecksum(payload);
 122            }
 0123            else if (header.EntrySize == 4 && data.IntData.Length > 0)
 124            {
 0125                byte[] payload = new byte[data.IntData.Length * 4];
 0126                Buffer.BlockCopy(data.IntData, 0, payload, 0, payload.Length);
 0127                header.Checksum = FPLutHeader.ComputeChecksum(payload);
 128            }
 129
 130            // 序列化
 0131            byte[] headerBytes = header.ToBytes();
 0132            byte[] result = new byte[FPLutHeader.HeaderSize +
 0133                (header.EntrySize == 8 ? data.LongData.Length * 8 : data.IntData.Length * 4)];
 134
 0135            Buffer.BlockCopy(headerBytes, 0, result, 0, FPLutHeader.HeaderSize);
 136
 0137            if (header.EntrySize == 8 && data.LongData.Length > 0)
 138            {
 0139                Buffer.BlockCopy(data.LongData, 0, result, FPLutHeader.HeaderSize, data.LongData.Length * 8);
 140            }
 0141            else if (data.IntData.Length > 0)
 142            {
 0143                Buffer.BlockCopy(data.IntData, 0, result, FPLutHeader.HeaderSize, data.IntData.Length * 4);
 144            }
 145
 0146            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            {
 0158                if (!File.Exists(filePath))
 0159                    return false;
 160
 0161                byte[] data = File.ReadAllBytes(filePath);
 0162                if (data.Length < FPLutHeader.HeaderSize)
 0163                    return false;
 164
 0165                uint magic = BitConverter.ToUInt32(data, 0);
 0166                return magic == FPLutHeader.Magic;
 167            }
 0168            catch
 169            {
 0170                return false;
 171            }
 0172        }
 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        {
 0182            byte[] headerData = new byte[FPLutHeader.HeaderSize];
 0183            using (var fs = File.OpenRead(filePath))
 184            {
 0185                fs.Read(headerData, 0, FPLutHeader.HeaderSize);
 0186            }
 0187            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        {
 0198            Directory.CreateDirectory(targetDir);
 199
 0200            foreach (string file in Directory.GetFiles(sourceDir, "*" + FileExtension))
 201            {
 0202                string fileName = Path.GetFileName(file);
 0203                string targetPath = Path.Combine(targetDir, fileName);
 204
 205                try
 206                {
 0207                    var data = Load(file);
 0208                    Save(targetPath, data, targetVersion);
 0209                }
 0210                catch (Exception ex)
 211                {
 0212                    throw new InvalidOperationException($"Failed to convert {fileName}: {ex.Message}", ex);
 213                }
 214            }
 0215        }
 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        {
 284            uint current = FPLutHeader.CurrentVersion;
 285            uint min = FPLutHeader.MinCompatibleVersion;
 286
 287            if (fileVersion == current)
 288                return CompatibilityResult.ExactMatch;
 289
 290            if (fileVersion > current)
 291                return CompatibilityResult.NewerVersion;
 292
 293            if (fileVersion >= min)
 294                return CompatibilityResult.BackwardCompatible;
 295
 296            return CompatibilityResult.Incompatible;
 297        }
 298
 299        /// <summary>
 300        /// 获取版本变更说明
 301        /// </summary>
 302        public static string GetVersionChanges(uint fromVersion, uint toVersion)
 303        {
 304            // 版本变更日志
 305            if (fromVersion == 0x00010000 && toVersion == 0x00010000)
 306                return "Same version (1.0.0)";
 307
 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}