Skip to content

Commit 451ae01

Browse files
committed
Add tonemapper that hashes to a random color
1 parent 2c7e709 commit 451ae01

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

include/tev/Common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ enum ETonemap : int {
373373
Gamma,
374374
FalseColor,
375375
PositiveNegative,
376+
Hash,
376377

377378
// This enum value should never be used directly.
378379
// It facilitates looping over all members of this enum.

src/Common.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ ETonemap toTonemap(string name) {
232232
return FalseColor;
233233
} else if (name == "POSITIVENEGATIVE" || name == "POSNEG" || name == "PN" ||name == "+-") {
234234
return PositiveNegative;
235+
} else if (name == "HASH") {
236+
return Hash;
235237
} else {
236238
return SRGB;
237239
}

src/ImageCanvas.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,17 @@ Vector3f ImageCanvas::applyTonemap(const Vector3f& value, float gamma, ETonemap
558558
result = {-2.0f * mean(min(value, Vector3f{0.0f})), 2.0f * mean(max(value, Vector3f{0.0f})), 0.0f};
559559
break;
560560
}
561+
case ETonemap::Hash:
562+
{
563+
static const auto fract = [](float x){ return x - floor(x); };
564+
result = value;
565+
result *= {abs(result.x()) < 1024.0f ? 1.0f : 1.0f / 1024.0f, abs(result.y()) < 1024.0f ? 1.0f : 1.0f / 1024.0f, abs(result.z()) < 1024.0f ? 1.0f : 1.0f / 1024.0f};
566+
result *= {abs(result.x()) < 1024.0f ? 1.0f : 1.0f / 1024.0f, abs(result.y()) < 1024.0f ? 1.0f : 1.0f / 1024.0f, abs(result.z()) < 1024.0f ? 1.0f : 1.0f / 1024.0f};
567+
result *= {abs(result.x()) < 1024.0f ? 1.0f : 1.0f / 1024.0f, abs(result.y()) < 1024.0f ? 1.0f : 1.0f / 1024.0f, abs(result.z()) < 1024.0f ? 1.0f : 1.0f / 1024.0f};
568+
result = abs(fract(dot(result, Vector3f{115.191742f, 64.0546951f, 124.512291f})) - 0.5f) * Vector3f{1368.46143f, 1523.2019f, 1034.50476f};
569+
result = {2.0f * abs(fract(result.x()) - 0.5f), 2.0f * abs(fract(result.y()) - 0.5f), 2.0f * abs(fract(result.z()) - 0.5f)};
570+
break;
571+
}
561572
default:
562573
throw runtime_error{"Invalid tonemap selected."};
563574
}

src/ImageViewer.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ using namespace std;
2525

2626
namespace tev {
2727

28-
static const int SIDEBAR_MIN_WIDTH = 230;
28+
static const int SIDEBAR_MIN_WIDTH = 245;
2929
static const float CROP_MIN_SIZE = 3;
3030

3131
ImageViewer::ImageViewer(
@@ -214,7 +214,7 @@ ImageViewer::ImageViewer(
214214
// Tonemap options
215215
{
216216
mTonemapButtonContainer = new Widget{mSidebarLayout};
217-
mTonemapButtonContainer->set_layout(new GridLayout{Orientation::Horizontal, 4, Alignment::Fill, 5, 2});
217+
mTonemapButtonContainer->set_layout(new GridLayout{Orientation::Horizontal, 5, Alignment::Fill, 5, 2});
218218

219219
auto makeTonemapButton = [&](const string& name, function<void()> callback) {
220220
auto button = new Button{mTonemapButtonContainer, name};
@@ -228,6 +228,7 @@ ImageViewer::ImageViewer(
228228
makeTonemapButton("Gamma", [this]() { setTonemap(ETonemap::Gamma); });
229229
makeTonemapButton("FC", [this]() { setTonemap(ETonemap::FalseColor); });
230230
makeTonemapButton("+/-", [this]() { setTonemap(ETonemap::PositiveNegative); });
231+
makeTonemapButton("Hash", [this]() { setTonemap(ETonemap::Hash); });
231232

232233
setTonemap(ETonemap::SRGB);
233234

@@ -244,7 +245,10 @@ ImageViewer::ImageViewer(
244245
"False-color visualization\n\n"
245246

246247
"+/-\n"
247-
"Positive=Green, Negative=Red"
248+
"Positive=Green, Negative=Red\n\n"
249+
250+
"Hash\n"
251+
"Hash values to random colors"
248252
);
249253
}
250254

src/UberShader.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ UberShader::UberShader(RenderPass* renderPass) {
1818
# elif defined(NANOGUI_USE_GLES)
1919
std::string preamble =
2020
R"(#version 100
21-
precision highp float;)";
21+
precision highp float;
22+
precision highp sampler2D;)";
2223
# endif
2324
auto vertexShader = preamble +
2425
R"glsl(
@@ -51,6 +52,7 @@ UberShader::UberShader(RenderPass* renderPass) {
5152
#define GAMMA 1
5253
#define FALSE_COLOR 2
5354
#define POS_NEG 3
55+
#define HASH 4
5456
5557
#define ERROR 0
5658
#define ABSOLUTE_ERROR 1
@@ -117,6 +119,13 @@ UberShader::UberShader(RenderPass* renderPass) {
117119
}
118120
}
119121
122+
vec3 hash(vec3 co){
123+
co *= mix(vec3(1.0), vec3(1.0 / 1024.0), step(1024.0, abs(co)));
124+
co *= mix(vec3(1.0), vec3(1.0 / 1024.0), step(1024.0, abs(co)));
125+
co *= mix(vec3(1.0), vec3(1.0 / 1024.0), step(1024.0, abs(co)));
126+
return 2.0 * abs(fract(abs(fract(dot(co, vec3(115.191742, 64.0546951, 124.512291))) - 0.5) * vec3(1368.46143, 1523.2019, 1034.50476)) - 0.5);
127+
}
128+
120129
vec3 applyTonemap(vec3 col, vec4 background) {
121130
if (tonemap == SRGB) {
122131
col = col +
@@ -129,6 +138,8 @@ UberShader::UberShader(RenderPass* renderPass) {
129138
return falseColor(log2(average(col)+0.03125) / 10.0 + 0.5) + (background.rgb - falseColor(0.0)) * background.a;
130139
} else if (tonemap == POS_NEG) {
131140
return vec3(-average(min(col, vec3(0.0))) * 2.0, average(max(col, vec3(0.0))) * 2.0, 0.0) + background.rgb * background.a;
141+
} else if (tonemap == HASH) {
142+
return hash(col) + (background.rgb - hash(vec3(offset))) * background.a;
132143
}
133144
return vec3(0.0);
134145
}
@@ -230,6 +241,7 @@ UberShader::UberShader(RenderPass* renderPass) {
230241
#define GAMMA 1
231242
#define FALSE_COLOR 2
232243
#define POS_NEG 3
244+
#define HASH 4
233245
234246
#define ERROR 0
235247
#define ABSOLUTE_ERROR 1
@@ -272,6 +284,13 @@ UberShader::UberShader(RenderPass* renderPass) {
272284
}
273285
}
274286
287+
float3 hash(float3 co){
288+
co *= mix(float3(1.0f), float3(1.0f / 1024.0f), step(1024.0f, abs(co)));
289+
co *= mix(float3(1.0f), float3(1.0f / 1024.0f), step(1024.0f, abs(co)));
290+
co *= mix(float3(1.0f), float3(1.0f / 1024.0f), step(1024.0f, abs(co)));
291+
return 2.0f * abs(fract(abs(fract(dot(co.xyz, float3(115.191742f, 64.0546951f, 124.512291f))) - 0.5f) * float3(1368.46143f, 1523.2019f, 1034.50476f)) - 0.5f);
292+
}
293+
275294
float3 applyTonemap(float3 col, float4 background, int tonemap, float offset, float gamma, texture2d<float, access::sample> colormap, sampler colormapSampler) {
276295
switch (tonemap) {
277296
case SRGB:
@@ -286,6 +305,8 @@ UberShader::UberShader(RenderPass* renderPass) {
286305
return falseColor(log2(average(col)+0.03125f) / 10.0f + 0.5f, colormap, colormapSampler) + (background.rgb - falseColor(0.0f, colormap, colormapSampler)) * background.a;
287306
case POS_NEG:
288307
return float3(-average(min(col, float3(0.0f))) * 2.0f, average(max(col, float3(0.0f))) * 2.0f, 0.0f) + background.rgb * background.a;
308+
case HASH:
309+
return hash(col) + (background.rgb - hash(float3(offset))) * background.a;
289310
}
290311
return float3(0.0f);
291312
}

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ int mainFunc(const vector<string>& arguments) {
233233
"Gamma - Gamma curve\n"
234234
"FC - False Color\n"
235235
"PN - Positive=Green, Negative=Red\n"
236+
"Hash - Hash values to random colors\n"
236237
"Default is sRGB.",
237238
{'t', "tonemap"},
238239
};

0 commit comments

Comments
 (0)