Skip to content

Commit

Permalink
Add PQ in sRGB linear signal detection
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Oct 17, 2024
1 parent c5832c2 commit d4e5682
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
2 changes: 2 additions & 0 deletions include/lut-calibrator/ColorSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ namespace ColorSpaceMath

double3 srgb_linear_to_nonlinear(double3 input);

double srgb_linear_to_nonlinear(double input);

double3 from_BT2020_to_BT709(double3 a);

double PQ_ST2084(double scale, double nonlinear);
Expand Down
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, sRGB, BT2020inSRGB };
enum HDR_GAMMA { PQ = 0, HLG, sRGB, BT2020inSRGB, PQinSRGB};
}

struct BestResult;
Expand Down
3 changes: 1 addition & 2 deletions sources/api/HyperAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,6 @@ void HyperAPI::handleCropCommand(const QJsonObject& message, const QString& comm

void HyperAPI::handleBenchmarkCommand(const QJsonObject& message, const QString& command, int tan)
{
GrabberWrapper* grabberWrapper = (_videoGrabber != nullptr) ? _videoGrabber->grabberWrapper() : nullptr;
const QString& subc = message["subcommand"].toString().trimmed();
int status = message["status"].toInt();

Expand Down Expand Up @@ -990,7 +989,7 @@ void HyperAPI::handleLutCalibrationCommand(const QJsonObject& message, const QSt
{
QString subcommand = message["subcommand"].toString("");

if (_lutCalibratorThread == nullptr && subcommand == "capture")
if (subcommand == "capture")
{
bool debug = message["debug"].toBool(false);
bool lchCorrection = message["lch_correction"].toBool(false);
Expand Down
4 changes: 3 additions & 1 deletion sources/lut-calibrator/ColorSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ namespace ColorSpaceMath
return "sRGB";
else if (gamma == HDR_GAMMA::BT2020inSRGB)
return "BT2020 with sRGB TRC";
else if (gamma == HDR_GAMMA::PQinSRGB)
return "PQ in SRGB";
return "UNKNOWN";
}

Expand Down Expand Up @@ -453,7 +455,7 @@ namespace ColorSpaceMath
return DBL_MAX;
}

constexpr double maxLenInColorspace
double maxLenInColorspace
(const std::vector<double2>& primaries,
const double cos_angle,
const double sin_angle)
Expand Down
22 changes: 16 additions & 6 deletions sources/lut-calibrator/LutCalibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,11 @@ static double3 hdr_to_srgb(const YuvConverter* _yuvConverter, double3 yuv, const
{
e = srgb_nonlinear_to_linear(a);
}
else if (gamma == HDR_GAMMA::PQinSRGB)
{
a = srgb_linear_to_nonlinear(a);
e = PQ_ST2084(10000.0 / nits, a);
}


if (gamma != HDR_GAMMA::sRGB)
Expand Down Expand Up @@ -756,7 +761,6 @@ void CalibrationWorker::run()
{
constexpr int MAX_HINT = std::numeric_limits<int>::max() / 2.0;
constexpr double MAX_HDOUBLE = std::numeric_limits<double>::max() / 2.0;
constexpr auto MAX_IND = SCREEN_COLOR_DIMENSION - 1;

printf("Starter thread: %i. Range: [%i - %i)\n", id, krIndexStart, krIndexEnd);

Expand Down Expand Up @@ -815,11 +819,9 @@ void CalibrationWorker::run()
for (auto v = vertex.begin(); v != vertex.end(); ++v)
{
auto& sample = *(*v).first;
const auto& index = sample.coords();

auto minError = MAX_CALIBRATION_ERROR;
auto sampleList = sample.getInputYuvColors();
bool gotSample = false;

for (auto iter = sampleList.cbegin(); iter != sampleList.cend(); ++iter)
{
Expand Down Expand Up @@ -1015,7 +1017,7 @@ void LutCalibrator::fineTune(bool precise)
sampleColors[SampleColor::LOW_GREEN] = (std::pair<double3, byte2>(sampleGreenLow.getInputYuvColors().front().first, byte2{ sampleGreenLow.U(), sampleGreenLow.V() }));
sampleColors[SampleColor::LOW_BLUE] = (std::pair<double3, byte2>(sampleBlueLow.getInputYuvColors().front().first, byte2{ sampleBlueLow.U(), sampleBlueLow.V() }));

for (int gamma = (precise) ? bestResult->gamma : HDR_GAMMA::PQ; gamma <= HDR_GAMMA::BT2020inSRGB; gamma++)
for (int gamma = (precise) ? bestResult->gamma : HDR_GAMMA::PQ; gamma <= HDR_GAMMA::PQinSRGB; gamma++)
{
std::vector<double> gammasHLG;

Expand All @@ -1037,6 +1039,10 @@ void LutCalibrator::fineTune(bool precise)
{
NITS = 10000.0 * PQ_ST2084(1.0, maxLevel);
}
else if (gamma == HDR_GAMMA::PQinSRGB)
{
NITS = 10000.0 * PQ_ST2084(1.0, srgb_linear_to_nonlinear(maxLevel));
}

for (double gammaHLG : gammasHLG)
{
Expand Down Expand Up @@ -1228,7 +1234,6 @@ void LutCalibrator::calibration()
static void reportLCH(Logger* _log, std::vector<std::vector<std::vector<CapturedColor>>>* all)
{
QStringList info, intro;
const int SCALE = SCREEN_COLOR_DIMENSION - 1;
std::list<MappingPrime> mHigh;
std::list<MappingPrime> mLow;

Expand Down Expand Up @@ -1489,6 +1494,11 @@ void LutCalibrator::capturedPrimariesCorrection(ColorSpaceMath::HDR_GAMMA gamma,
{
a = srgb_nonlinear_to_linear(a);
}
else if (gamma == HDR_GAMMA::PQinSRGB)
{
a = srgb_linear_to_nonlinear(a);
a = PQ_ST2084(10000.0 / nits, a);
}
actualPrimaries.push_back(a);
}

Expand Down Expand Up @@ -1570,7 +1580,7 @@ bool LutCalibrator::setTestData()

const auto& colors = (*iter);

for (int i = 0; i < colors.size(); i += 4)
for (int i = 0; i < static_cast<int>(colors.size()); i += 4)
{
for (int j = 0; j < colors[i]; j++)
{
Expand Down

0 comments on commit d4e5682

Please sign in to comment.