Debian Codegraph @

tools/cmgen/tests/test_cmgen.cpp

raw
1 /*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <image/ColorTransform.h>
18 #include <image/ImageOps.h>
19 #include <image/ImageSampler.h>
20 #include <image/LinearImage.h>
21
22 #include <imageio/ImageDecoder.h>
23 #include <imageio/ImageEncoder.h>
24 #include <imageio/ImageDiffer.h>
25
26 #include <gtest/gtest.h>
27
28 #include <utils/Panic.h>
29 #include <utils/Path.h>
30
31 #include <math/vec3.h>
32
33 #include <cstdlib>
34 #include <fstream>
35 #include <regex>
36 #include <string>
37 #include <sstream>
38 #include <streambuf>
39
40 using std::string;
41 using utils::Path;
42 using namespace filament::math;
43
44 using namespace image;
45
46 class CmgenTest : public testing::Test {};
47
48 static ComparisonMode g_comparisonMode;
49
50 static void checkFileExistence(const string& path) {
51 std::ifstream s(path.c_str(), std::ios::binary);
52 if (!s) {
53 std::cerr << "ERROR file does not exist: " << path << std::endl;
54 exit(1);
55 }
56 }
57
58 // Reads the entire content of the specified file
59 static string readFile(const Path& inputPath) {
60 std::ifstream t(inputPath);
61 string s;
62
63 // Pre-allocate the memory
64 t.seekg(0, std::ios::end);
65 s.reserve((size_t) t.tellg());
66 t.seekg(0, std::ios::beg);
67
68 // Copy the file content into the string
69 s.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
70 return s;
71 }
72
73 // This spawns cmgen, telling it to process the environment map located at "inputPath".
74 // The supplied parameters are passed to cmgen as is. The commandSuffix is added at
75 // the end of the command line; it is useful to redirect the output to a file for instance
76 static void launchTool(string inputPath,
77 const string& parameters = "", const string& commandSuffix = "") {
78 const string executableFolder = Path::getCurrentExecutable().getParent();
79 inputPath = Path::getCurrentDirectory() + inputPath;
80
81 std::cout << "Running cmgen on " << inputPath << std::endl;
82 checkFileExistence(inputPath);
83 string cmdline = executableFolder + "cmgen " + parameters +
84 " " + inputPath + " " + commandSuffix;
85 ASSERT_EQ(std::system(cmdline.c_str()), 0);
86 }
87
88 // This spawns cmgen, telling it to process the environment map located at "inputPath". It creates
89 // an output folder in the same location as the test executable, which lets us avoid polluting our
90 // local source tree with output files. The given "resultPath" points the specific newly-generated
91 // output image that we'd like to compare or update, and the "goldenPath" points to the golden image
92 // (which lives in our source tree).
93 static void processEnvMap(string inputPath, string resultPath, string goldenPath) {
94 const string executableFolder = Path::getCurrentExecutable().getParent();
95 resultPath = Path::getCurrentExecutable().getParent() + resultPath;
96 goldenPath = Path::getCurrentDirectory() + goldenPath;
97
98 launchTool(std::move(inputPath), "--quiet -f rgbm -x " + executableFolder);
99
100 std::cout << "Reading result image from " << resultPath << std::endl;
101 checkFileExistence(resultPath);
102 std::ifstream resultStream(resultPath.c_str(), std::ios::binary);
103 LinearImage resultImage = ImageDecoder::decode(resultStream, resultPath);
104 ASSERT_EQ(resultImage.isValid(), true);
105 ASSERT_EQ(resultImage.getChannels(), 4);
106 LinearImage resultLImage = toLinearFromRGBM(
107 reinterpret_cast<filament::math::float4 const*>(resultImage.getPixelRef()),
108 resultImage.getWidth(), resultImage.getHeight());
109
110 std::cout << "Golden image is at " << goldenPath << std::endl;
111 updateOrCompare(resultLImage, goldenPath, g_comparisonMode, 0.01f);
112 }
113
114 static void compareSh(const string& content, const string& regex,
115 const float3& match, float epsilon = 1e-5f) {
116 std::smatch smatch;
117 if (std::regex_search(content, smatch, std::regex(regex))) {
118 float3 sh(std::stof(smatch[1]), std::stof(smatch[2]), std::stof(smatch[3]));
119 ASSERT_TRUE(all(lessThan(abs(sh - match), float3{epsilon})));
120 }
121 }
122
123 TEST_F(CmgenTest, SphericalHarmonics) { // NOLINT
124 const string inputPath = "assets/environments/white_furnace/white_furnace.exr";
125 const string resultPath = Path::getCurrentExecutable().getParent() + "white_furnace_sh.txt";
126 launchTool(inputPath, "--quiet --sh=3 --sh-shader", "> " + resultPath);
127
128 string content = readFile(resultPath);
129 string vec3(R"(\(\s+([-+0-9.]+),\s+([-+0-9.]+),\s+([-+0-9.]+)\); // )");
130
131 compareSh(content, vec3 + "L00", float3{1.0f});
132 compareSh(content, vec3 + "L1-1", float3{0.0f});
133 compareSh(content, vec3 + "L10", float3{0.0f});
134 compareSh(content, vec3 + "L11", float3{0.0f});
135 compareSh(content, vec3 + "L2-2", float3{0.0f});
136 compareSh(content, vec3 + "L2-1", float3{0.0f});
137 compareSh(content, vec3 + "L20", float3{0.0f});
138 compareSh(content, vec3 + "L21", float3{0.0f});
139 compareSh(content, vec3 + "L22", float3{0.0f});
140 }
141
142 TEST_F(CmgenTest, HdrLatLong) { // NOLINT
143 const string inputPath = "assets/environments/white_furnace/white_furnace.exr";
144 const string resultPath = "white_furnace/nx.rgbm";
145 const string goldenPath = "tools/cmgen/tests/white_furnace_nx.rgbm";
146 processEnvMap(inputPath, resultPath, goldenPath);
147 }
148
149 TEST_F(CmgenTest, LdrCrossCube) { // NOLINT
150 const string inputPath = "tools/cmgen/tests/Footballfield/Footballfield.png";
151 const string resultPath = "Footballfield/m3_nx.rgbm";
152 const string goldenPath = "tools/cmgen/tests/Footballfield/m3_nx.rgbm";
153 processEnvMap(inputPath, resultPath, goldenPath);
154 }
155
156 int main(int argc, char** argv) {
157 ::testing::InitGoogleTest(&argc, argv);
158 if (argc != 2) {
159 std::cerr << "test_cmgen [compare|update]" << std::endl;
160 return 1;
161 }
162 string command(argv[1]);
163 if (command == "update") {
164 g_comparisonMode = ComparisonMode::UPDATE;
165 } else if (command == "compare") {
166 g_comparisonMode = ComparisonMode::COMPARE;
167 } else {
168 return 1;
169 }
170 return RUN_ALL_TESTS();
171 }