|
| 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 | +} |
0 commit comments