Add color space conversions on input and output.
[movit] / effect_chain.cpp
index 2bae9ed..5d9eaa3 100644 (file)
@@ -17,7 +17,7 @@
 #include "texture_enum.h"
 
 EffectChain::EffectChain(unsigned width, unsigned height)
-       : width(width), height(height), finalized(false) {}
+       : width(width), height(height), use_srgb_texture_format(false), finalized(false) {}
 
 void EffectChain::add_input(const ImageFormat &format)
 {
@@ -34,13 +34,13 @@ void EffectChain::add_output(const ImageFormat &format)
 Effect *instantiate_effect(EffectId effect)
 {
        switch (effect) {
-       case GAMMA_CONVERSION:
+       case EFFECT_GAMMA_EXPANSION:
                return new GammaExpansionEffect();
-       case RGB_PRIMARIES_CONVERSION:
-               return new GammaExpansionEffect();
-       case LIFT_GAMMA_GAIN:
+       case EFFECT_GAMMA_COMPRESSION:
+               return new GammaCompressionEffect();
+       case EFFECT_LIFT_GAMMA_GAIN:
                return new LiftGammaGainEffect();
-       case SATURATION:
+       case EFFECT_SATURATION:
                return new SaturationEffect();
        }
        assert(false);
@@ -48,9 +48,14 @@ Effect *instantiate_effect(EffectId effect)
 
 void EffectChain::normalize_to_linear_gamma()
 {
-       GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
-       gamma_conversion->set_int("source_curve", current_gamma_curve);
-       effects.push_back(gamma_conversion);
+       if (current_gamma_curve == GAMMA_sRGB) {
+               // TODO: check if the extension exists
+               use_srgb_texture_format = true;
+       } else {
+               GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
+               gamma_conversion->set_int("source_curve", current_gamma_curve);
+               effects.push_back(gamma_conversion);
+       }
        current_gamma_curve = GAMMA_LINEAR;
 }
 
@@ -127,7 +132,13 @@ std::string replace_prefix(const std::string &text, const std::string &prefix)
 
 void EffectChain::finalize()
 {
-       // TODO: If we want a non-sRGB output color space, convert.
+       if (current_color_space != output_format.color_space) {
+               ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
+               colorspace_conversion->set_int("source_space", current_color_space);
+               colorspace_conversion->set_int("destination_space", output_format.color_space);
+               effects.push_back(colorspace_conversion);
+               current_color_space = output_format.color_space;
+       }
 
        if (current_gamma_curve != output_format.gamma_curve) {
                if (current_gamma_curve != GAMMA_LINEAR) {
@@ -183,11 +194,15 @@ void EffectChain::render_to_screen(unsigned char *src)
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
 
-       // TODO: use sRGB textures if applicable
+       GLenum internal_format = GL_RGBA8;
+       if (use_srgb_texture_format) {
+               internal_format = GL_SRGB8;
+       }
+
        if (input_format.pixel_format == FORMAT_RGB) {
-               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, src);
+               glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, src);
        } else if (input_format.pixel_format == FORMAT_RGBA) {
-               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, src);
+               glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, src);
        } else {
                assert(false);
        }