Skip to content

Commit

Permalink
[image] dpc: constness
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiencastan committed Feb 10, 2023
1 parent 44c8b51 commit 89b4acc
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 95 deletions.
90 changes: 45 additions & 45 deletions src/aliceVision/image/dcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace image {
using aliceVision::clamp;
namespace bfs = boost::filesystem;

double calibrationIlluminantToTemperature(LightSource light)
double calibrationIlluminantToTemperature(const LightSource light)
{
// These temperatures are those found in DNG SDK reference code
switch(light)
Expand Down Expand Up @@ -1580,14 +1580,14 @@ inline void DCPProfile::hsdApply(const HsdTableInfo& table_info, const std::vect
}
}

DCPProfile::Matrix DCPProfile::getInterpolatedMatrix(const double cct, const std::string& type)
DCPProfile::Matrix DCPProfile::getInterpolatedMatrix(const double cct, const std::string& type) const
{
double a = 1.e6 / info.temperature_1;
double b = 1.e6 / info.temperature_2;
double c = 1.e6 / cct;
const double a = 1.e6 / info.temperature_1;
const double b = 1.e6 / info.temperature_2;
const double c = 1.e6 / cct;

Matrix Ma = (type == "color") ? color_matrix_1 : ((type == "calib") ? camera_calibration_1 : forward_matrix_1);
Matrix Mb = (type == "color") ? color_matrix_2 : ((type == "calib") ? camera_calibration_2 : forward_matrix_2);
const Matrix Ma = (type == "color") ? color_matrix_1 : ((type == "calib") ? camera_calibration_1 : forward_matrix_1);
const Matrix Mb = (type == "color") ? color_matrix_2 : ((type == "calib") ? camera_calibration_2 : forward_matrix_2);
Matrix interpolatedMatrix;
for (int i = 0; i < 3; ++i)
{
Expand All @@ -1600,55 +1600,55 @@ DCPProfile::Matrix DCPProfile::getInterpolatedMatrix(const double cct, const std
return interpolatedMatrix;
}

DCPProfile::Matrix DCPProfile::matMult(const Matrix& A, const Matrix& B)
DCPProfile::Matrix DCPProfile::matMult(const Matrix& A, const Matrix& B) const
{
Matrix Res;
Matrix res;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
Res[i][j] = 0.f;
res[i][j] = 0.f;
for (int k = 0; k < 3; k++)
{
Res[i][j] += A[i][k] * B[k][j];
res[i][j] += A[i][k] * B[k][j];
}
}
}
return Res;
return res;
}
DCPProfile::Triple DCPProfile::matMult(const Matrix& M, const Triple& V)
DCPProfile::Triple DCPProfile::matMult(const Matrix& M, const Triple& V) const
{
Triple Res;
Triple res;
for (int i = 0; i < 3; ++i)
{
Res[i] = 0.f;
res[i] = 0.f;
for (int k = 0; k < 3; k++)
{
Res[i] += M[i][k] * V[k];
res[i] += M[i][k] * V[k];
}
}
return Res;
return res;
}
DCPProfile::Matrix DCPProfile::matInv(const Matrix& M)
DCPProfile::Matrix DCPProfile::matInv(const Matrix& M) const
{
Matrix Inv = M;
Matrix inv = M;

float det = M[0][0]*M[1][1]*M[2][2] + M[0][1]*M[1][2]*M[2][0] + M[0][2]*M[1][0]*M[2][1] - M[2][0]*M[1][1]*M[0][2] - M[1][0]*M[0][1]*M[2][2] - M[0][0]*M[2][1]*M[1][2];
const float det = M[0][0]*M[1][1]*M[2][2] + M[0][1]*M[1][2]*M[2][0] + M[0][2]*M[1][0]*M[2][1] - M[2][0]*M[1][1]*M[0][2] - M[1][0]*M[0][1]*M[2][2] - M[0][0]*M[2][1]*M[1][2];

if (det != 0.f)
{
Inv[0][0] = (M[1][1] * M[2][2] - M[2][1] * M[1][2]) / det;
Inv[0][1] = (M[0][2] * M[2][1] - M[0][1] * M[2][2]) / det;
Inv[0][2] = (M[0][1] * M[1][2] - M[0][2] * M[1][1]) / det;
Inv[1][0] = (M[1][2] * M[2][0] - M[1][0] * M[2][2]) / det;
Inv[1][1] = (M[0][0] * M[2][2] - M[2][0] * M[0][2]) / det;
Inv[1][2] = (M[0][2] * M[1][0] - M[0][0] * M[1][2]) / det;
Inv[2][0] = (M[1][0] * M[2][1] - M[1][1] * M[2][0]) / det;
Inv[2][1] = (M[0][1] * M[2][0] - M[0][0] * M[2][1]) / det;
Inv[2][2] = (M[0][0] * M[1][1] - M[1][0] * M[0][1]) / det;
inv[0][0] = (M[1][1] * M[2][2] - M[2][1] * M[1][2]) / det;
inv[0][1] = (M[0][2] * M[2][1] - M[0][1] * M[2][2]) / det;
inv[0][2] = (M[0][1] * M[1][2] - M[0][2] * M[1][1]) / det;
inv[1][0] = (M[1][2] * M[2][0] - M[1][0] * M[2][2]) / det;
inv[1][1] = (M[0][0] * M[2][2] - M[2][0] * M[0][2]) / det;
inv[1][2] = (M[0][2] * M[1][0] - M[0][0] * M[1][2]) / det;
inv[2][0] = (M[1][0] * M[2][1] - M[1][1] * M[2][0]) / det;
inv[2][1] = (M[0][1] * M[2][0] - M[0][0] * M[2][1]) / det;
inv[2][2] = (M[0][0] * M[1][1] - M[1][0] * M[0][1]) / det;
}

return Inv;
return inv;
}

/**
Expand All @@ -1668,7 +1668,7 @@ DCPProfile::Matrix DCPProfile::matInv(const Matrix& M)
* Returns:
* tuple. Temperature, tint
*/
void DCPProfile::setChromaticityCoordinates(const double x, const double y, double& cct, double& tint)
void DCPProfile::setChromaticityCoordinates(const double x, const double y, double& cct, double& tint) const
{
const double u = 2.0 * x / (1.5 - x + 6.0 * y);
const double v = 3.0 * y / (1.5 - x + 6.0 * y);
Expand Down Expand Up @@ -1734,7 +1734,7 @@ void DCPProfile::setChromaticityCoordinates(const double x, const double y, doub
* Returns:
* tuple. Chromaticity coordinates.
*/
void DCPProfile::getChromaticityCoordinates(const double cct, const double tint, double& x, double& y)
void DCPProfile::getChromaticityCoordinates(const double cct, const double tint, double& x, double& y) const
{
const double r = 1.0e6 / cct;
const double offset = tint * (1.0 / TINT_SCALE);
Expand Down Expand Up @@ -1786,13 +1786,13 @@ void DCPProfile::getChromaticityCoordinates(const double cct, const double tint,
}


void DCPProfile::getChromaticityCoordinatesFromXyz(const Triple& xyz, double& x, double& y)
void DCPProfile::getChromaticityCoordinatesFromXyz(const Triple& xyz, double& x, double& y) const
{
x = xyz[0] / (xyz[0] + xyz[1] + xyz[2]);
y = xyz[1] / (xyz[0] + xyz[1] + xyz[2]);
}

DCPProfile::Triple DCPProfile::getXyzFromChromaticityCoordinates(const double x, const double y)
DCPProfile::Triple DCPProfile::getXyzFromChromaticityCoordinates(const double x, const double y) const
{
Triple xyz;
xyz[0] = x * 1.0 / y;
Expand All @@ -1801,7 +1801,7 @@ DCPProfile::Triple DCPProfile::getXyzFromChromaticityCoordinates(const double x,
return xyz;
}

DCPProfile::Triple DCPProfile::getXyzFromTemperature(const double cct, const double tint)
DCPProfile::Triple DCPProfile::getXyzFromTemperature(const double cct, const double tint) const
{
double x, y;
getChromaticityCoordinates(cct, tint, x, y);
Expand Down Expand Up @@ -1851,7 +1851,7 @@ DCPProfile::Triple DCPProfile::getXyzFromTemperature(const double cct, const dou
* Returns :
* tuple.Chromaticity coordinates.
*/
void DCPProfile::getChromaticityCoordinatesFromCameraNeutral(const Matrix& analogBalance, const Triple& asShotNeutral, double& x, double& y)
void DCPProfile::getChromaticityCoordinatesFromCameraNeutral(const Matrix& analogBalance, const Triple& asShotNeutral, double& x, double& y) const
{
x = 0.24559589702841558;
y = 0.24240399461846152;
Expand Down Expand Up @@ -1925,7 +1925,7 @@ void DCPProfile::getChromaticityCoordinatesFromCameraNeutral(const Matrix& analo
* Returns:
* Matrix. Chromatic Adaptation matrix ( 3 x 3 ).
*/
DCPProfile::Matrix DCPProfile::getChromaticAdaptationMatrix(const Triple& xyzSource, const Triple& xyzTarget)
DCPProfile::Matrix DCPProfile::getChromaticAdaptationMatrix(const Triple& xyzSource, const Triple& xyzTarget) const
{
const Triple pybSource = matMult(CAT02_MATRIX, xyzSource);
const Triple pybTarget = matMult(CAT02_MATRIX, xyzTarget);
Expand Down Expand Up @@ -1996,7 +1996,7 @@ DCPProfile::Matrix DCPProfile::getChromaticAdaptationMatrix(const Triple& xyzSou
* Returns:
* Matrix. Camera to XYZ ( D50 ) matrix ( 3 x 3 ).
*/
DCPProfile::Matrix DCPProfile::getCameraToXyzD50Matrix(const double x, const double y)
DCPProfile::Matrix DCPProfile::getCameraToXyzD50Matrix(const double x, const double y) const
{
double cct, tint;
setChromaticityCoordinates(x, y, cct, tint);
Expand Down Expand Up @@ -2057,7 +2057,7 @@ DCPProfile::Matrix DCPProfile::getCameraToXyzD50Matrix(const double x, const dou
return cameraToXyzD50;
}

DCPProfile::Matrix DCPProfile::getCameraToSrgbLinearMatrix(const double x, const double y)
DCPProfile::Matrix DCPProfile::getCameraToSrgbLinearMatrix(const double x, const double y) const
{
double cct, tint;
setChromaticityCoordinates(x, y, cct, tint);
Expand Down Expand Up @@ -2106,7 +2106,7 @@ DCPProfile::Matrix DCPProfile::getCameraToSrgbLinearMatrix(const double x, const
return cameraToSrgbLinear;
}

DCPProfile::Matrix DCPProfile::getCameraToACES2065Matrix(const Triple& asShotNeutral, const bool sourceIsRaw)
DCPProfile::Matrix DCPProfile::getCameraToACES2065Matrix(const Triple& asShotNeutral, const bool sourceIsRaw) const
{
double x, y;
getChromaticityCoordinatesFromCameraNeutral(IdentityMatrix, asShotNeutral, x, y);
Expand Down Expand Up @@ -2158,7 +2158,7 @@ DCPProfile::Matrix DCPProfile::getCameraToACES2065Matrix(const Triple& asShotNeu
}


void DCPProfile::getMatrices(const std::string& type, std::vector<Matrix>& v_Mat)
void DCPProfile::getMatrices(const std::string& type, std::vector<Matrix>& v_Mat) const
{
v_Mat.clear();

Expand All @@ -2185,7 +2185,7 @@ void DCPProfile::getMatrices(const std::string& type, std::vector<Matrix>& v_Mat
}
}

void DCPProfile::getMatricesAsStrings(const std::string& type, std::vector<std::string>& v_strMat)
void DCPProfile::getMatricesAsStrings(const std::string& type, std::vector<std::string>& v_strMat) const
{
v_strMat.clear();
std::vector<Matrix> v_Mat;
Expand Down Expand Up @@ -2268,7 +2268,7 @@ void DCPProfile::setMatricesFromStrings(const std::string& type, std::vector<std
setMatrices(type, v_Mat);
}

void DCPProfile::applyLinear(OIIO::ImageBuf& image, const Triple neutral, const bool sourceIsRaw)
void DCPProfile::applyLinear(OIIO::ImageBuf& image, const Triple& neutral, const bool sourceIsRaw) const
{
const Matrix cameraToACES2065Matrix = getCameraToACES2065Matrix(neutral, sourceIsRaw);

Expand All @@ -2292,7 +2292,7 @@ void DCPProfile::applyLinear(OIIO::ImageBuf& image, const Triple neutral, const
}
}

void DCPProfile::applyLinear(Image<image::RGBAfColor>& image, const Triple neutral, const bool sourceIsRaw)
void DCPProfile::applyLinear(Image<image::RGBAfColor>& image, const Triple& neutral, const bool sourceIsRaw) const
{
const Matrix cameraToACES2065Matrix = getCameraToACES2065Matrix(neutral, sourceIsRaw);

Expand Down Expand Up @@ -2356,7 +2356,7 @@ void DCPDatabase::clear()
folderName = "";
}

bool DCPDatabase::getDcpForCamera(const std::string& make, const std::string& model, DCPProfile& dcpProf)
bool DCPDatabase::retrieveDcpForCamera(const std::string& make, const std::string& model, DCPProfile& dcpProf)
{
const std::string dcpKey = make + "_" + model;

Expand Down
46 changes: 23 additions & 23 deletions src/aliceVision/image/dcp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ constexpr double sRGB_xyz[3][3] = {
{ -0.9787684, 1.9161415, 0.0334540},
{0.0719453, -0.2289914, 1.4052427}
};

constexpr double xyz_prophoto[3][3] = {
{0.7976749, 0.1351917, 0.0313534},
{0.2880402, 0.7118741, 0.0000857},
{0.0000000, 0.0000000, 0.8252100}
};
};
constexpr double prophoto_xyz[3][3] = {
{1.3459433, -0.2556075, -0.0511118},
{ -0.5445989, 1.5081673, 0.0205351},
Expand Down Expand Up @@ -173,14 +173,14 @@ class DCPProfile final
* param[in] type The matrices to get, "color" or "forward"
* param[in] v_Mat A vector of matrices to be populated
*/
void getMatrices(const std::string& type, std::vector<Matrix>& v_Mat);
void getMatrices(const std::string& type, std::vector<Matrix>& v_Mat) const;

/**
* @brief getMatricesAsStrings gets some matrices contained in the profile in a string format (one string per matrix)
* param[in] type The matrices to get, "color" or "forward"
* param[in] v_strMat A vector of std::string to be populated
*/
void getMatricesAsStrings(const std::string& type, std::vector<std::string>& v_strMat);
void getMatricesAsStrings(const std::string& type, std::vector<std::string>& v_strMat) const;

/**
* @brief setMatrices sets some matrices contained in the profile
Expand All @@ -202,15 +202,15 @@ class DCPProfile final
* param[in] neutral The neutral value calculated from the camera multiplicators contained in the cam_mul OIIO metadata
* param[in] sourceIsRaw indicates that the image buffer contains data in raw space (no neutralization <=> cam_mul not applied)
*/
void applyLinear(OIIO::ImageBuf& image, const Triple neutral, const bool sourceIsRaw = false);
void applyLinear(OIIO::ImageBuf& image, const Triple& neutral, const bool sourceIsRaw = false) const;

/**
* @brief applyLinear applies the linear part of a DCP profile on an aliceVision image
* param[in] image The aliceVision image on which the profile must be applied
* param[in] neutral The neutral value calculated from the camera multiplicators contained in the cam_mul OIIO metadata
* param[in] sourceIsRaw indicates that the image buffer contains data in raw space (no neutralization <=> cam_mul not applied)
*/
void applyLinear(Image<image::RGBAfColor>& image, const Triple neutral, const bool sourceIsRaw = false);
void applyLinear(Image<image::RGBAfColor>& image, const Triple& neutral, const bool sourceIsRaw = false) const;

/**
* @brief apply applies the non linear part of a DCP profile on an OIIO image buffer
Expand Down Expand Up @@ -256,21 +256,21 @@ class DCPProfile final

void hsdApply(const HsdTableInfo& table_info, const std::vector<HsbModify>& table_base, float& h, float& s, float& v) const;

Matrix matMult(const Matrix& A, const Matrix& B);
Triple matMult(const Matrix& M, const Triple& V);
Matrix matInv(const Matrix& M);

Matrix getInterpolatedMatrix(const double cct, const std::string& type);
void getChromaticityCoordinatesFromXyz(const Triple& xyz, double& x, double& y);
Triple getXyzFromChromaticityCoordinates(const double x, const double y);
Triple getXyzFromTemperature(const double cct, const double tint = 0.f);
void setChromaticityCoordinates(const double x, const double y, double& cct, double& tint);
void getChromaticityCoordinates(const double cct, const double tint, double& x, double& y);
void getChromaticityCoordinatesFromCameraNeutral(const Matrix& analogBalance, const Triple& asShotNeutral, double& x, double& y);
Matrix getChromaticAdaptationMatrix(const Triple& xyzSource, const Triple& xyzTarget);
Matrix getCameraToXyzD50Matrix(const double x, const double y);
Matrix getCameraToSrgbLinearMatrix(const double x, const double y);
Matrix getCameraToACES2065Matrix(const Triple& asShotNeutral, const bool sourceIsRaw = false);
Matrix matMult(const Matrix& A, const Matrix& B) const;
Triple matMult(const Matrix& M, const Triple& V) const;
Matrix matInv(const Matrix& M) const;

Matrix getInterpolatedMatrix(const double cct, const std::string& type) const;
void getChromaticityCoordinatesFromXyz(const Triple& xyz, double& x, double& y) const;
Triple getXyzFromChromaticityCoordinates(const double x, const double y) const;
Triple getXyzFromTemperature(const double cct, const double tint = 0.f) const;
void setChromaticityCoordinates(const double x, const double y, double& cct, double& tint) const;
void getChromaticityCoordinates(const double cct, const double tint, double& x, double& y) const;
void getChromaticityCoordinatesFromCameraNeutral(const Matrix& analogBalance, const Triple& asShotNeutral, double& x, double& y) const;
Matrix getChromaticAdaptationMatrix(const Triple& xyzSource, const Triple& xyzTarget) const;
Matrix getCameraToXyzD50Matrix(const double x, const double y) const;
Matrix getCameraToSrgbLinearMatrix(const double x, const double y) const;
Matrix getCameraToACES2065Matrix(const Triple& asShotNeutral, const bool sourceIsRaw = false) const;

Matrix ws_sRGB; // working color space to sRGB
Matrix sRGB_ws; // sRGB to working color space
Expand Down Expand Up @@ -335,15 +335,15 @@ class DCPDatabase final
void add_or_replace(DCPProfile& dcpProf, const std::string& make, const std::string& model);

/**
* @brief getDcpForCamera gets a DCP profile in the database for a given camera.
* @brief retrieveDcpForCamera searches for a DCP profile in the database for a given camera.
* Search first in the cache. If no DCP profile is found search if an appropriate DCP file exists in the file list.
* If a dcp file is found then load it and store it in the cache.
* param[in] make The camera maker
* param[in] model The camera model
* param[in] dcpProf the DCP profile to be filled in
* return True if a corresponding profile has been found
*/
bool getDcpForCamera(const std::string& make, const std::string& model, DCPProfile& dcpProf);
bool retrieveDcpForCamera(const std::string& make, const std::string& model, DCPProfile& dcpProf);

private:

Expand Down
29 changes: 25 additions & 4 deletions src/aliceVision/stl/mapUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
// m[1] = 1.6;
// std::vector<int> keys;
// // Retrieve all keys
// transform(m.begin(), m.end(), back_inserter(keys), stl::RetrieveKey());
// transform(m.begin(), m.end(), back_inserter(keys), RetrieveKey());
// // Log all keys to console
// copy(keys.begin(), keys.end(), ostream_iterator<int>(cout, "\n"));
// // Retrieve all values
// std::vector<double> values;
// // Retrieve all values
// transform(m.begin(), m.end(), back_inserter(values), stl::RetrieveValue());
// transform(m.begin(), m.end(), back_inserter(values), RetrieveValue());

#include <map>

namespace stl{
namespace stl {

/// Allow to select the Keys of a map.
struct RetrieveKey
Expand All @@ -48,4 +48,25 @@ struct RetrieveValue
}
};

} // namespace stl
}

namespace aliceVision {

template<typename Map>
const typename Map::mapped_type& map_get_with_default(const Map& m, const typename Map::key_type& key, const typename Map::mapped_type& defval)
{
auto it = m.find(key);
if(it == m.end())
return defval;
return it->second;
}
template<typename Map>
bool map_has_non_empty_value(const Map& m, const typename Map::key_type& key)
{
auto it = m.find(key);
if(it == m.end())
return false;
return !it->second.empty();
}

}
Loading

0 comments on commit 89b4acc

Please sign in to comment.