AliceVision
Photogrammetric Computer Vision Framework
dcp.hpp
1 #pragma once
2 
3 #include <map>
4 #include <vector>
5 #include <array>
6 #include <memory>
7 #include <string>
8 
9 #include <OpenImageIO/imagebuf.h>
10 #include <aliceVision/image/Image.hpp>
11 
12 namespace aliceVision {
13 namespace image {
14 
21 constexpr double xyz_sRGB[3][3] = {{0.4360747, 0.3850649, 0.1430804}, {0.2225045, 0.7168786, 0.0606169}, {0.0139322, 0.0971045, 0.7141733}};
22 constexpr double sRGB_xyz[3][3] = {{3.1338561, -1.6168667, -0.4906146}, {-0.9787684, 1.9161415, 0.0334540}, {0.0719453, -0.2289914, 1.4052427}};
23 
24 constexpr double xyz_prophoto[3][3] = {{0.7976749, 0.1351917, 0.0313534}, {0.2880402, 0.7118741, 0.0000857}, {0.0000000, 0.0000000, 0.8252100}};
25 constexpr double prophoto_xyz[3][3] = {{1.3459433, -0.2556075, -0.0511118}, {-0.5445989, 1.5081673, 0.0205351}, {0.0000000, 0.0000000, 1.2118128}};
26 
27 constexpr double xyz_AdobeRGB[3][3] = {{0.6097559, 0.2052401, 0.1492240}, {0.3111242, 0.6256560, 0.0632197}, {0.0194811, 0.0608902, 0.7448387}};
28 constexpr double AdobeRGB_xyz[3][3] = {{1.9624274, -0.6105343, -0.3413404}, {-0.9787684, 1.9161415, 0.0334540}, {0.0286869, -0.1406752, 1.3487655}};
30 
31 enum class LightSource
32 {
33  UNKNOWN = 0,
34  DAYLIGHT = 1,
35  FLUORESCENT = 2,
36  TUNGSTEN = 3,
37  FLASH = 4,
38  FINE_WEATHER = 9,
39  CLOUDY_WEATHER = 10,
40  SHADE = 11,
41  DAYLIGHT_FLUORESCENT = 12, // D 5700 - 7100K
42  DAYWHITE_FLUORESCENT = 13, // N 4600 - 5500K
43  COOL_WHITE_FLUORESCENT = 14, // W 3800 - 4500K
44  WHITE_FLUORESCENT = 15, // WW 3250 - 3800K
45  WARM_WHITE_FLUORESCENT = 16, // L 2600 - 3250K
46  STANDARD_LIGHT_A = 17,
47  STANDARD_LIGHT_B = 18,
48  STANDARD_LIGHT_C = 19,
49  D55 = 20,
50  D65 = 21,
51  D75 = 22,
52  D50 = 23,
53  ISO_STUDIO_TUNGSTEN = 24,
54  OTHER = 255
55 };
56 
57 double calibrationIlluminantToTemperature(LightSource light);
58 
62 class SplineToneCurve final
63 {
64  public:
65  SplineToneCurve() = default;
66  ~SplineToneCurve() = default;
67 
68  void Set(const std::vector<double>& v_xy);
69  void Apply(float& ir, float& ig, float& ib) const;
70  float operator[](const float idx) const { return getval(idx); }
71 
72  private:
73  void RGBTone(float& maxval, float& medval, float& minval) const;
74  float getval(const float idx) const;
75 
76  std::vector<float> ffffToneCurve = std::vector<float>(65536, 0.f);
77 };
78 
83 {
84  bool apply_hue_shift = true;
85  bool apply_baseline_exposure_offset = true;
86  bool use_tone_curve = true;
87  bool apply_look_table = true;
88  std::string working_space = "sRGB";
89 };
90 
95 {
96  std::string filename = "";
97  std::string profileCalibrationSignature = "";
98 
99  bool has_color_matrix_1 = false;
100  bool has_color_matrix_2 = false;
101  bool has_camera_calibration_1 = false;
102  bool has_camera_calibration_2 = false;
103  bool has_forward_matrix_1 = false;
104  bool has_forward_matrix_2 = false;
105  bool has_look_table = false;
106  bool has_hue_sat_map = false;
107  bool has_tone_curve = false;
108  bool has_baseline_exposure_offset = false;
109 
110  short light_source_1 = 0;
111  short light_source_2 = 0;
112  double temperature_1 = 0.0;
113  double temperature_2 = 0.0;
114 };
115 
121 class DCPProfile final
122 {
123  public:
124  DCPProfileInfo info;
125 
126  using Triple = std::array<double, 3>;
127  using Matrix = std::array<Triple, 3>;
128 
129  DCPProfile();
130 
135  explicit DCPProfile(const std::string& filename);
136  ~DCPProfile();
137 
142  void Load(const std::string& filename);
143 
148  void Load(const std::map<std::string, std::string>& metadata);
149 
155  void getMatrices(const std::string& type, std::vector<Matrix>& v_Mat) const;
156 
162  void getMatricesAsStrings(const std::string& type, std::vector<std::string>& v_strMat) const;
163 
169  void setMatrices(const std::string& type, std::vector<Matrix>& v_Mat);
170 
176  void setMatricesFromStrings(const std::string& type, std::vector<std::string>& v_strMat);
177 
186  void applyLinear(OIIO::ImageBuf& image,
187  const Triple& neutral,
188  double& cct,
189  const bool sourceIsRaw = false,
190  const bool useColorMatrixOnly = true) const;
191 
201  const Triple& neutral,
202  double& cct,
203  const bool sourceIsRaw = false,
204  const bool useColorMatrixOnly = false) const;
205 
211  void apply(OIIO::ImageBuf& image, const DCPProfileApplyParams& params);
217  void apply(float* rgb, const DCPProfileApplyParams& params) const;
218 
225  void getColorTemperatureAndTintFromNeutral(const Triple& neutral, double& cct, double& tint) const;
226 
227  private:
228  struct HsbModify
229  {
230  float hue_shift;
231  float sat_scale;
232  float val_scale;
233  };
234 
235  struct HsdTableInfo
236  {
237  int hue_divisions;
238  int sat_divisions;
239  int val_divisions;
240  int hue_step;
241  int val_step;
242  unsigned int array_count;
243  bool srgb_gamma;
244  struct
245  {
246  float h_scale;
247  float s_scale;
248  float v_scale;
249  int max_hue_index0;
250  int max_sat_index0;
251  int max_val_index0;
252  int hue_step;
253  int val_step;
254  } pc;
255  };
256 
257  void hsdApply(const HsdTableInfo& table_info, const std::vector<HsbModify>& table_base, float& h, float& s, float& v) const;
258 
259  Matrix matMult(const Matrix& A, const Matrix& B) const;
260  Triple matMult(const Matrix& M, const Triple& V) const;
261  Matrix matInv(const Matrix& M) const;
262 
263  Matrix getInterpolatedMatrix(const double cct, const std::string& type) const;
264  void getChromaticityCoordinatesFromXyz(const Triple& xyz, double& x, double& y) const;
265  Triple getXyzFromChromaticityCoordinates(const double x, const double y) const;
266  Triple getXyzFromTemperature(const double cct, const double tint = 0.f) const;
267  void setChromaticityCoordinates(const double x, const double y, double& cct, double& tint) const;
268  void getChromaticityCoordinates(const double cct, const double tint, double& x, double& y) const;
269  void getChromaticityCoordinatesFromCameraNeutral(const Matrix& analogBalance, const Triple& asShotNeutral, double& x, double& y) const;
270  Matrix getChromaticAdaptationMatrix(const Triple& xyzSource, const Triple& xyzTarget) const;
271  Matrix getCameraToXyzD50Matrix(const double x, const double y) const;
272  Matrix getCameraToSrgbLinearMatrix(const double x, const double y) const;
273  Matrix getCameraToACES2065Matrix(const Triple& asShotNeutral,
274  double& cct,
275  const bool sourceIsRaw = false,
276  const bool useColorMatrixOnly = false) const;
277 
278  Matrix ws_sRGB; // working color space to sRGB
279  Matrix sRGB_ws; // sRGB to working color space
280  Matrix color_matrix_1; // Color matrix for illuminant 1
281  Matrix color_matrix_2; // Color matrix for illuminant 2
282  Matrix camera_calibration_1; // Calibration matrix for illuminant 1
283  Matrix camera_calibration_2; // Calibration matrix for illuminant 2
284  Matrix analogBalance;
285  Matrix forward_matrix_1; // white balanced raw to xyzD50 for illumimant 1
286  Matrix forward_matrix_2; // white balanced raw to xyzD50 for illumimant 2
287  double baseline_exposure_offset;
288  std::vector<HsbModify> deltas_1; // Basic Hue Sat update table for illumimant 1
289  std::vector<HsbModify> deltas_2; // Basic Hue Sat update table for illumimant 2
290  std::vector<HsbModify> look_table; // Hue Sat update table for look modification
291  HsdTableInfo delta_info; // Information for basic Hue Sat updates
292  HsdTableInfo look_info; // Information for look modification
293 
294  SplineToneCurve AS_tone_curve; // Adobe standard tone curve
295 
296  SplineToneCurve gammatab_srgb;
297  SplineToneCurve igammatab_srgb;
298 };
299 
303 class DCPDatabase final
304 {
305  public:
306  DCPDatabase() = default;
307  DCPDatabase(const std::string& databaseDirPath);
308 
309  ~DCPDatabase() = default;
310 
318  int load(const std::string& databaseDirPath, bool force = false);
319 
323  void clear();
324 
329  inline bool empty() { return dcpFilenamesList.empty(); }
330 
335  inline size_t size() { return dcpFilenamesList.size(); }
336 
344  void add_or_replace(DCPProfile& dcpProf, const std::string& make, const std::string& model);
345 
355  bool retrieveDcpForCamera(const std::string& make, const std::string& model, DCPProfile& dcpProf);
356 
357  private:
358  std::string folderName;
359 
360  std::vector<std::string> dcpFilenamesList;
361 
362  std::map<std::string, DCPProfile> dcpStore;
363 };
364 
365 } // namespace image
366 } // namespace aliceVision
aliceVision::image::SplineToneCurve
SplineToneCurve represents a tone curve that can be embedded within a DCP color profile.
Definition: dcp.hpp:62
aliceVision::image::DCPProfile::getColorTemperatureAndTintFromNeutral
void getColorTemperatureAndTintFromNeutral(const Triple &neutral, double &cct, double &tint) const
compute color temperature and tint from neutral param[in] neutral The neutral triplet value param[out...
Definition: dcp.cpp:2361
aliceVision::image::DCPDatabase::add_or_replace
void add_or_replace(DCPProfile &dcpProf, const std::string &make, const std::string &model)
add_or_replace adds or replaces an existing DCP profile in the cache. Update the DCP file list with d...
Definition: dcp.cpp:2441
aliceVision::image::DCPProfile::apply
void apply(OIIO::ImageBuf &image, const DCPProfileApplyParams &params)
apply applies the non linear part of a DCP profile on an OIIO image buffer param[in] image The OIIO i...
Definition: dcp.cpp:1305
aliceVision::image::DCPDatabase::retrieveDcpForCamera
bool retrieveDcpForCamera(const std::string &make, const std::string &model, DCPProfile &dcpProf)
retrieveDcpForCamera searches for a DCP profile in the database for a given camera....
Definition: dcp.cpp:2407
aliceVision::image::DCPProfileApplyParams
Aggregate all DCP color profile application options (non linear part)
Definition: dcp.hpp:82
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::image::DCPProfile::getMatricesAsStrings
void getMatricesAsStrings(const std::string &type, std::vector< std::string > &v_strMat) const
getMatricesAsStrings gets some matrices contained in the profile in a string format (one string per m...
Definition: dcp.cpp:2223
aliceVision::image::Image
Definition: ImageDescriber_AKAZE_OCV.hpp:21
aliceVision::image::DCPDatabase::load
int load(const std::string &databaseDirPath, bool force=false)
load stores in a file list all the valid dcp filenames found in a folder (including subfolders)....
Definition: dcp.cpp:2371
aliceVision::image::DCPDatabase
DCPDatabase manages DCP profiles loading and caching.
Definition: dcp.hpp:303
aliceVision::image::DCPProfile::setMatricesFromStrings
void setMatricesFromStrings(const std::string &type, std::vector< std::string > &v_strMat)
setMatricesFromStrings sets some matrices contained in the profile from strings (one string per matri...
Definition: dcp.cpp:2288
aliceVision::image::DCPProfile::Load
void Load(const std::string &filename)
DCPProfile loader.
Definition: dcp.cpp:828
aliceVision::image::DCPProfile::getMatrices
void getMatrices(const std::string &type, std::vector< Matrix > &v_Mat) const
getMatrices gets some matrices contained in the profile param[in] type The matrices to get,...
Definition: dcp.cpp:2196
aliceVision::image::DCPProfileInfo
DCPProfileInfo contains information about matrices, table and curves contained in the profile.
Definition: dcp.hpp:94
aliceVision::image::DCPProfile::setMatrices
void setMatrices(const std::string &type, std::vector< Matrix > &v_Mat)
setMatrices sets some matrices contained in the profile param[in] type The matrices to set,...
Definition: dcp.cpp:2239
aliceVision::image::DCPProfile
DCPProfile contains a Dng Color Profile as specified by Adobe DNG specification can be found here: ht...
Definition: dcp.hpp:121
aliceVision::rgb
Definition: Rgb.hpp:11
aliceVision::image::DCPDatabase::size
size_t size()
Size of the database. return number of known profiles.
Definition: dcp.hpp:335
aliceVision::image::DCPProfile::applyLinear
void applyLinear(OIIO::ImageBuf &image, const Triple &neutral, double &cct, const bool sourceIsRaw=false, const bool useColorMatrixOnly=true) const
applyLinear applies the linear part of a DCP profile on an OIIO image buffer param[in] image The OIIO...
Definition: dcp.cpp:2306
aliceVision::image::DCPDatabase::empty
bool empty()
Check if the database is empty. return True if empty.
Definition: dcp.hpp:329
aliceVision::image::DCPDatabase::clear
void clear()
clear clears the cache and the DCP file list.
Definition: dcp.cpp:2400