Skip to content

Commit

Permalink
Add SMPTE C colorimetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Gumball2415 committed Sep 5, 2021
1 parent 82bae36 commit 3655def
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
39 changes: 28 additions & 11 deletions Core/BisqwitNtscFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void BisqwitNtscFilter::OnBeforeApplyFilter()
NtscFilterSettings ntscSettings = _console->GetSettings()->GetNtscFilterSettings();

_keepVerticalRes = ntscSettings.KeepVerticalResolution;
//_SMPTE_C = ntscSettings.NtscSmpteC;

const double pi = std::atan(1.0) * 4;
int contrast = (int)((pictureSettings.Contrast + 1.0) * (pictureSettings.Contrast + 1.0) * 167941);
Expand All @@ -103,14 +104,30 @@ void BisqwitNtscFilter::OnBeforeApplyFilter()

_y = contrast / _yWidth;

_ir = (int)(contrast * 1.994681e-6 * saturation / _iWidth);
_qr = (int)(contrast * 9.915742e-7 * saturation / _qWidth);

_ig = (int)(contrast * 9.151351e-8 * saturation / _iWidth);
_qg = (int)(contrast * -6.334805e-7 * saturation / _qWidth);

_ib = (int)(contrast * -1.012984e-6 * saturation / _iWidth);
_qb = (int)(contrast * 1.667217e-6 * saturation / _qWidth);
// magic numbers is corresponding values from the YIQ to RGB formula
// but divided by 13,995 * [arbitrary value]
/*
_ir = (int)(( 0.95599 / (13995 * 34.2457747)) * contrast * saturation / _iWidth);
_ig = (int)((-0.27201 / (13995 * 212.3864250)) * contrast * saturation / _iWidth);
_ib = (int)((-1.10674 / (13995 * 78.0674723)) * contrast * saturation / _iWidth);
_qr = (int)(( 0.62082 / (13995 * 44.7370743)) * contrast * saturation / _qWidth);
_qg = (int)((-0.64720 / (13995 * 73.0015960)) * contrast * saturation / _qWidth);
_qb = (int)(( 1.70423 / (13995 * 73.0404051)) * contrast * saturation / _qWidth);
*/
_ir = (int)(contrast * 1.994681e-6 * saturation / _iWidth);
_ig = (int)(contrast * 9.151351e-8 * saturation / _iWidth);
_ib = (int)(contrast * -1.012984e-6 * saturation / _iWidth);
_qr = (int)(contrast * 9.915742e-7 * saturation / _qWidth);
_qg = (int)(contrast * -6.334805e-7 * saturation / _qWidth);
_qb = (int)(contrast * 1.667217e-6 * saturation / _qWidth);

// alternate values based on the SMPTE C color primaries
_irC = (int)((0.95599 / (13995 * 80)) * contrast * saturation / _iWidth);
_igC = (int)((-0.27201 / (13995 * 80)) * contrast * saturation / _iWidth);
_ibC = (int)((-1.10674 / (13995 * 80)) * contrast * saturation / _iWidth);
_qrC = (int)((0.62082 / (13995 * 80)) * contrast * saturation / _qWidth);
_qgC = (int)((-0.64720 / (13995 * 80)) * contrast * saturation / _qWidth);
_qbC = (int)((1.70423 / (13995 * 80)) * contrast * saturation / _qWidth);
}

void BisqwitNtscFilter::RecursiveBlend(int iterationCount, uint64_t *output, uint64_t *currentLine, uint64_t *nextLine, int pixelsPerCycle, bool verticalBlend)
Expand Down Expand Up @@ -282,9 +299,9 @@ void BisqwitNtscFilter::NtscDecodeLine(int width, const int8_t* signal, uint32_t
qsum += Read(s) * Sin(s) - Read(s - _qWidth) * Sin(s - _qWidth);

if(!(s % _resDivider) && s >= leftOverscan) {
int r = std::min(255, std::max(0, (ysum*_y + isum*_ir + qsum*_qr) / 65536));
int g = std::min(255, std::max(0, (ysum*_y + isum*_ig + qsum*_qg) / 65536));
int b = std::min(255, std::max(0, (ysum*_y + isum*_ib + qsum*_qb) / 65536));
int r = std::min(255, std::max(0, (ysum*_y + isum*_irC + qsum*_qrC) / 65536));
int g = std::min(255, std::max(0, (ysum*_y + isum*_igC + qsum*_qgC) / 65536));
int b = std::min(255, std::max(0, (ysum*_y + isum*_ibC + qsum*_qbC) / 65536));

*target = 0xFF000000 | (r << 16) | (g << 8) | b;
target++;
Expand Down
5 changes: 3 additions & 2 deletions Core/BisqwitNtscFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class BisqwitNtscFilter : public BaseVideoFilter
atomic<bool> _workDone;

bool _keepVerticalRes = false;
bool _SMPTE_C = false;

int _resDivider = 1;
uint16_t *_ppuOutputBuffer = nullptr;
Expand All @@ -33,8 +34,8 @@ class BisqwitNtscFilter : public BaseVideoFilter
*/
int _yWidth, _iWidth, _qWidth;
int _y;
int _ir, _ig, _ib;
int _qr, _qg, _qb;
int _ir, _ig, _ib, _irC, _igC, _ibC;
int _qr, _qg, _qb, _qrC, _qgC, _qbC;

//To finetune hue, you would have to recalculate sinetable[]. (Coarse changes can be made with Phase0.)
int8_t _sinetable[27]; // 8*sin(x*2pi/12)
Expand Down

0 comments on commit 3655def

Please sign in to comment.