port 7.2 mods: align_edits foreground plugin refresh_frame tweak, rework soundlevel...
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / alpha / alpha.C
diff --git a/cinelerra-5.1/plugins/alpha/alpha.C b/cinelerra-5.1/plugins/alpha/alpha.C
new file mode 100644 (file)
index 0000000..87f141e
--- /dev/null
@@ -0,0 +1,229 @@
+
+/*
+ * CINELERRA
+ * Copyright (C) 2008-2019 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>
+#include <stdint.h>
+#include <string.h>
+
+#include "alpha.h"
+#include "filexml.h"
+#include "keyframe.h"
+#include "language.h"
+#include "vframe.h"
+
+REGISTER_PLUGIN(AlphaMain)
+
+
+AlphaConfig::AlphaConfig()
+{
+       a = 1.;
+}
+
+int AlphaConfig::equivalent(AlphaConfig &that)
+{
+       return a == that.a;
+}
+
+void AlphaConfig::copy_from(AlphaConfig &that)
+{
+       a = that.a;
+}
+
+void AlphaConfig::interpolate(AlphaConfig &prev, AlphaConfig &next,
+               long prev_frame, long next_frame, long current_frame)
+{
+       double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
+       double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
+
+       a = (prev.a * prev_scale + next.a * next_scale);
+}
+
+
+AlphaSlider::AlphaSlider(AlphaWindow *window, AlphaMain *plugin,
+               int x, int y, int w)
+ : BC_FSlider(x, y, 0, w, w, 0.f, 1.f, plugin->config.a)
+{
+       this->window = window;
+       this->plugin = plugin;
+       set_precision(0.001);
+}
+AlphaSlider::~AlphaSlider()
+{
+}
+
+int AlphaSlider::handle_event()
+{
+       plugin->config.a = get_value();
+       plugin->send_configure_change();
+       return 1;
+}
+
+#define ALPHA_W xS(400)
+#define ALPHA_H yS(60)
+
+AlphaWindow::AlphaWindow(AlphaMain *plugin)
+ : PluginClientWindow(plugin, ALPHA_W, ALPHA_H, ALPHA_W, ALPHA_H, 0)
+{
+       this->plugin = plugin;
+}
+
+AlphaWindow::~AlphaWindow()
+{
+}
+
+void AlphaWindow::create_objects()
+{
+       int x = xS(10), y = yS(10);
+       BC_Title *title;
+       add_subwindow(title = new BC_Title(x, y, _("Alpha:")));
+       y += title->get_h() + yS(5);
+       add_subwindow(alpha_slider = new AlphaSlider(this, plugin, x, y, xS(380)));
+       show_window();
+}
+
+void AlphaWindow::update()
+{
+       float alpha = plugin->config.a;
+       alpha_slider->update(alpha);
+}
+
+
+AlphaMain::AlphaMain(PluginServer *server)
+ : PluginVClient(server)
+{
+}
+
+AlphaMain::~AlphaMain()
+{
+}
+
+const char* AlphaMain::plugin_title() { return N_("Alpha"); }
+int AlphaMain::is_realtime() { return 1; }
+
+NEW_WINDOW_MACRO(AlphaMain, AlphaWindow)
+
+LOAD_CONFIGURATION_MACRO(AlphaMain, AlphaConfig)
+
+int AlphaMain::is_synthesis()
+{
+       return 1;
+}
+
+
+int AlphaMain::process_buffer(VFrame *frame,
+       int64_t start_position,
+       double frame_rate)
+{
+       load_configuration();
+
+       int color_model = frame->get_color_model();
+       int need_alpha = BC_CModels::has_alpha(color_model);
+       if( need_alpha ) {
+               read_frame(frame, 0, start_position,
+                       frame_rate, get_use_opengl());
+       }
+       int w = frame->get_w();
+       int h = frame->get_h();
+
+
+#define MAIN_LOOP(type, components, is_yuv, max) do { \
+       if( components == 4 ) { \
+               for( int y=0; y<h; ++y ) { \
+                       type *row = (type*)frame->get_rows()[y]; \
+                       for( int x=0; x<w; ++x ) { \
+                               row[3] = a * max; \
+                               row += components; \
+                       } \
+               } \
+       } \
+       else if( is_yuv ) { \
+               type ofs = (max+1)/2; \
+               for( int y=0; y<h; ++y ) { \
+                       type *row = (type*)frame->get_rows()[y]; \
+                       for( int x=0; x<w; ++x ) { \
+                               row[0] = row[0] * a; \
+                               row[1] = (row[1]-ofs) * a + ofs; \
+                               row[2] = (row[2]-ofs) * a + ofs; \
+                               row += components; \
+                       } \
+               } \
+       } \
+       else {\
+               for( int y=0; y<h; ++y ) { \
+                       type *row = (type*)frame->get_rows()[y]; \
+                       for( int x=0; x<w; ++x ) { \
+                               row[0] *= a; \
+                               row[1] *= a; \
+                               row[2] *= a; \
+                               row += components; \
+                       } \
+               } \
+       } \
+} while(0)
+
+       float a = config.a;
+       switch( frame->get_color_model() ) {
+       case BC_RGB888:     MAIN_LOOP(uint8_t, 3, 0, 0xff);  break;
+       case BC_RGB_FLOAT:  MAIN_LOOP(float,   3, 0, 1.0 );  break;
+       case BC_YUV888:     MAIN_LOOP(uint8_t, 3, 1, 0xff);  break;
+       case BC_RGBA8888:   MAIN_LOOP(uint8_t, 4, 0, 0xff);  break;
+       case BC_RGBA_FLOAT: MAIN_LOOP(float,   4, 0, 1.0 );  break;
+       case BC_YUVA8888:   MAIN_LOOP(uint8_t, 4, 1, 0xff);  break;
+       }
+
+       return 0;
+}
+
+
+void AlphaMain::update_gui()
+{
+       if( !thread ) return;
+       AlphaWindow *window = (AlphaWindow*)thread->window;
+       if( !window ) return;
+       if( !load_configuration() ) return;
+       window->lock_window("AlphaMain::update_gui");
+       window->update();
+       window->unlock_window();
+}
+
+
+void AlphaMain::save_data(KeyFrame *keyframe)
+{
+       FileXML output;
+       output.set_shared_output(keyframe->xbuf);
+       output.tag.set_title("ALPHA");
+       output.tag.set_property("A", config.a);
+       output.append_tag();
+       output.terminate_string();
+}
+
+void AlphaMain::read_data(KeyFrame *keyframe)
+{
+       FileXML input;
+       input.set_shared_input(keyframe->xbuf);
+
+       int result = 0;
+       while( !(result = input.read_tag()) ) {
+               if( input.tag.title_is("ALPHA") ) {
+                       config.a = input.tag.get_property("A", config.a);
+               }
+       }
+}
+