Skip to content

Commit ed070ca

Browse files
committed
fix(image): color drift on copy&paste / save&load
1 parent 02b221b commit ed070ca

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

include/tev/Common.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,10 @@ inline float toSRGB(float val, float gamma = 2.4f) {
600600
}
601601
}
602602

603+
inline nanogui::Vector3f toSRGB(nanogui::Vector3f val, float gamma = 2.4f) {
604+
return {toSRGB(val.x(), gamma), toSRGB(val.y(), gamma), toSRGB(val.z(), gamma)};
605+
}
606+
603607
inline float toLinear(float val, float gamma = 2.4f) {
604608
static constexpr float a = 0.055f;
605609
static constexpr float threshold = 0.04045f;
@@ -612,6 +616,16 @@ inline float toLinear(float val, float gamma = 2.4f) {
612616
}
613617
}
614618

619+
inline nanogui::Vector3f toLinear(nanogui::Vector3f val, float gamma = 2.4f) {
620+
return {toLinear(val.x(), gamma), toLinear(val.y(), gamma), toLinear(val.z(), gamma)};
621+
}
622+
623+
inline float applyGamma(float val, float gamma) { return std::copysign(std::pow(std::abs(val), gamma), val); }
624+
625+
inline nanogui::Vector3f applyGamma(nanogui::Vector3f val, float gamma) {
626+
return {applyGamma(val.x(), gamma), applyGamma(val.y(), gamma), applyGamma(val.z(), gamma)};
627+
}
628+
615629
int lastError();
616630
int lastSocketError();
617631
std::string errorString(int errorId);
@@ -696,7 +710,7 @@ inline nanogui::Vector3f applyTonemap(nanogui::Vector3f value, float gamma, ETon
696710
nanogui::Vector3f result;
697711
switch (tonemap) {
698712
case ETonemap::Gamma: {
699-
result = {std::pow(value.x(), 1 / gamma), std::pow(value.y(), 1 / gamma), std::pow(value.z(), 1 / gamma)};
713+
result = applyGamma(value, 1.0f / gamma);
700714
break;
701715
}
702716
case ETonemap::FalseColor: {

src/Image.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,15 +1241,18 @@ Task<HeapArray<uint8_t>> Image::getRgbaLdrImageData(
12411241
rgbaHdrData.size(),
12421242
[&](const size_t i) {
12431243
const size_t start = 4 * i;
1244-
const Vector3f rgb = applyTonemap(
1245-
{
1246-
applyExposureAndOffset(rgbaHdrData[start + 0], exposure, offset),
1247-
applyExposureAndOffset(rgbaHdrData[start + 1], exposure, offset),
1248-
applyExposureAndOffset(rgbaHdrData[start + 2], exposure, offset),
1249-
},
1250-
gamma,
1251-
tonemap
1252-
);
1244+
const Vector3f rgb = toSRGB(applyGamma(
1245+
applyTonemap(
1246+
{
1247+
applyExposureAndOffset(rgbaHdrData[start + 0], exposure, offset),
1248+
applyExposureAndOffset(rgbaHdrData[start + 1], exposure, offset),
1249+
applyExposureAndOffset(rgbaHdrData[start + 2], exposure, offset),
1250+
},
1251+
gamma,
1252+
tonemap
1253+
),
1254+
2.2f
1255+
));
12531256

12541257
const auto rgba = Vector4f{rgb.x(), rgb.y(), rgb.z(), rgbaHdrData[start + 3]};
12551258

0 commit comments

Comments
 (0)