Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Oct 2, 2024
1 parent 6d37f16 commit fde0461
Show file tree
Hide file tree
Showing 14 changed files with 368 additions and 239 deletions.
4 changes: 2 additions & 2 deletions include/lut-calibrator/BoardUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace BoardUtils
CapturedColor readBlock(const Image<ColorRgb>& yuvImage, int2 position, byte3* _color = nullptr);
void getWhiteBlackColorLevels(const Image<ColorRgb>& yuvImage, CapturedColor& white, CapturedColor& black, int& line);
bool verifyBlackColorPattern(const Image<ColorRgb>& yuvImage, bool isFirstWhite, CapturedColor& black);
bool parseBoard(Logger* _log, const Image<ColorRgb>& yuvImage, int& boardIndex, CapturedColors& allColors);
bool parseBoard(Logger* _log, const Image<ColorRgb>& yuvImage, int& boardIndex, CapturedColors& allColors, bool multiFrame = false);
Image<ColorRgb> loadTestBoardAsYuv(const std::string& filename);
void createTestBoards(const char* pattern = "D:/table_%1.png");
bool verifyTestBoards(Logger* _log, const char* pattern = "D:/table_%1.png");
Expand All @@ -81,7 +81,7 @@ namespace BoardUtils
double _upYLimit = 0;

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

std::vector<std::vector<std::vector<CapturedColor>>> all = std::vector(BoardUtils::SCREEN_COLOR_DIMENSION,
std::vector<std::vector<CapturedColor>>(BoardUtils::SCREEN_COLOR_DIMENSION,
Expand Down
21 changes: 11 additions & 10 deletions include/lut-calibrator/CapturedColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@
class CapturedColor
{
private:
int totalSamples = 0;
int3 sourceRGB;
int sourceRGBdelta = 0;
byte3 finalRGB;
std::list<byte3> finalRGB;
double3 color;
std::map<double3, int> inputColors;
std::list<std::pair<double3, int>> sortedInputColors;
byte3 min, max;
byte3 colorInt;
static byte3 totalMinYUV, totalMaxYUV;

int count = 0;

public:
CapturedColor() = default;
Expand All @@ -65,17 +65,18 @@ class CapturedColor
const uint8_t& U() const { return colorInt.y; }
const uint8_t& V() const { return colorInt.z; }

void importColors(const CapturedColor& color);
bool calculateFinalColor();
bool hasAllSamples();
bool hasAnySample();
std::list<std::pair<double3, int>> getInputColors();
void addColor(ColorRgb i);
void addColor(double3 i);
void addColor(const double3& i);
void setSourceRGB(byte3 _color);
int getSourceError(const int3& _color);
int3 getSourceRGB() const;
void setFinalRGB(double3 _color);
byte3 getFinalRGB() const;
static byte3 getMaxYUV();
static byte3 getMinYUV();
static void resetTotalRange();
void setFinalRgb(double3 _color);
std::list<byte3> getFinalRGB() const;

QString toString();
};
25 changes: 23 additions & 2 deletions include/lut-calibrator/ColorSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,33 @@ namespace ColorSpaceMath

double2 XYZ_to_xy(const double3& a);

double3x3 to_XYZ(
constexpr double3x3 to_XYZ(
const double2& red_xy,
const double2& green_xy,
const double2& blue_xy,
const double2& white_xy
);
)
{
double3 r(red_xy.x, red_xy.y, 1.0 - (red_xy.x + red_xy.y));
double3 g(green_xy.x, green_xy.y, 1.0 - (green_xy.x + green_xy.y));
double3 b(blue_xy.x, blue_xy.y, 1.0 - (blue_xy.x + blue_xy.y));
double3 w(white_xy.x, white_xy.y, 1.0 - (white_xy.x + white_xy.y));

w /= white_xy.y;

double3x3 retMat(r, g, b);

double3x3 invMat;
invMat = linalg::inverse(retMat);

double3 scale = linalg::mul(invMat, w);

retMat[0] *= scale.x;
retMat[1] *= scale.y;
retMat[2] *= scale.z;

return retMat;
};

double3 xyz_to_lab(double3 xyz);

Expand Down
8 changes: 4 additions & 4 deletions include/lut-calibrator/LutCalibrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class LutCalibrator : public QObject
void SignalLutCalibrationUpdated(const QJsonObject& data);

public slots:
void startHandler(QString rootpath, hyperhdr::Components defaultComp, bool debug);
void startHandler(QString rootpath, hyperhdr::Components defaultComp, bool debug, bool postprocessing);
void stopHandler();
void setVideoImage(const QString& name, const Image<ColorRgb>& image);
void setSystemImage(const QString& name, const Image<ColorRgb>& image);
Expand All @@ -91,15 +91,14 @@ public slots:
void notifyCalibrationMessage(QString message, bool started = false);
void error(QString message);
void handleImage(const Image<ColorRgb>& image);
void scoreBoard(bool testOnly, const linalg::mat<double, 4, 4>& coefMatrix, ColorSpaceMath::HDR_GAMMA gamma, double gammaHLG, double nits, linalg::vec<double, 3> aspect, int tryBt2020Range, int altConvert, const linalg::mat<double, 3, 3>& bt2020_to_sRgb, const long long int& minError, long long int& currentError);
void tryHDR10();
void calibration();
void setupWhitePointCorrection();
bool setTestData();

void capturedPrimariesCorrection(ColorSpaceMath::HDR_GAMMA gamma, double gammaHLG, double nits, int coef, linalg::mat<double, 3, 3>& convert_bt2020_to_XYZ, linalg::mat<double, 3, 3>& convert_XYZ_to_corrected, bool printDebug = false);
bool parseTextLut2binary(const char* filename = "D:/interpolated_lut.txt", const char* outfile = "D:/lut_lin_tables.3d");

static QString writeLUT(Logger* _log, QString _rootPath, BestResult* bestResult);
static QString writeLUT(Logger* _log, QString _rootPath, BestResult* bestResult, std::vector<std::vector<std::vector<CapturedColor>>>* all);

Logger* _log;
std::shared_ptr<BoardUtils::CapturedColors> _capturedColors;
Expand All @@ -108,4 +107,5 @@ public slots:
MemoryBuffer<uint8_t> _lut;
QString _rootPath;
bool _debug;
bool _postprocessing;
};
4 changes: 2 additions & 2 deletions sources/api/HyperAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,11 +997,11 @@ void HyperAPI::handleLutCalibrationCommand(const QJsonObject& message, const QSt
}

bool debug = message["debug"].toBool(false);

bool postprocessing = message["postprocessing"].toBool(true);
sendSuccessReply(command, tan);

if (subcommand == "capture")
_lutCalibrator->startHandler(_instanceManager->getRootPath(), getActiveComponent(), debug);
_lutCalibrator->startHandler(_instanceManager->getRootPath(), getActiveComponent(), debug, postprocessing);
else
_lutCalibrator->stopHandler();
}
Expand Down
4 changes: 4 additions & 0 deletions sources/api/JSONRPC_schema/schema-lut-calibration.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"debug": {
"type" : "boolean",
"required" : false
},
"postprocessing": {
"type" : "boolean",
"required" : false
}
},
"additionalProperties": false
Expand Down
78 changes: 47 additions & 31 deletions sources/lut-calibrator/BoardUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ namespace BoardUtils
return boardIndex;
}

bool parseBoard(Logger* _log, const Image<ColorRgb>& yuvImage, int& boardIndex, CapturedColors& allColors)
bool parseBoard(Logger* _log, const Image<ColorRgb>& yuvImage, int& boardIndex, CapturedColors& allColors, bool multiFrame)
{
bool exit = (multiFrame) ? false : true;

int line = 0;
CapturedColor white, black;

Expand Down Expand Up @@ -210,8 +212,17 @@ namespace BoardUtils
try
{
auto capturedColor = readBlock(yuvImage, position, &color);
capturedColor.setSourceRGB(color);
allColors.all[R][G][B] = capturedColor;
if (allColors.all[R][G][B].hasAnySample())
{
allColors.all[R][G][B].importColors(capturedColor);
if (allColors.all[R][G][B].hasAllSamples())
exit = true;
}
else
{
capturedColor.setSourceRGB(color);
allColors.all[R][G][B] = capturedColor;
}
}
catch (std::exception& ex)
{
Expand All @@ -237,10 +248,19 @@ namespace BoardUtils
allColors.setRange(YuvConverter::COLOR_RANGE::FULL);
}

if (multiFrame)
{
if (!exit)
Info(_log, "Successfully parsed image of test board no. %i. Waiting for another frame...", boardIndex);
else
Info(_log, "Successfully parsed final image of test board no. %i", boardIndex);
}
else
{
Info(_log, "Successfully parsed image of the %i test board", boardIndex);
}

Info(_log, "Successfully parsed image of the %i test board", boardIndex);

return true;
return exit;
}

Image<ColorRgb> loadTestBoardAsYuv(const std::string& filename)
Expand Down Expand Up @@ -455,45 +475,41 @@ namespace BoardUtils
if (!myfile.is_open())
return false;

std::list<std::pair<const char*, const char*>> arrayMark = { {"np.array([", "])"}, {"{", "}"} };

myfile << "// Values of the table represent YUV in range [0 - 1] * " << std::to_string(IMPORT_SCALE) << ", limited of full" << std::endl;
myfile << "// Each row of the " << std::to_string(SCREEN_COLOR_DIMENSION) <<"^3 table consist of the following RGB coordinates:" << std::endl << "// [ ";

for (int i = 0, j = 0; i < SCREEN_COLOR_DIMENSION; i++, j += SCREEN_COLOR_STEP)
{
if (i)
myfile << ", ";
myfile << std::to_string(std::min(j, 255));
}
std::list<std::pair<const char*, const char*>> arrayMark = { {"{", "}"} }; // {"np.array([", "])"},

myfile << " ]" << std::endl << "// First table is in Python format, second in C++ format" << std::endl << std::endl;

myfile << result;
myfile << result << std::endl;

for(const auto& currentArray : arrayMark)
{
myfile << currentArray.first << std::endl;
myfile << currentArray.first;
for (int r = 0; r < SCREEN_COLOR_DIMENSION; r++)
{
myfile << "\t" << currentArray.first << std::endl;
for (int g = 0; g < SCREEN_COLOR_DIMENSION; g++)
{
myfile << "\t\t" << currentArray.first << std::endl << "\t\t\t";
myfile << std::endl << "\t";
for (int b = 0; b < SCREEN_COLOR_DIMENSION; b++)
{
const auto& elem = all[r][g][b].yuv();
auto elems = all[r][g][b].getInputColors();
myfile << currentArray.first;
myfile << " " << std::to_string(std::lround(elem.x * IMPORT_SCALE)) << ", ";
myfile << std::to_string(std::lround(elem.y * IMPORT_SCALE)) << ", ";
myfile << std::to_string(std::lround(elem.z * IMPORT_SCALE)) << " ";
myfile << currentArray.second << ((b + 1 < SCREEN_COLOR_DIMENSION) ? "," : "");

for (const auto& elem : elems)
{
if (elem != elems.front())
{
myfile << ",";
}

myfile << " " << std::to_string(elem.second) << ",";
myfile << std::to_string((int)elem.first.x) << ",";
myfile << std::to_string((int)elem.first.y) << ",";
myfile << std::to_string((int)elem.first.z);
}

myfile << currentArray.second << ((r + 1 < SCREEN_COLOR_DIMENSION || g + 1 < SCREEN_COLOR_DIMENSION || b + 1 < SCREEN_COLOR_DIMENSION) ? "," : "");
}
myfile << std::endl << "\t\t" << currentArray.second << ((g + 1 < SCREEN_COLOR_DIMENSION) ? "," : "") << std::endl;
}
myfile << "\t" << currentArray.second << ((r + 1< SCREEN_COLOR_DIMENSION) ? "," : "") << std::endl;
}
myfile << currentArray.second << std::endl << std::endl << std::endl;
myfile << std::endl << currentArray.second << std::endl;
}

myfile.close();
Expand Down
Loading

0 comments on commit fde0461

Please sign in to comment.