Move saturation out into its own effect, and hook up the control.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 18:35:17 +0000 (20:35 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 18:35:17 +0000 (20:35 +0200)
Makefile
effect_chain.cpp
effect_id.h
lift_gamma_gain_effect.cpp
lift_gamma_gain_effect.h
main.cpp
saturation_effect.cpp [new file with mode: 0644]
saturation_effect.glsl [new file with mode: 0644]
saturation_effect.h [new file with mode: 0644]

index 4dbaf12..b61f78b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ CXX=g++
 CXXFLAGS=-Wall -g
 LDFLAGS=-lSDL -lSDL_image -lGL
 OBJS=main.o util.o widgets.o effect.o effect_chain.o
-OBJS += lift_gamma_gain_effect.o gamma_expansion_effect.o gamma_compression_effect.o colorspace_conversion_effect.o
+OBJS += lift_gamma_gain_effect.o gamma_expansion_effect.o gamma_compression_effect.o colorspace_conversion_effect.o saturation_effect.o
 
 test: $(OBJS)
        $(CXX) -o test $(OBJS) $(LDFLAGS)
index 1ff57d7..2bae9ed 100644 (file)
@@ -13,6 +13,7 @@
 #include "gamma_compression_effect.h"
 #include "lift_gamma_gain_effect.h"
 #include "colorspace_conversion_effect.h"
+#include "saturation_effect.h"
 #include "texture_enum.h"
 
 EffectChain::EffectChain(unsigned width, unsigned height)
@@ -39,6 +40,8 @@ Effect *instantiate_effect(EffectId effect)
                return new GammaExpansionEffect();
        case LIFT_GAMMA_GAIN:
                return new LiftGammaGainEffect();
+       case SATURATION:
+               return new SaturationEffect();
        }
        assert(false);
 }
index af92ac0..20cd3b9 100644 (file)
@@ -8,6 +8,7 @@ enum EffectId {
 
        // Color.
        LIFT_GAMMA_GAIN,
+       SATURATION,
 };
 
 #endif // !defined(_EFFECT_ID_H)
index b32ed93..cd54773 100644 (file)
 LiftGammaGainEffect::LiftGammaGainEffect()
        : lift(0.0f, 0.0f, 0.0f),
          gamma(1.0f, 1.0f, 1.0f),
-         gain(1.0f, 1.0f, 1.0f),
-         saturation(1.0f)
+         gain(1.0f, 1.0f, 1.0f)
 {
        register_vec3("lift", (float *)&lift);
        register_vec3("gamma", (float *)&gamma);
        register_vec3("gain", (float *)&gain);
-       register_float("saturation", &saturation);
 }
 
 std::string LiftGammaGainEffect::output_glsl()
index 0971955..6224934 100644 (file)
@@ -12,7 +12,6 @@ public:
 
 private:
        RGBTriplet lift, gamma, gain;
-       float saturation;
 };
 
 #endif // !defined(_LIFT_GAMMA_GAIN_EFFECT_H)
index d823694..333cdfc 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -34,7 +34,7 @@ float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
 float saturation = 1.0f;
 
-void update_hsv(Effect *lift_gamma_gain_effect)
+void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect)
 {
        RGBTriplet lift(0.0f, 0.0f, 0.0f);
        RGBTriplet gamma(1.0f, 1.0f, 1.0f);
@@ -52,6 +52,8 @@ void update_hsv(Effect *lift_gamma_gain_effect)
        if (saturation < 0.0) {
                saturation = 0.0;
        }
+       ok = saturation_effect->set_float("saturation", saturation);
+       assert(ok);
 }
 
 void mouse(int x, int y)
@@ -182,6 +184,7 @@ int main(int argc, char **argv)
 
        chain.add_input(inout_format);
        Effect *lift_gamma_gain_effect = chain.add_effect(LIFT_GAMMA_GAIN);
+       Effect *saturation_effect = chain.add_effect(SATURATION);
        chain.add_output(inout_format);
        chain.finalize();
 
@@ -277,7 +280,7 @@ int main(int argc, char **argv)
 
                ++frame;
 
-               update_hsv(lift_gamma_gain_effect);
+               update_hsv(lift_gamma_gain_effect, saturation_effect);
                chain.render_to_screen(src_img);
                
                glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
diff --git a/saturation_effect.cpp b/saturation_effect.cpp
new file mode 100644 (file)
index 0000000..376e0fc
--- /dev/null
@@ -0,0 +1,15 @@
+#define GL_GLEXT_PROTOTYPES 1
+
+#include "saturation_effect.h"
+#include "util.h"
+
+SaturationEffect::SaturationEffect()
+       : saturation(1.0f)
+{
+       register_float("saturation", &saturation);
+}
+
+std::string SaturationEffect::output_glsl()
+{
+       return read_file("saturation_effect.glsl");
+}
diff --git a/saturation_effect.glsl b/saturation_effect.glsl
new file mode 100644 (file)
index 0000000..a68485a
--- /dev/null
@@ -0,0 +1,10 @@
+// Saturate/desaturate (in linear space).
+
+vec4 FUNCNAME(vec2 tc) {
+       vec4 x = LAST_INPUT(tc);
+
+       float luminance = dot(x.rgb, vec3(0.2126, 0.7152, 0.0722));
+       x.rgb = mix(vec3(luminance), x.rgb, PREFIX(saturation));
+
+       return x;
+}
diff --git a/saturation_effect.h b/saturation_effect.h
new file mode 100644 (file)
index 0000000..3f057e9
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef _SATURATION_EFFECT_H
+#define _SATURATION_EFFECT_H 1
+
+#include "effect.h"
+
+class SaturationEffect : public Effect {
+public:
+       SaturationEffect();
+       std::string output_glsl();
+
+private:
+       float saturation;
+};
+
+#endif // !defined(_SATURATION_EFFECT_H)