Skip to content

Commit 927bf4c

Browse files
authored
Usage examples (#176)
* add usage examples Signed-off-by: Anton Dukhovnikov <[email protected]>
1 parent 9e1388f commit 927bf4c

File tree

3 files changed

+292
-0
lines changed

3 files changed

+292
-0
lines changed

tests/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,36 @@ cmake_minimum_required(VERSION 3.12)
22

33
################################################################################
44

5+
add_executable (
6+
Test_core_usage
7+
core_usage.cpp
8+
)
9+
10+
target_link_libraries(
11+
Test_core_usage
12+
PUBLIC
13+
${RAWTOACES_CORE_LIB}
14+
OpenImageIO::OpenImageIO
15+
)
16+
17+
add_test ( NAME Test_core_usage COMMAND Test_core_usage )
18+
19+
################################################################################
20+
add_executable (
21+
Test_util_usage
22+
util_usage.cpp
23+
)
24+
25+
target_link_libraries(
26+
Test_util_usage
27+
PUBLIC
28+
${RAWTOACES_UTIL_LIB}
29+
OpenImageIO::OpenImageIO
30+
)
31+
32+
add_test ( NAME Test_util_usage COMMAND Test_util_usage )
33+
34+
################################################################################
535
add_executable (
636
Test_SpectralData
737
test_SpectralData.cpp

tests/core_usage.cpp

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright Contributors to the rawtoaces Project.
3+
4+
#include <filesystem>
5+
#include <OpenImageIO/unittest.h>
6+
#include <rawtoaces/rawtoaces_core.h>
7+
8+
// This file contains some usage examples of the core library.
9+
// It has only very little unit test functionality to keep the code clean.
10+
11+
// A path to the rawtoaces database,
12+
// typically in /usr/local/share/rawtoaces/data.
13+
// We just use the copy in the current repo.
14+
#define DATA_PATH "../_deps/rawtoaces_data-src/data/"
15+
16+
/// A helper function to configure the spectral solver. Typically, only the camera
17+
/// data file path, and its make and model would change per image.
18+
/// Every other bit of data should be the same.
19+
void configure_spectral_solver( rta::core::SpectralSolver &solver )
20+
{
21+
const std::string camera_make = "nikon";
22+
const std::string camera_model = "d200";
23+
24+
// Spectral curves to use.
25+
const std::string camera_path = "camera/nikon_d200_380_780_5.json";
26+
const std::string observer_path = "cmf/cmf_1931.json";
27+
const std::string training_path = "training/training_spectral.json";
28+
29+
std::string absolute_camera_path =
30+
std::filesystem::absolute( DATA_PATH + camera_path ).string();
31+
std::string absolute_observer_path =
32+
std::filesystem::absolute( DATA_PATH + observer_path ).string();
33+
std::string absolute_training_path =
34+
std::filesystem::absolute( DATA_PATH + training_path ).string();
35+
36+
// Configure the solver.
37+
solver.load_camera( absolute_camera_path, camera_make, camera_model );
38+
solver.load_observer( absolute_observer_path );
39+
solver.load_training_data( absolute_training_path );
40+
}
41+
42+
/// Test the spectral solver, using the white balance weights either from
43+
/// the image file's metadata, or custom weights.
44+
void test_SpectralSolver_multipliers()
45+
{
46+
// Input parameters.
47+
const std::vector<double> white_balance = { 1.79488, 1, 1.39779 };
48+
49+
// Configure the solver.
50+
rta::core::SpectralSolver solver;
51+
configure_spectral_solver( solver );
52+
53+
// Pass an empty list of illuminant files. This means the solver will not
54+
// use any custom illuminant files, and will try to find the best suitable
55+
// illuminant as either a daylight or blackbody illuminant.
56+
solver.load_illuminant( {} );
57+
58+
// Find the best suitable illuminant.
59+
solver.find_best_illuminant( white_balance, 0 );
60+
61+
// Solve the transform matrix.
62+
solver.calculate_IDT_matrix();
63+
64+
// Get the solved matrix.
65+
const std::vector<std::vector<double>> &solved_IDT =
66+
solver.get_IDT_matrix();
67+
68+
// Check the results.
69+
const std::vector<std::vector<double>> true_IDT = {
70+
{ 0.713439, 0.221480, 0.065082 },
71+
{ 0.064818, 1.076460, -0.141278 },
72+
{ 0.039568, -0.140956, 1.101387 }
73+
};
74+
75+
for ( size_t row = 0; row < 3; row++ )
76+
for ( size_t col = 0; col < 3; col++ )
77+
OIIO_CHECK_EQUAL_THRESH(
78+
solved_IDT[row][col], true_IDT[row][col], 1e-5 );
79+
};
80+
81+
/// Test the spectral solver, white-balancing to a specific illuminant.
82+
void test_SpectralSolver_illuminant()
83+
{
84+
// Input parameters.
85+
const std::string illuminant = "d55";
86+
87+
// Configure the solver.
88+
rta::core::SpectralSolver solver;
89+
configure_spectral_solver( solver );
90+
91+
// Pass an empty list of illuminant files. We don't need any custom
92+
// illuminants. Also specify the illuminant we want to use.
93+
solver.load_illuminant( {}, illuminant );
94+
95+
// Select the provided illuminant.
96+
solver.select_illuminant( illuminant, 0 );
97+
98+
// Get the solved WB weights.
99+
const std::vector<double> &solved_WB = solver.get_WB_multipliers();
100+
101+
// Solve the transform matrix.
102+
solver.calculate_IDT_matrix();
103+
104+
// Get the solved matrix.
105+
const std::vector<std::vector<double>> &solved_IDT =
106+
solver.get_IDT_matrix();
107+
108+
// Check the results.
109+
const std::vector<double> true_WB = { 1.79488, 1, 1.39779 };
110+
for ( size_t row = 0; row < 3; row++ )
111+
OIIO_CHECK_EQUAL_THRESH( solved_WB[row], true_WB[row], 1e-5 );
112+
113+
const std::vector<std::vector<double>> true_IDT = {
114+
{ 0.713428, 0.221535, 0.065037 },
115+
{ 0.064829, 1.076544, -0.141372 },
116+
{ 0.039572, -0.140962, 1.101390 }
117+
};
118+
for ( size_t row = 0; row < 3; row++ )
119+
for ( size_t col = 0; col < 3; col++ )
120+
OIIO_CHECK_EQUAL_THRESH(
121+
solved_IDT[row][col], true_IDT[row][col], 1e-5 );
122+
};
123+
124+
/// A helper function to init the metadata object.
125+
/// Normally the values come from a DNG file metadata.
126+
void init_metadata( rta::core::Metadata &metadata )
127+
{
128+
metadata.baseline_exposure = 2.4;
129+
130+
metadata.neutral_RGB = { 0.6289999865031245, 1, 0.79040003045288199 };
131+
132+
metadata.calibration[0].illuminant = 17;
133+
metadata.calibration[1].illuminant = 21;
134+
135+
metadata.calibration[0].XYZ_to_RGB_matrix = {
136+
1.3119699954986572, -0.49678999185562134, 0.011559999547898769,
137+
-0.41723001003265381, 1.4423700571060181, 0.045279998332262039,
138+
0.067230001091957092, 0.21709999442100525, 0.72650998830795288
139+
};
140+
141+
metadata.calibration[1].XYZ_to_RGB_matrix = {
142+
1.0088499784469604, -0.27351000905036926, -0.082580000162124634,
143+
-0.48996999859809875, 1.3444099426269531, 0.11174000054597855,
144+
-0.064060002565383911, 0.32997000217437744, 0.5391700267791748
145+
};
146+
}
147+
148+
/// Test the metadata solver.
149+
void test_MetadataSolver()
150+
{
151+
// Init the metadata.
152+
rta::core::Metadata metadata;
153+
init_metadata( metadata );
154+
155+
// Init the solver.
156+
rta::core::MetadataSolver solver( metadata );
157+
158+
// Solve the transform matrix.
159+
const std::vector<std::vector<double>> solved_IDT =
160+
solver.calculate_IDT_matrix();
161+
162+
// Check the results.
163+
const std::vector<std::vector<double>> true_IDT = {
164+
{ 1.053647, 0.003904, 0.004908 },
165+
{ -0.489956, 1.361479, 0.102084 },
166+
{ -0.002450, 0.006050, 1.013916 }
167+
};
168+
for ( size_t row = 0; row < 3; row++ )
169+
for ( size_t col = 0; col < 3; col++ )
170+
OIIO_CHECK_EQUAL_THRESH(
171+
solved_IDT[row][col], true_IDT[row][col], 1e-5 );
172+
};
173+
174+
int main( int, char ** )
175+
{
176+
test_SpectralSolver_multipliers();
177+
test_SpectralSolver_illuminant();
178+
test_MetadataSolver();
179+
180+
return unit_test_failures;
181+
}

tests/util_usage.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright Contributors to the rawtoaces Project.
3+
4+
#include <filesystem>
5+
#include <OpenImageIO/unittest.h>
6+
#include <rawtoaces/image_converter.h>
7+
8+
// This file contains some usage examples of the util library.
9+
// It has only very little unit test functionality to keep the code clean.
10+
11+
/// Test the image converter using command line parameters for intialisation.
12+
void test_ImageConverter_arguments()
13+
{
14+
// This test fails on CI runners having an old version of OIIO.
15+
if ( OIIO::openimageio_version() < 30000 )
16+
return;
17+
18+
const char *image_path =
19+
"../../../tests/materials/blackmagic_cinema_camera_cinemadng.dng";
20+
std::string absolute_image_path =
21+
std::filesystem::absolute( image_path ).string();
22+
23+
// Input parameters.
24+
const char *argv[] = { "DUMMY PROGRAM PATH",
25+
"--wb-method",
26+
"metadata",
27+
"--mat-method",
28+
"metadata" };
29+
30+
const size_t argc = sizeof( argv ) / sizeof( argv[0] );
31+
32+
// Parse the command line parameters and configure the converter.
33+
rta::util::ImageConverter converter;
34+
OIIO::ArgParse arg_parser;
35+
converter.init_parser( arg_parser );
36+
arg_parser.parse_args( argc, argv );
37+
converter.parse_parameters( arg_parser );
38+
39+
// Process an image.
40+
bool result = converter.process_image( absolute_image_path );
41+
42+
// Check the result.
43+
OIIO_CHECK_ASSERT( result );
44+
};
45+
46+
/// Test the image converter, initialising the settings struct directly.
47+
void test_ImageConverter_settings()
48+
{
49+
// This test fails on CI runners having an old version of OIIO.
50+
if ( OIIO::openimageio_version() < 30000 )
51+
return;
52+
53+
const char *image_path =
54+
"../../tests/materials/blackmagic_cinema_camera_cinemadng.dng";
55+
std::string absolute_image_path =
56+
std::filesystem::absolute( image_path ).string();
57+
58+
// Configure the converter.
59+
rta::util::ImageConverter converter;
60+
converter.settings.wbMethod =
61+
rta::util::ImageConverter::Settings::WBMethod::Metadata;
62+
converter.settings.matrixMethod =
63+
rta::util::ImageConverter::Settings::MatrixMethod::Metadata;
64+
65+
// Process an image.
66+
bool result = converter.process_image( absolute_image_path );
67+
68+
// Check the result.
69+
OIIO_CHECK_ASSERT( result );
70+
};
71+
72+
int main( int, char ** )
73+
{
74+
// Only run on the linux CI runners.
75+
#ifdef __LINUX__
76+
test_ImageConverter_arguments();
77+
test_ImageConverter_settings();
78+
#endif
79+
80+
return unit_test_failures;
81+
}

0 commit comments

Comments
 (0)