x265 update 2.7, findobj upgrade, fix svg init, fix for dragchkbox buttonpress, add...
[goodguy/history.git] / cinelerra-5.1 / plugins / histogram / histogram.C
index b248785724062d84120cda952d7833fe1d0d2b24..33c7b21d62dfc0ad433ff6761befa077be34e686 100644 (file)
@@ -2,21 +2,21 @@
 /*
  * CINELERRA
  * Copyright (C) 2008-2012 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 <math.h>
@@ -36,7 +36,7 @@
 #include "language.h"
 #include "loadbalance.h"
 #include "playback3d.h"
-#include "cicolors.h"
+#include "bccolors.h"
 #include "vframe.h"
 #include "workarounds.h"
 
@@ -66,7 +66,7 @@ REGISTER_PLUGIN(HistogramMain)
 HistogramMain::HistogramMain(PluginServer *server)
  : PluginVClient(server)
 {
-       
+
        engine = 0;
        for(int i = 0; i < HISTOGRAM_MODES; i++)
        {
@@ -86,7 +86,7 @@ HistogramMain::HistogramMain(PluginServer *server)
 
 HistogramMain::~HistogramMain()
 {
-       
+
        for(int i = 0; i < HISTOGRAM_MODES;i++)
        {
                delete [] lookup[i];
@@ -96,7 +96,7 @@ HistogramMain::~HistogramMain()
        delete engine;
 }
 
-const char* HistogramMain::plugin_title() { return _("Histogram"); }
+const char* HistogramMain::plugin_title() { return N_("Histogram"); }
 int HistogramMain::is_realtime() { return 1; }
 
 
@@ -159,7 +159,7 @@ void HistogramMain::update_gui()
        {
                ((HistogramWindow*)thread->window)->lock_window("HistogramMain::update_gui");
                int reconfigure = load_configuration();
-               if(reconfigure) 
+               if(reconfigure)
                {
                        ((HistogramWindow*)thread->window)->update(1,
                                1,
@@ -271,7 +271,7 @@ void HistogramMain::read_data(KeyFrame *keyframe)
 
 }
 
-float HistogramMain::calculate_level(float input, 
+float HistogramMain::calculate_level(float input,
        int mode,
        int use_value)
 {
@@ -287,7 +287,7 @@ float HistogramMain::calculate_level(float input,
        {
                output = input;
        }
-       
+
 
 
        if(!EQUIV(config.gamma[mode], 0))
@@ -420,7 +420,7 @@ int HistogramMain::calculate_use_opengl()
 {
 // glHistogram doesn't work.
        int result = get_use_opengl() &&
-               !config.automatic && 
+               !config.automatic &&
                (!config.plot || !gui_open());
        return result;
 }
@@ -437,9 +437,9 @@ int HistogramMain::process_buffer(VFrame *frame,
        int use_opengl = calculate_use_opengl();
 
 //printf("%d\n", use_opengl);
-       read_frame(frame, 
-               0, 
-               start_position, 
+       read_frame(frame,
+               0,
+               start_position,
                frame_rate,
                use_opengl);
 
@@ -459,8 +459,8 @@ int HistogramMain::process_buffer(VFrame *frame,
 // table to avoid green borders
 
 
-       if(need_reconfigure || 
-               !lookup[0] || 
+       if(need_reconfigure ||
+               !lookup[0] ||
                config.automatic)
        {
 // Calculate new curves
@@ -475,8 +475,8 @@ int HistogramMain::process_buffer(VFrame *frame,
                        tabulate_curve(i, 1);
        }
 
-// printf("HistogramMain::process_buffer %d %f %f %f  %f %f %f  %f %f %f\n", 
-// __LINE__, 
+// printf("HistogramMain::process_buffer %d %f %f %f  %f %f %f  %f %f %f\n",
+// __LINE__,
 // config.low_input[HISTOGRAM_RED],
 // config.gamma[HISTOGRAM_RED],
 // config.high_input[HISTOGRAM_RED],
@@ -516,8 +516,8 @@ void HistogramMain::tabulate_curve(int subscript, int use_value)
                        case BC_RGBA8888:
                                for(i = 0; i < 0x100; i++)
                                {
-                                       lookup[subscript][i] = 
-                                               (int)(calculate_level((float)i / 0xff, subscript, use_value) * 
+                                       lookup[subscript][i] =
+                                               (int)(calculate_level((float)i / 0xff, subscript, use_value) *
                                                0xff);
                                        CLAMP(lookup[subscript][i], 0, 0xff);
                                }
@@ -526,8 +526,8 @@ void HistogramMain::tabulate_curve(int subscript, int use_value)
                        default:
                                for(i = 0; i < 0x10000; i++)
                                {
-                                       lookup[subscript][i] = 
-                                               (int)(calculate_level((float)i / 0xffff, subscript, use_value) * 
+                                       lookup[subscript][i] =
+                                               (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
                                                0xffff);
                                        CLAMP(lookup[subscript][i], 0, 0xffff);
                                }
@@ -546,8 +546,8 @@ void HistogramMain::tabulate_curve(int subscript, int use_value)
        {
                for(i = 0; i < 0x10000; i++)
                {
-                       preview_lookup[subscript][i] = 
-                               (int)(calculate_level((float)i / 0xffff, subscript, use_value) * 
+                       preview_lookup[subscript][i] =
+                               (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
                                0xffff);
                        CLAMP(preview_lookup[subscript][i], 0, 0xffff);
                }
@@ -571,7 +571,7 @@ int HistogramMain::handle_opengl()
                "       return texture2D(tex, gl_TexCoord[0].st);\n"
                "}\n";
 
-       static const char *head_frag = 
+       static const char *head_frag =
                "uniform vec4 low_input;\n"
                "uniform vec4 high_input;\n"
                "uniform vec4 gamma;\n"
@@ -597,7 +597,7 @@ int HistogramMain::handle_opengl()
 
 
 
-       static const char *apply_histogram_frag = 
+       static const char *apply_histogram_frag =
                APPLY_INPUT_CURVE("pixel.r", "low_input.r", "high_input.r", "gamma.r")
                APPLY_INPUT_CURVE("pixel.g", "low_input.g", "high_input.g", "gamma.g")
                APPLY_INPUT_CURVE("pixel.b", "low_input.b", "high_input.b", "gamma.b")
@@ -625,8 +625,14 @@ int HistogramMain::handle_opengl()
        get_output()->to_texture();
        get_output()->enable_opengl();
 
-       const char *shader_stack[] = { 0, 0, 0, 0, 0, 0, 0, 0, 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_interpolation = 0;
        int aggregate_gamma = 0;
        int aggregate_colorbalance = 0;
@@ -671,67 +677,34 @@ int HistogramMain::handle_opengl()
        if(!strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
                aggregate_colorbalance = 1;
 
-
 // The order of processing is fixed by this sequence
        if(aggregate_interpolation)
-               INTERPOLATE_COMPILE(shader_stack, 
-                       current_shader)
+               INTERPOLATE_COMPILE(shader_stack, current_shader);
 
        if(aggregate_gamma)
-               GAMMA_COMPILE(shader_stack, 
-                       current_shader, 
-                       aggregate_interpolation)
+               GAMMA_COMPILE(shader_stack, current_shader,
+                       aggregate_interpolation);
 
        if(aggregate_colorbalance)
-               COLORBALANCE_COMPILE(shader_stack, 
-                       current_shader, 
-                       aggregate_interpolation || aggregate_gamma)
+               COLORBALANCE_COMPILE(shader_stack, current_shader,
+                       aggregate_interpolation || aggregate_gamma);
 
+       shader_stack[current_shader++] = 
+               aggregate_interpolation || aggregate_gamma || aggregate_colorbalance ?
+                       histogram_get_pixel1 : histogram_get_pixel2;
 
-       if(aggregate_interpolation || aggregate_gamma || aggregate_colorbalance)
-               shader_stack[current_shader++] = histogram_get_pixel1;
-       else
-               shader_stack[current_shader++] = histogram_get_pixel2;
+       shader_stack[current_shader++] = head_frag;
+       shader_stack[current_shader++] = BC_CModels::is_yuv(get_output()->get_color_model()) ?
+                       get_yuv_frag : get_rgb_frag;
+       shader_stack[current_shader++] = apply_histogram_frag;
+       shader_stack[current_shader++] = BC_CModels::is_yuv(get_output()->get_color_model()) ?
+                       put_yuv_frag : put_rgb_frag;
 
-       unsigned int shader = 0;
-       switch(get_output()->get_color_model())
-       {
-               case BC_YUV888:
-               case BC_YUVA8888:
-                       shader_stack[current_shader++] = head_frag;
-                       shader_stack[current_shader++] = get_yuv_frag;
-                       shader_stack[current_shader++] = apply_histogram_frag;
-                       shader_stack[current_shader++] = put_yuv_frag;
-                       break;
-               default:
-                       shader_stack[current_shader++] = head_frag;
-                       shader_stack[current_shader++] = get_rgb_frag;
-                       shader_stack[current_shader++] = apply_histogram_frag;
-                       shader_stack[current_shader++] = put_rgb_frag;
-                       break;
-       }
+       shader_stack[current_shader] = 0;
+       unsigned int shader = VFrame::make_shader(shader_stack);
 
-       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],
-               shader_stack[8],
-               shader_stack[9],
-               shader_stack[10],
-               shader_stack[11],
-               shader_stack[12],
-               shader_stack[13],
-               shader_stack[14],
-               shader_stack[15],
-               0);
-
-// printf("HistogramMain::handle_opengl %d %d %d %d shader=%d\n", 
-// aggregate_interpolation, 
+// printf("HistogramMain::handle_opengl %d %d %d %d shader=%d\n",
+// aggregate_interpolation,
 // aggregate_gamma,
 // aggregate_colorbalance,
 // current_shader,
@@ -763,14 +736,15 @@ int HistogramMain::handle_opengl()
        {
                glUseProgram(shader);
                glUniform1i(glGetUniformLocation(shader, "tex"), 0);
-               if(aggregate_gamma) GAMMA_UNIFORMS(shader)
-               if(aggregate_interpolation) INTERPOLATE_UNIFORMS(shader)
-               if(aggregate_colorbalance) COLORBALANCE_UNIFORMS(shader)
+               if(aggregate_gamma) GAMMA_UNIFORMS(shader);
+               if(aggregate_interpolation) INTERPOLATE_UNIFORMS(shader);
+               if(aggregate_colorbalance) COLORBALANCE_UNIFORMS(shader);
                glUniform4fv(glGetUniformLocation(shader, "low_input"), 1, low_input);
                glUniform4fv(glGetUniformLocation(shader, "high_input"), 1, high_input);
                glUniform4fv(glGetUniformLocation(shader, "gamma"), 1, gamma);
                glUniform4fv(glGetUniformLocation(shader, "low_output"), 1, low_output);
                glUniform4fv(glGetUniformLocation(shader, "output_scale"), 1, output_scale);
+               if( need_color_matrix ) BC_GL_COLORS(shader);
        }
 
        get_output()->init_screen();
@@ -784,16 +758,16 @@ int HistogramMain::handle_opengl()
                glBegin(GL_TRIANGLES);
                glNormal3f(0, 0, 1.0);
 
-               glTexCoord2f(0.0 / get_output()->get_texture_w(), 
+               glTexCoord2f(0.0 / get_output()->get_texture_w(),
                        0.0 / get_output()->get_texture_h());
                glVertex3f(0.0, -(float)get_output()->get_h(), 0);
 
 
-               glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(), 
+               glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
                        (float)get_output()->get_h() / get_output()->get_texture_h());
                glVertex3f((float)get_output()->get_w(), -0.0, 0);
 
-               glTexCoord2f(0.0 / get_output()->get_texture_w(), 
+               glTexCoord2f(0.0 / get_output()->get_texture_w(),
                        (float)get_output()->get_h() / get_output()->get_texture_h());
                glVertex3f(0.0, -0.0, 0);
 
@@ -814,16 +788,16 @@ int HistogramMain::handle_opengl()
                glNormal3f(0, 0, 1.0);
 
 
-               glTexCoord2f(0.0 / get_output()->get_texture_w(), 
+               glTexCoord2f(0.0 / get_output()->get_texture_w(),
                        0.0 / get_output()->get_texture_h());
                glVertex3f(0.0, -(float)get_output()->get_h(), 0);
 
-               glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(), 
+               glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
                        0.0 / get_output()->get_texture_h());
-               glVertex3f((float)get_output()->get_w(), 
+               glVertex3f((float)get_output()->get_w(),
                        -(float)get_output()->get_h(), 0);
 
-               glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(), 
+               glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
                        (float)get_output()->get_h() / get_output()->get_texture_h());
                glVertex3f((float)get_output()->get_w(), -0.0, 0);
 
@@ -855,7 +829,7 @@ HistogramPackage::HistogramPackage()
 
 
 
-HistogramUnit::HistogramUnit(HistogramEngine *server, 
+HistogramUnit::HistogramUnit(HistogramEngine *server,
        HistogramMain *plugin)
  : LoadClient(server)
 {
@@ -957,7 +931,7 @@ void HistogramUnit::process_package(LoadPackage *package)
                                y = (row[0] << 8) | row[0];
                                u = (row[1] << 8) | row[1];
                                v = (row[2] << 8) | row[2];
-                               plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
+                               YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
                                HISTOGRAM_TAIL(3)
                                break;
                        case BC_RGBA8888:
@@ -979,7 +953,7 @@ void HistogramUnit::process_package(LoadPackage *package)
                                y = (row[0] << 8) | row[0];
                                u = (row[1] << 8) | row[1];
                                v = (row[2] << 8) | row[2];
-                               plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
+                               YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
                                HISTOGRAM_TAIL(4)
                                break;
                        case BC_RGB161616:
@@ -994,7 +968,7 @@ void HistogramUnit::process_package(LoadPackage *package)
                                y = row[0];
                                u = row[1];
                                v = row[2];
-                               plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
+                               YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
                                HISTOGRAM_TAIL(3)
                                break;
                        case BC_RGBA16161616:
@@ -1009,7 +983,7 @@ void HistogramUnit::process_package(LoadPackage *package)
                                y = row[0];
                                u = row[1];
                                v = row[2];
-                               plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
+                               YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
                                HISTOGRAM_TAIL(4)
                                break;
                }
@@ -1060,7 +1034,7 @@ void HistogramUnit::process_package(LoadPackage *package)
                                v = row[2]; \
                        } \
  \
-                       plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
+                       YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
  \
 /* Look up in RGB domain */ \
                        r = lookup_r[r]; \
@@ -1068,7 +1042,7 @@ void HistogramUnit::process_package(LoadPackage *package)
                        b = lookup_b[b]; \
  \
 /* Convert to 16 bit YUV */ \
-                       plugin->yuv.rgb_to_yuv_16(r, g, b, y, u, v); \
+                       YUV::yuv.rgb_to_yuv_16(r, g, b, y, u, v); \
  \
                        if(max == 0xff) \
                        { \
@@ -1163,8 +1137,8 @@ void HistogramUnit::process_package(LoadPackage *package)
 
 
 
-HistogramEngine::HistogramEngine(HistogramMain *plugin, 
-       int total_clients, 
+HistogramEngine::HistogramEngine(HistogramMain *plugin,
+       int total_clients,
        int total_packages)
  : LoadServer(total_clients, total_packages)
 {