From 8a0a3ca94bdd26057ff1ed5f9e13273cc0476e10 Mon Sep 17 00:00:00 2001 From: Andrew Kensler Date: Wed, 10 Jul 2024 22:27:15 -0700 Subject: [PATCH] Add tonemapper that hashes to a random color --- include/tev/Common.h | 1 + src/Common.cpp | 2 ++ src/ImageViewer.cpp | 10 +++++++--- src/UberShader.cpp | 14 ++++++++++++++ src/main.cpp | 1 + 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/include/tev/Common.h b/include/tev/Common.h index f6a3837..62c352b 100644 --- a/include/tev/Common.h +++ b/include/tev/Common.h @@ -373,6 +373,7 @@ enum ETonemap : int { Gamma, FalseColor, PositiveNegative, + Hash, // This enum value should never be used directly. // It facilitates looping over all members of this enum. diff --git a/src/Common.cpp b/src/Common.cpp index 42b5eb2..84cff39 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -232,6 +232,8 @@ ETonemap toTonemap(string name) { return FalseColor; } else if (name == "POSITIVENEGATIVE" || name == "POSNEG" || name == "PN" ||name == "+-") { return PositiveNegative; + } else if (name == "HASH") { + return Hash; } else { return SRGB; } diff --git a/src/ImageViewer.cpp b/src/ImageViewer.cpp index 3871d26..2be6042 100644 --- a/src/ImageViewer.cpp +++ b/src/ImageViewer.cpp @@ -25,7 +25,7 @@ using namespace std; namespace tev { -static const int SIDEBAR_MIN_WIDTH = 230; +static const int SIDEBAR_MIN_WIDTH = 245; static const float CROP_MIN_SIZE = 3; ImageViewer::ImageViewer( @@ -214,7 +214,7 @@ ImageViewer::ImageViewer( // Tonemap options { mTonemapButtonContainer = new Widget{mSidebarLayout}; - mTonemapButtonContainer->set_layout(new GridLayout{Orientation::Horizontal, 4, Alignment::Fill, 5, 2}); + mTonemapButtonContainer->set_layout(new GridLayout{Orientation::Horizontal, 5, Alignment::Fill, 5, 2}); auto makeTonemapButton = [&](const string& name, function callback) { auto button = new Button{mTonemapButtonContainer, name}; @@ -228,6 +228,7 @@ ImageViewer::ImageViewer( makeTonemapButton("Gamma", [this]() { setTonemap(ETonemap::Gamma); }); makeTonemapButton("FC", [this]() { setTonemap(ETonemap::FalseColor); }); makeTonemapButton("+/-", [this]() { setTonemap(ETonemap::PositiveNegative); }); + makeTonemapButton("Hash", [this]() { setTonemap(ETonemap::Hash); }); setTonemap(ETonemap::SRGB); @@ -244,7 +245,10 @@ ImageViewer::ImageViewer( "False-color visualization\n\n" "+/-\n" - "Positive=Green, Negative=Red" + "Positive=Green, Negative=Red\n\n" + + "Hash\n" + "Hash values to random colors" ); } diff --git a/src/UberShader.cpp b/src/UberShader.cpp index 6a1167d..09cdab4 100644 --- a/src/UberShader.cpp +++ b/src/UberShader.cpp @@ -51,6 +51,7 @@ UberShader::UberShader(RenderPass* renderPass) { #define GAMMA 1 #define FALSE_COLOR 2 #define POS_NEG 3 + #define HASH 4 #define ERROR 0 #define ABSOLUTE_ERROR 1 @@ -117,6 +118,10 @@ UberShader::UberShader(RenderPass* renderPass) { } } + vec3 hash(vec3 co){ + return fract(cos(dot(co.xyz, vec3(61.5499458, -40.7123604, 34.8567848))) * vec3(-41882.2148, 62285.1367, 38605.582)); + } + vec3 applyTonemap(vec3 col, vec4 background) { if (tonemap == SRGB) { col = col + @@ -129,6 +134,8 @@ UberShader::UberShader(RenderPass* renderPass) { return falseColor(log2(average(col)+0.03125) / 10.0 + 0.5) + (background.rgb - falseColor(0.0)) * background.a; } else if (tonemap == POS_NEG) { return vec3(-average(min(col, vec3(0.0))) * 2.0, average(max(col, vec3(0.0))) * 2.0, 0.0) + background.rgb * background.a; + } else if (tonemap == HASH) { + return hash(col) + (background.rgb - hash(vec3(offset))) * background.a; } return vec3(0.0); } @@ -230,6 +237,7 @@ UberShader::UberShader(RenderPass* renderPass) { #define GAMMA 1 #define FALSE_COLOR 2 #define POS_NEG 3 + #define HASH 4 #define ERROR 0 #define ABSOLUTE_ERROR 1 @@ -272,6 +280,10 @@ UberShader::UberShader(RenderPass* renderPass) { } } + float3 hash(float3 co){ + return fract(cos(dot(co.xyz, float3(61.5499458f, -40.7123604f, 34.8567848f))) * float3(-41882.2148f, 62285.1367f, 38605.582f)); + } + float3 applyTonemap(float3 col, float4 background, int tonemap, float offset, float gamma, texture2d colormap, sampler colormapSampler) { switch (tonemap) { case SRGB: @@ -286,6 +298,8 @@ UberShader::UberShader(RenderPass* renderPass) { return falseColor(log2(average(col)+0.03125f) / 10.0f + 0.5f, colormap, colormapSampler) + (background.rgb - falseColor(0.0f, colormap, colormapSampler)) * background.a; case POS_NEG: return float3(-average(min(col, float3(0.0f))) * 2.0f, average(max(col, float3(0.0f))) * 2.0f, 0.0f) + background.rgb * background.a; + case HASH: + return hash(col) + (background.rgb - hash(float3(offset))) * background.a; } return float3(0.0f); } diff --git a/src/main.cpp b/src/main.cpp index 10d044a..40bd72b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -233,6 +233,7 @@ int mainFunc(const vector& arguments) { "Gamma - Gamma curve\n" "FC - False Color\n" "PN - Positive=Green, Negative=Red\n" + "Hash - Hash values to random colors\n" "Default is sRGB.", {'t', "tonemap"}, };