|
| 1 | +// Copyright (c) 2023-2025 Koji Hasegawa. |
| 2 | +// This software is released under the MIT License. |
| 3 | + |
| 4 | +#if ENABLE_FLIP_BINDING |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using FlipBinding.CSharp; |
| 8 | +using TestHelper.RuntimeInternals; |
| 9 | +using UnityEngine; |
| 10 | + |
| 11 | +namespace TestHelper.Comparers |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Compares two <see cref="Texture2D"/> using <see href="https://github.com/NVlabs/flip">FLIP</see>. |
| 15 | + /// </summary> |
| 16 | + public class FlipTexture2dEqualityComparer : IComparer<Texture2D> |
| 17 | + { |
| 18 | + private const float DefaultMeanErrorTolerance = 1E-05f; |
| 19 | + private const float DefaultPpd = 67.0206451f; |
| 20 | + |
| 21 | + // Comparison parameters |
| 22 | + private readonly float _meanErrorTolerance; |
| 23 | + private readonly string _errorMapOutputDirectory; |
| 24 | + private readonly string _errorMapOutputFilename; |
| 25 | + private readonly bool _namespaceToDirectory; |
| 26 | + |
| 27 | + // FLIP parameters |
| 28 | + private readonly bool _useHdr; |
| 29 | + private readonly float _ppd; |
| 30 | + private readonly Tonemapper _tonemapper; |
| 31 | + private readonly float _startExposure; |
| 32 | + private readonly float _stopExposure; |
| 33 | + private readonly int _numExposures; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Constructor. |
| 37 | + /// </summary> |
| 38 | + /// <param name="meanErrorTolerance">Mean FLIP error value in the range [0,1]. Lower values indicate more similar images.</param> |
| 39 | + /// <param name="errorMapOutputDirectory">Directory to output error map. |
| 40 | + /// If omitted, the directory specified by command line argument <c>-testHelperScreenshotDirectory</c> is used. |
| 41 | + /// If the command line argument is also omitted, <c>Application.persistentDataPath</c> + "/TestHelper/Screenshots/" is used.</param> |
| 42 | + /// <param name="errorMapOutputFilename">Filename to output error map. |
| 43 | + /// If omitted, default filename is <c>TestContext.Test.Name</c> + ".diff.png".</param> |
| 44 | + /// <param name="namespaceToDirectory">Insert subdirectory named from test namespace if true.</param> |
| 45 | + /// <param name="useHdr">Whether to use HDR mode. LDR: values in [0,1], HDR: values can exceed [0,1].</param> |
| 46 | + /// <param name="ppd">Pixels per degree. Default is 67 (4K display at 0.7m viewing distance). You can calculate PPD with <see cref="Flip.CalculatePpd"/></param> |
| 47 | + /// <param name="tonemapper">Tonemapper for HDR-FLIP processing. Ignored when useHdr is false.</param> |
| 48 | + /// <param name="startExposure">Start exposure for HDR-FLIP. Use float.PositiveInfinity for auto-calculation.</param> |
| 49 | + /// <param name="stopExposure">Stop exposure for HDR-FLIP. Use float.PositiveInfinity for auto-calculation.</param> |
| 50 | + /// <param name="numExposures">Number of exposures for HDR-FLIP. Use -1 for auto-calculation.</param> |
| 51 | + public FlipTexture2dEqualityComparer( |
| 52 | + float meanErrorTolerance = DefaultMeanErrorTolerance, |
| 53 | + string errorMapOutputDirectory = null, |
| 54 | + string errorMapOutputFilename = null, |
| 55 | + bool namespaceToDirectory = false, |
| 56 | + bool useHdr = false, |
| 57 | + float ppd = DefaultPpd, |
| 58 | + Tonemapper tonemapper = Tonemapper.Aces, |
| 59 | + float startExposure = float.PositiveInfinity, |
| 60 | + float stopExposure = float.PositiveInfinity, |
| 61 | + int numExposures = -1) |
| 62 | + { |
| 63 | + _meanErrorTolerance = meanErrorTolerance; |
| 64 | + _errorMapOutputDirectory = errorMapOutputDirectory; |
| 65 | + _errorMapOutputFilename = errorMapOutputFilename; |
| 66 | + _namespaceToDirectory = namespaceToDirectory; |
| 67 | + _useHdr = useHdr; |
| 68 | + _ppd = ppd; |
| 69 | + _tonemapper = tonemapper; |
| 70 | + _startExposure = startExposure; |
| 71 | + _stopExposure = stopExposure; |
| 72 | + _numExposures = numExposures; |
| 73 | + } |
| 74 | + |
| 75 | + /// <inheritdoc/> |
| 76 | + public int Compare(Texture2D x, Texture2D y) |
| 77 | + { |
| 78 | + if (x!.width != y!.width || x.height != y.height) |
| 79 | + { |
| 80 | + Debug.Log("Texture sizes are different.\n" + |
| 81 | + $" Expected: {x.width}x{x.height}\n" + |
| 82 | + $" But was: {y.width}x{y.height}\n"); |
| 83 | + return -1; |
| 84 | + } |
| 85 | + |
| 86 | + var referenceRgb = ConvertToRgbArray(y); |
| 87 | + var testRgb = ConvertToRgbArray(x); |
| 88 | + |
| 89 | + var result = Flip.Evaluate( |
| 90 | + referenceRgb, |
| 91 | + testRgb, |
| 92 | + y.width, |
| 93 | + y.height, |
| 94 | + _useHdr, |
| 95 | + _ppd, |
| 96 | + _tonemapper, |
| 97 | + _startExposure, |
| 98 | + _stopExposure, |
| 99 | + _numExposures, |
| 100 | + applyMagmaMap: true); |
| 101 | + |
| 102 | + if (result.MeanError <= _meanErrorTolerance) |
| 103 | + { |
| 104 | + return 0; |
| 105 | + } |
| 106 | + |
| 107 | + Debug.Log($"Mean FLIP error value: {result.MeanError}\n" + |
| 108 | + $"Exceeds the specified tolerance {_meanErrorTolerance}"); |
| 109 | + SaveErrorMap(result); |
| 110 | + return -1; |
| 111 | + } |
| 112 | + |
| 113 | + private static float[] ConvertToRgbArray(Texture2D texture) |
| 114 | + { |
| 115 | + var pixels = texture.GetPixels(); |
| 116 | + var rgb = new float[pixels.Length * 3]; |
| 117 | + |
| 118 | + for (var i = 0; i < pixels.Length; i++) |
| 119 | + { |
| 120 | + var linear = pixels[i].linear; |
| 121 | + rgb[i * 3] = linear.r; |
| 122 | + rgb[i * 3 + 1] = linear.g; |
| 123 | + rgb[i * 3 + 2] = linear.b; |
| 124 | + } |
| 125 | + |
| 126 | + return rgb; |
| 127 | + } |
| 128 | + |
| 129 | + private void SaveErrorMap(FlipResult result) |
| 130 | + { |
| 131 | + var path = GetErrorMapPath(); |
| 132 | + var directory = Path.GetDirectoryName(path); |
| 133 | + if (!string.IsNullOrEmpty(directory)) |
| 134 | + { |
| 135 | + Directory.CreateDirectory(directory); |
| 136 | + } |
| 137 | + |
| 138 | + var texture = new Texture2D(result.Width, result.Height, TextureFormat.RGB24, false); |
| 139 | + var colors = new Color32[result.Width * result.Height]; |
| 140 | + |
| 141 | + for (var i = 0; i < colors.Length; i++) |
| 142 | + { |
| 143 | + var (r, g, b) = result.GetPixelRgb(i % result.Width, i / result.Width); |
| 144 | + colors[i] = new Color(r, g, b); |
| 145 | + } |
| 146 | + |
| 147 | + texture.SetPixels32(colors); |
| 148 | + texture.Apply(); |
| 149 | + |
| 150 | + var bytes = texture.EncodeToPNG(); |
| 151 | + File.WriteAllBytes(path, bytes); |
| 152 | + |
| 153 | + Debug.Log($"Error map saved to: {path}"); |
| 154 | + Object.Destroy(texture); |
| 155 | + } |
| 156 | + |
| 157 | + private string GetErrorMapPath() |
| 158 | + { |
| 159 | + string directory; |
| 160 | + if (_errorMapOutputDirectory != null) |
| 161 | + { |
| 162 | + directory = Path.GetFullPath(_errorMapOutputDirectory); |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + directory = CommandLineArgs.GetScreenshotDirectory(); |
| 167 | + } |
| 168 | + |
| 169 | + string path; |
| 170 | + if (_errorMapOutputFilename == null) |
| 171 | + { |
| 172 | + path = PathHelper.CreateFilePath( |
| 173 | + baseDirectory: directory, |
| 174 | + extension: ".diff.png", |
| 175 | + namespaceToDirectory: _namespaceToDirectory); |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + path = Path.Combine(directory, _errorMapOutputFilename); |
| 180 | + if (!path.EndsWith(".png")) |
| 181 | + { |
| 182 | + path += ".png"; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return path; |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | +#endif |
0 commit comments