Skip to content

Commit

Permalink
Support for SDR calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Sep 13, 2024
1 parent 6c28e42 commit b1c3fbb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 20 deletions.
11 changes: 9 additions & 2 deletions include/lut-calibrator/BoardUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ namespace BoardUtils
int _capturedFlag = 0;
YuvConverter::COLOR_RANGE _range = YuvConverter::COLOR_RANGE::UNKNOWN;

double _yRange = 0;
double _yShift = 0;
double _downYLimit = 0;
double _upYLimit = 0;

public:
CapturedColors() { CapturedColor::resetTotalRange(); };

Expand All @@ -81,10 +86,12 @@ namespace BoardUtils
std::vector <CapturedColor>(BoardUtils::SCREEN_COLOR_DIMENSION)));;

bool isCaptured(int index) const;
bool areAllCaptured() const;
bool areAllCaptured();
void finilizeBoard();
void correctYRange(double3& yuv);
void setCaptured(int index);
void setRange(YuvConverter::COLOR_RANGE range);
YuvConverter::COLOR_RANGE getRange();
YuvConverter::COLOR_RANGE getRange() const;
bool saveResult(const char* filename = "D:/result.txt");
};
};
2 changes: 1 addition & 1 deletion include/lut-calibrator/LutCalibrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace linalg {
}

namespace ColorSpaceMath {
enum HDR_GAMMA { PQ = 0, HLG };
enum HDR_GAMMA { PQ = 0, HLG, sRGB };
}

struct BestResult;
Expand Down
22 changes: 20 additions & 2 deletions sources/lut-calibrator/BoardUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,32 @@ namespace BoardUtils
return ((_capturedFlag & (1 << index)));
}

bool CapturedColors::areAllCaptured() const
bool CapturedColors::areAllCaptured()
{
for (int i = 0; i <= BoardUtils::SCREEN_LAST_BOARD_INDEX; i++)
if (!isCaptured(i))
return false;

finilizeBoard();

return true;
}

void CapturedColors::finilizeBoard()
{
// update stats
bool limited = (getRange() == YuvConverter::COLOR_RANGE::LIMITED);
_yRange = (limited) ? (235.0 - 16.0) / 255.0 : 1.0;
_yShift = (limited) ? (16.0 / 255.0) : 0;
_downYLimit = all[0][0][0].y();
_upYLimit = all[SCREEN_COLOR_DIMENSION - 1][SCREEN_COLOR_DIMENSION - 1][SCREEN_COLOR_DIMENSION - 1].y();
}

void CapturedColors::correctYRange(double3& yuv)
{
yuv.x = ((yuv.x - _downYLimit) / (_upYLimit - _downYLimit)) * _yRange + _yShift;
}

void CapturedColors::setCaptured(int index)
{
_capturedFlag |= (1 << index);
Expand All @@ -413,7 +431,7 @@ namespace BoardUtils
_range = range;
}

YuvConverter::COLOR_RANGE CapturedColors::getRange()
YuvConverter::COLOR_RANGE CapturedColors::getRange() const
{
return _range;
}
Expand Down
2 changes: 2 additions & 0 deletions sources/lut-calibrator/ColorSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ namespace ColorSpaceMath
return "PQ";
else if (gamma == HDR_GAMMA::HLG)
return "HLG";
else if (gamma == HDR_GAMMA::sRGB)
return "sRGB";
return "UNKNOWN";
}

Expand Down
50 changes: 35 additions & 15 deletions sources/lut-calibrator/LutCalibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,11 @@ linalg::vec<double, 3> LutCalibrator::hdr_to_srgb(linalg::vec<double, 3> yuv, co
{
double3 srgb;

if (gamma == HDR_GAMMA::sRGB)
{
_capturedColors->correctYRange(yuv);
}

yuv.x = (yuv.x - 0.5) * aspect.x + 0.5;
if (UV.x != UV.y || UV.x < 127 || UV.x > 129)
{
Expand All @@ -618,18 +623,28 @@ linalg::vec<double, 3> LutCalibrator::hdr_to_srgb(linalg::vec<double, 3> yuv, co
{
e = OOTF_HLG(inverse_OETF_HLG(a), gammaHLG) * nits;
}


if (altConvert)
else if (gamma == HDR_GAMMA::sRGB)
{
srgb = mul(bt2020_to_sRgb, e);
srgb = a;
}
else


if (gamma != HDR_GAMMA::sRGB)
{
srgb = ColorSpaceMath::from_BT2020_to_BT709(e);

if (altConvert)
{
srgb = mul(bt2020_to_sRgb, e);
}
else
{
srgb = ColorSpaceMath::from_BT2020_to_BT709(e);
}

srgb = srgb_linear_to_nonlinear(srgb);
}

ColorSpaceMath::trim01(srgb);
srgb = srgb_linear_to_nonlinear(srgb);

if (tryBt2020Range)
{
Expand Down Expand Up @@ -678,7 +693,7 @@ void LutCalibrator::fineTune()
{
const auto MAX_IND = SCREEN_COLOR_DIMENSION - 1;
const auto white = _capturedColors->all[MAX_IND][MAX_IND][MAX_IND].Y();
double2 nits{ 0, 0 };
double3 nits{ 0, 0, 0 };
double maxLevel = 0;

if (_capturedColors->getRange() == YuvConverter::COLOR_RANGE::LIMITED)
Expand All @@ -692,13 +707,14 @@ void LutCalibrator::fineTune()

nits[HDR_GAMMA::PQ] = 10000.0 * PQ_ST2084(1.0, maxLevel);

for (int gamma = HDR_GAMMA::PQ; gamma <= HDR_GAMMA::HLG; gamma++)
for (int gamma = HDR_GAMMA::PQ; gamma <= HDR_GAMMA::sRGB; gamma++)
{
std::vector<double> gammasHLG;
if (gamma == HDR_GAMMA::PQ)
gammasHLG = { 0 };
else if (gamma == HDR_GAMMA::HLG)

if (gamma == HDR_GAMMA::HLG)
gammasHLG = { 0 , 1.2, 1.1};
else
gammasHLG = { 0 };

for (double gammaHLG : gammasHLG)
{
Expand Down Expand Up @@ -731,7 +747,7 @@ void LutCalibrator::fineTune()
for (int altConvert = 0; altConvert <= 1; altConvert++)
for (int tryBt2020Range = 0; tryBt2020Range <= 1; tryBt2020Range++)
for (double aspectX = 1.0; aspectX >= 0.9799; aspectX -= 0.01)
for (double aspectYZ = 1.0; aspectYZ <= 1.0901; aspectYZ += 0.005)
for (double aspectYZ = 1.0; aspectYZ <= 1.1301; aspectYZ += 0.005)

{
double3 aspect(aspectX, aspectYZ, aspectYZ);
Expand Down Expand Up @@ -805,15 +821,17 @@ void LutCalibrator::tryHDR10()
Debug(_log, "Score: %f", bestResult->minError / 1000.0);
Debug(_log, "Time: %f", totalTime / 1000.0);


Debug(_log, "Selected coef: %s", QSTRING_CSTR( _yuvConverter->coefToString(bestResult->coef)));
Debug(_log, "Selected coef delta: %f %f", bestResult->coefDelta.x, bestResult->coefDelta.y);
Debug(_log, "Selected EOTF: %s", QSTRING_CSTR(ColorSpaceMath::gammaToString(bestResult->gamma)));
if (bestResult->gamma == HDR_GAMMA::HLG)
{
Debug(_log, "Selected HLG gamma: %f", bestResult->gammaHLG);
}
Debug(_log, "Selected nits: %f", (bestResult->gamma == HDR_GAMMA::HLG) ? 1000.0 * ( 1 / bestResult->nits) : bestResult->nits);
if (bestResult->gamma != HDR_GAMMA::sRGB)
{
Debug(_log, "Selected nits: %f", (bestResult->gamma == HDR_GAMMA::HLG) ? 1000.0 * (1 / bestResult->nits) : bestResult->nits);
}
Debug(_log, "Selected bt2020 range: %i", bestResult->bt2020Range);
Debug(_log, "Selected alt convert: %i", bestResult->altConvert);
Debug(_log, "Selected aspect: %f %f %f", bestResult->aspect.x, bestResult->aspect.y, bestResult->aspect.z);
Expand Down Expand Up @@ -928,6 +946,8 @@ void LutCalibrator::calibrate()
#endif

bestResult = std::make_shared<BestResult>();
_capturedColors->finilizeBoard();


sendReport("Captured colors:\r\n" +
generateReport(false));
Expand Down

0 comments on commit b1c3fbb

Please sign in to comment.