Skip to content

Commit

Permalink
Add BT1886 view EOTF (#928)
Browse files Browse the repository at this point in the history
* Add BT1886 view EOTF

---------

Co-authored-by: cedric <cedric.paille@gmail.com>
Co-authored-by: Frédéric Devernay <devernay@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 28, 2023
1 parent 2acaa84 commit e81c3f3
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Engine/ImageConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ lutFromColorspace(ViewerColorSpaceEnum cs)
case eViewerColorSpaceRec709:
lut = Color::LutManager::Rec709Lut();
break;
case eViewerColorSpaceBT1886:
lut = Color::LutManager::BT1886Lut();
break;
case eViewerColorSpaceLinear:
default:
lut = 0;
Expand Down
6 changes: 6 additions & 0 deletions Engine/Lut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,12 @@ LutManager::sRGBLut()
return LutManager::m_instance.getLut("sRGB", from_func_srgb, to_func_srgb);
}

const Lut*
LutManager::BT1886Lut()
{
return LutManager::m_instance.getLut("BT1886", from_func_bt1886, to_func_bt1886);
}

// Rec.709 and Rec.2020 share the same transfer function (and illuminant), except that
// Rec.2020 is more precise.
// https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.2020-0-201208-S!!PDF-E.pdf
Expand Down
13 changes: 13 additions & 0 deletions Engine/Lut.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class LutManager
///buit-ins color-spaces
static const Lut* sRGBLut();
static const Lut* Rec709Lut();
static const Lut* BT1886Lut();
static const Lut* CineonLut();
static const Lut* Gamma1_8Lut();
static const Lut* Gamma2_2Lut();
Expand Down Expand Up @@ -496,6 +497,18 @@ to_func_srgb(float v)
}
}

inline float
from_func_bt1886(float v)
{
return std::pow(v, 2.4);
}

inline float
to_func_bt1886(float v)
{
return std::pow(v, 1.0/2.4);
}

// r,g,b values are from 0 to 1
// h = [0,NATRON_COLOR_HUE_CIRCLE], s = [0,1], v = [0,1]
// if s == 0, then h = 0 (undefined)
Expand Down
1 change: 1 addition & 0 deletions Engine/Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ Project::initializeKnobs()
colorSpaces.push_back(ChoiceOption("Linear","",""));
colorSpaces.push_back(ChoiceOption("sRGB","",""));
colorSpaces.push_back(ChoiceOption("Rec.709","",""));
colorSpaces.push_back(ChoiceOption("BT1886","",""));

_imp->colorSpace8u = AppManager::createKnob<KnobChoice>( this, tr("8-Bit LUT") );
_imp->colorSpace8u->setName("defaultColorSpace8u");
Expand Down
3 changes: 3 additions & 0 deletions Engine/ViewerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ ViewerInstance::lutFromColorspace(ViewerColorSpaceEnum cs)
case eViewerColorSpaceRec709:
lut = Color::LutManager::Rec709Lut();
break;
case eViewerColorSpaceBT1886:
lut = Color::LutManager::BT1886Lut();
break;
case eViewerColorSpaceLinear:
default:
lut = 0;
Expand Down
3 changes: 2 additions & 1 deletion Global/Enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ enum ViewerColorSpaceEnum
{
eViewerColorSpaceSRGB = 0,
eViewerColorSpaceLinear,
eViewerColorSpaceRec709
eViewerColorSpaceRec709,
eViewerColorSpaceBT1886
};

enum ImageBitDepthEnum
Expand Down
20 changes: 15 additions & 5 deletions Gui/Shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,31 @@ const char* fragRGB =
"float linear_to_rec709(float c) {"
" return (c<0.018) ? (4.500*c) : (1.099*pow(c,0.45) - 0.099);\n"
"}\n"
"void main(){\n"
"float linear_to_bt1886(float c) {"
" return pow(c,1.0/2.4);\n"
"}\n"
"void main() {\n"
" vec4 color_tmp = texture2D(Tex,gl_TexCoord[0].st);\n"
" color_tmp.rgb = (color_tmp.rgb * gain) + offset;\n"
" if(lut == 0){ // srgb\n"
" if (lut == 0) { // srgb\n"
// << TO SRGB
" color_tmp.r = linear_to_srgb(color_tmp.r);\n"
" color_tmp.g = linear_to_srgb(color_tmp.g);\n"
" color_tmp.b = linear_to_srgb(color_tmp.b);\n"
// << END TO SRGB
" }\n"
" else if (lut == 2){ // Rec 709\n" // << TO REC 709
" } else if (lut == 2) { // Rec 709\n"
// << TO REC 709
" color_tmp.r = linear_to_rec709(color_tmp.r);\n"
" color_tmp.g = linear_to_rec709(color_tmp.g);\n"
" color_tmp.b = linear_to_rec709(color_tmp.b);\n"
" }\n" // << END TO REC 709
// << END TO REC 709
" } else if (lut == 3) { // BT1886\n"
// << TO BT1886
" color_tmp.r = linear_to_bt1886(color_tmp.r);\n"
" color_tmp.g = linear_to_bt1886(color_tmp.g);\n"
" color_tmp.b = linear_to_bt1886(color_tmp.b);\n"
// << END TO BT1886
" }\n"
" if (gamma <= 0.) {\n"
" color_tmp.r = (color_tmp.r >= 1.) ? 1. : 0.;\n"
" color_tmp.g = (color_tmp.g >= 1.) ? 1. : 0.;\n"
Expand Down
1 change: 1 addition & 0 deletions Gui/ViewerTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ ViewerTab::ViewerTab(const std::list<NodeGuiPtr> & existingNodesContext,
_imp->viewerColorSpace->addItem( QString::fromUtf8("Linear(None)") );
_imp->viewerColorSpace->addItem( QString::fromUtf8("sRGB") );
_imp->viewerColorSpace->addItem( QString::fromUtf8("Rec.709") );
_imp->viewerColorSpace->addItem( QString::fromUtf8("BT1886") );
_imp->viewerColorSpace->setCurrentIndex(1);

QPixmap pixCheckerboardEnabled, pixCheckerboardDisabld;
Expand Down
2 changes: 2 additions & 0 deletions Gui/ViewerTab10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ ViewerTab::onColorSpaceComboBoxChanged(int v)
colorspace = eViewerColorSpaceSRGB;
} else if (v == 2) {
colorspace = eViewerColorSpaceRec709;
} else if (v == 3) {
colorspace = eViewerColorSpaceBT1886;
} else {
assert(false);
throw std::logic_error("ViewerTab::onColorSpaceComboBoxChanged(): unknown colorspace");
Expand Down
5 changes: 4 additions & 1 deletion Gui/ViewerTab30.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ ViewerTab::getColorSpace() const

return "Rec.709";
break;
default:
case eViewerColorSpaceBT1886:

return "BT1886";
break;
default:
return "";
break;
}
Expand Down

0 comments on commit e81c3f3

Please sign in to comment.