opengl colorspace + BT2020
[goodguy/history.git] / cinelerra-5.1 / plugins / colorbalance / colorbalance.C
index 13d5e629ace83b514f6b05e934f97ca9331d2ed5..16d26ef9fdb0bb96e814b476371d44c17d025e5e 100644 (file)
@@ -2,23 +2,24 @@
 /*
  * CINELERRA
  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
+ *
  */
 
+#include "bccolors.h"
 #include "filexml.h"
 #include "clip.h"
 #include "colorbalance.h"
@@ -51,10 +52,10 @@ ColorBalanceConfig::ColorBalanceConfig()
 
 int ColorBalanceConfig::equivalent(ColorBalanceConfig &that)
 {
-       return (cyan == that.cyan && 
-               magenta == that.magenta && 
-               yellow == that.yellow && 
-               lock_params == that.lock_params && 
+       return (cyan == that.cyan &&
+               magenta == that.magenta &&
+               yellow == that.yellow &&
+               lock_params == that.lock_params &&
        preserve == that.preserve);
 }
 
@@ -67,10 +68,10 @@ void ColorBalanceConfig::copy_from(ColorBalanceConfig &that)
     preserve = that.preserve;
 }
 
-void ColorBalanceConfig::interpolate(ColorBalanceConfig &prev, 
-       ColorBalanceConfig &next, 
-       int64_t prev_frame, 
-       int64_t next_frame, 
+void ColorBalanceConfig::interpolate(ColorBalanceConfig &prev,
+       ColorBalanceConfig &next,
+       int64_t prev_frame,
+       int64_t next_frame,
        int64_t current_frame)
 {
        double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
@@ -259,8 +260,8 @@ printf("PROCESS_F %f\n", cyan_f); \
                switch(input->get_color_model())
                {
                        case BC_RGB888:
-                               PROCESS(yuv.yuv_to_rgb_8, yuv.rgb_to_yuv_8, 
-                                       r_lookup_8, g_lookup_8, b_lookup_8, 
+                               PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
+                                       r_lookup_8, g_lookup_8, b_lookup_8,
                                        unsigned char, 0xff, 3, 0);
                                break;
 
@@ -269,42 +270,42 @@ printf("PROCESS_F %f\n", cyan_f); \
                                break;
 
                        case BC_YUV888:
-                               PROCESS(yuv.yuv_to_rgb_8, yuv.rgb_to_yuv_8, 
-                                       r_lookup_8, g_lookup_8, b_lookup_8, 
+                               PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
+                                       r_lookup_8, g_lookup_8, b_lookup_8,
                                        unsigned char, 0xff, 3, 1);
                                break;
-                       
+
                        case BC_RGBA_FLOAT:
                                PROCESS_F(4);
                                break;
 
                        case BC_RGBA8888:
-                               PROCESS(yuv.yuv_to_rgb_8, yuv.rgb_to_yuv_8, 
-                                       r_lookup_8, g_lookup_8, b_lookup_8, 
+                               PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
+                                       r_lookup_8, g_lookup_8, b_lookup_8,
                                        unsigned char, 0xff, 4, 0);
                                break;
 
                        case BC_YUVA8888:
-                               PROCESS(yuv.yuv_to_rgb_8, yuv.rgb_to_yuv_8, 
-                                       r_lookup_8, g_lookup_8, b_lookup_8, 
+                               PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
+                                       r_lookup_8, g_lookup_8, b_lookup_8,
                                        unsigned char, 0xff, 4, 1);
                                break;
-                       
+
                        case BC_YUV161616:
-                               PROCESS(yuv.yuv_to_rgb_16, yuv.rgb_to_yuv_16, 
-                                       r_lookup_16, g_lookup_16, b_lookup_16, 
+                               PROCESS(YUV::yuv.yuv_to_rgb_16, YUV::yuv.rgb_to_yuv_16,
+                                       r_lookup_16, g_lookup_16, b_lookup_16,
                                        u_int16_t, 0xffff, 3, 1);
                                break;
 
                        case BC_YUVA16161616:
-                               PROCESS(yuv.yuv_to_rgb_16, yuv.rgb_to_yuv_16, 
-                                       r_lookup_16, g_lookup_16, b_lookup_16, 
+                               PROCESS(YUV::yuv.yuv_to_rgb_16, YUV::yuv.rgb_to_yuv_16,
+                                       r_lookup_16, g_lookup_16, b_lookup_16,
                                        u_int16_t, 0xffff, 4, 1);
                                break;
                }
 
-               
-               
+
+
                output_lock.unlock();
        }
 }
@@ -317,12 +318,12 @@ ColorBalanceMain::ColorBalanceMain(PluginServer *server)
 {
        need_reconfigure = 1;
        engine = 0;
-       
+
 }
 
 ColorBalanceMain::~ColorBalanceMain()
 {
-       
+
 
 
        if(engine)
@@ -355,7 +356,7 @@ int ColorBalanceMain::reconfigure()
 
        RECONFIGURE(r_lookup_8, g_lookup_8, b_lookup_8, 0xff);
        RECONFIGURE(r_lookup_16, g_lookup_16, b_lookup_16, 0xffff);
-       
+
        return 0;
 }
 
@@ -402,27 +403,23 @@ int ColorBalanceMain::test_boundary(float &value)
 
 int ColorBalanceMain::synchronize_params(ColorBalanceSlider *slider, float difference)
 {
-       if(thread && config.lock_params)
-    {
-           if(slider != ((ColorBalanceWindow*)thread->window)->cyan)
-        {
-               config.cyan += difference;
-            test_boundary(config.cyan);
-               ((ColorBalanceWindow*)thread->window)->cyan->update((int64_t)config.cyan);
-        }
-           if(slider != ((ColorBalanceWindow*)thread->window)->magenta)
-        {
-               config.magenta += difference;
-            test_boundary(config.magenta);
-               ((ColorBalanceWindow*)thread->window)->magenta->update((int64_t)config.magenta);
-        }
-           if(slider != ((ColorBalanceWindow*)thread->window)->yellow)
-        {
-               config.yellow += difference;
-            test_boundary(config.yellow);
-               ((ColorBalanceWindow*)thread->window)->yellow->update((int64_t)config.yellow);
-        }
-    }
+       if(thread && config.lock_params) {
+               if(slider != ((ColorBalanceWindow*)thread->window)->cyan) {
+                       config.cyan += difference;
+                       test_boundary(config.cyan);
+                       ((ColorBalanceWindow*)thread->window)->cyan->update((int64_t)config.cyan);
+               }
+               if(slider != ((ColorBalanceWindow*)thread->window)->magenta) {
+                       config.magenta += difference;
+                       test_boundary(config.magenta);
+                       ((ColorBalanceWindow*)thread->window)->magenta->update((int64_t)config.magenta);
+               }
+               if(slider != ((ColorBalanceWindow*)thread->window)->yellow) {
+                       config.yellow += difference;
+                       test_boundary(config.yellow);
+                       ((ColorBalanceWindow*)thread->window)->yellow->update((int64_t)config.yellow);
+               }
+       }
        return 0;
 }
 
@@ -479,8 +476,8 @@ int ColorBalanceMain::process_buffer(VFrame *frame,
        get_aggregation(&aggregate_interpolate,
                &aggregate_gamma);
 
-       if(!EQUIV(config.cyan, 0) || 
-               !EQUIV(config.magenta, 0) || 
+       if(!EQUIV(config.cyan, 0) ||
+               !EQUIV(config.magenta, 0) ||
                !EQUIV(config.yellow, 0) ||
                (get_use_opengl() &&
                        (aggregate_interpolate ||
@@ -493,12 +490,12 @@ int ColorBalanceMain::process_buffer(VFrame *frame,
                        if(next_effect_is(_("Histogram"))) return 0;
                        return run_opengl();
                }
-       
+
                for(int i = 0; i < total_engines; i++)
                {
-                       engine[i]->start_process_frame(frame, 
-                               frame, 
-                               frame->get_h() * i / total_engines, 
+                       engine[i]->start_process_frame(frame,
+                               frame,
+                               frame->get_h() * i / total_engines,
                                frame->get_h() * (i + 1) / total_engines);
                }
 
@@ -604,9 +601,14 @@ int ColorBalanceMain::handle_opengl()
        get_output()->to_texture();
        get_output()->enable_opengl();
 
-       unsigned int shader = 0;
-       const char *shader_stack[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
-       int current_shader = 0;
+        const char *shader_stack[16];
+        memset(shader_stack,0, sizeof(shader_stack));
+        int current_shader = 0;
+
+       int need_color_matrix = BC_CModels::is_yuv(get_output()->get_color_model()) ? 1 : 0;
+       if( need_color_matrix )
+               shader_stack[current_shader++] = bc_gl_colors;
+
        int aggregate_interpolate = 0;
        int aggregate_gamma = 0;
 
@@ -615,28 +617,18 @@ int ColorBalanceMain::handle_opengl()
 
 //printf("ColorBalanceMain::handle_opengl %d %d\n", aggregate_interpolate, aggregate_gamma);
        if(aggregate_interpolate)
-               INTERPOLATE_COMPILE(shader_stack, current_shader)
+               INTERPOLATE_COMPILE(shader_stack, current_shader);
 
        if(aggregate_gamma)
-               GAMMA_COMPILE(shader_stack, current_shader, aggregate_interpolate)
-
-       COLORBALANCE_COMPILE(shader_stack, 
-               current_shader, 
-               aggregate_gamma || aggregate_interpolate)
-
-       shader = VFrame::make_shader(0, 
-               shader_stack[0], 
-               shader_stack[1], 
-               shader_stack[2], 
-               shader_stack[3], 
-               shader_stack[4], 
-               shader_stack[5], 
-               shader_stack[6], 
-               shader_stack[7], 
-               0);
-
-       if(shader > 0)
-       {
+               GAMMA_COMPILE(shader_stack, current_shader,
+                       aggregate_interpolate);
+
+       COLORBALANCE_COMPILE(shader_stack, current_shader,
+               aggregate_gamma || aggregate_interpolate);
+
+       shader_stack[current_shader] = 0;
+        unsigned int shader = VFrame::make_shader(shader_stack);
+       if( shader > 0 ) {
                glUseProgram(shader);
                glUniform1i(glGetUniformLocation(shader, "tex"), 0);
 
@@ -644,7 +636,7 @@ int ColorBalanceMain::handle_opengl()
                if(aggregate_gamma) GAMMA_UNIFORMS(shader);
 
                COLORBALANCE_UNIFORMS(shader);
-
+               if( need_color_matrix ) BC_GL_COLORS(shader);
        }
 
        get_output()->init_screen();