anonymous contribution for improved Chromakey plugin to include sliders, textbox...
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / sketcher / sketcher.C
index 2b392327176b85cdb35e3a22cd8283dd6ff85d3c..151b42e11a0d6efadbe3a041bfb84d8b0443d7a3 100644 (file)
 #include "edl.h"
 #include "edlsession.h"
 #include "filexml.h"
+#include "keyframes.h"
 #include "overlayframe.h"
 #include "pluginserver.h"
 #include "preferences.h"
 #include "sketcher.h"
 #include "sketcherwindow.h"
+#include "transportque.inc"
 #include "language.h"
 #include "vframe.h"
 
@@ -163,20 +165,24 @@ void SketcherCurve::read_data(FileXML &input)
        bclamp(pen, 0, PEN_SZ-1);
 }
 
-int Sketcher::new_curve(int pen, int width, int color)
+int SketcherConfig::new_curve(int pen, int width, int color)
 {
-       SketcherCurves &curves = config.curves;
        int k = curves.size(), id = 1;
        for( int i=k; --i>=0; ) {
-               int n = config.curves[i]->id;
+               int n = curves[i]->id;
                if( n >= id ) id = n + 1;
        }
        SketcherCurve *cv = new SketcherCurve(id, pen, width, color);
        curves.append(cv);
-       config.cv_selected = k;
        return k;
 }
 
+int Sketcher::new_curve(int pen, int width, int color)
+{
+       cv_selected = config.new_curve(pen, width, color);
+       return cv_selected;
+}
+
 int Sketcher::new_curve()
 {
        return new_curve(PEN_XLANT, 1, CV_COLOR);
@@ -198,7 +204,7 @@ int Sketcher::new_point(SketcherCurve *cv, int arc, coord x, coord y, int idx)
 
 int Sketcher::new_point(int idx, int arc)
 {
-       int ci = config.cv_selected;
+       int ci = cv_selected;
        if( ci < 0 || ci >= config.curves.size() )
                return -1;
        SketcherCurve *cv = config.curves[ci];
@@ -241,10 +247,7 @@ REGISTER_PLUGIN(Sketcher)
 
 SketcherConfig::SketcherConfig()
 {
-       drag = 1;
        aliasing = 0;
-       cv_selected = 0;
-       pt_selected = 0;
 }
 SketcherConfig::~SketcherConfig()
 {
@@ -252,10 +255,7 @@ SketcherConfig::~SketcherConfig()
 
 int SketcherConfig::equivalent(SketcherConfig &that)
 {
-       if( this->drag != that.drag ) return 0;
        if( this->aliasing != that.aliasing ) return 0;
-       if( this->cv_selected != that.cv_selected ) return 0;
-       if( this->pt_selected != that.pt_selected ) return 0;
        if( this->curves.size() != that.curves.size() ) return 0;
        for( int i=0, n=curves.size(); i<n; ++i ) {
                if( !curves[i]->equivalent(*that.curves[i]) ) return 0;
@@ -265,10 +265,7 @@ int SketcherConfig::equivalent(SketcherConfig &that)
 
 void SketcherConfig::copy_from(SketcherConfig &that)
 {
-       this->drag = that.drag;
        this->aliasing = that.aliasing;
-       this->cv_selected = that.cv_selected;
-       this->pt_selected = that.pt_selected;
        int m = curves.size(), n = that.curves.size();
        while( m > n ) curves.remove_object_number(--m);
        while( m < n ) { curves.append(new SketcherCurve());  ++m; }
@@ -278,9 +275,6 @@ void SketcherConfig::copy_from(SketcherConfig &that)
 void SketcherConfig::interpolate(SketcherConfig &prev, SketcherConfig &next,
                long prev_frame, long next_frame, long current_frame)
 {
-       this->cv_selected = prev.cv_selected;
-       this->pt_selected = prev.pt_selected;
-       this->drag = prev.drag;
        this->aliasing = prev.aliasing;
 
        double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
@@ -340,6 +334,10 @@ Sketcher::Sketcher(PluginServer *server)
        img = 0;
        out = 0;
        overlay_frame = 0;
+
+       drag = 1;
+       cv_selected = 0;
+       pt_selected = 0;
 }
 
 Sketcher::~Sketcher()
@@ -356,21 +354,36 @@ int Sketcher::is_synthesis() { return 1; }
 NEW_WINDOW_MACRO(Sketcher, SketcherWindow);
 LOAD_CONFIGURATION_MACRO(Sketcher, SketcherConfig)
 
-void Sketcher::save_data(KeyFrame *keyframe)
+void Sketcher::render_gui(void *data)
+{
+       Sketcher *sketcher = (Sketcher *)data;
+       sketcher->drag = drag;
+       sketcher->cv_selected = cv_selected;
+       sketcher->pt_selected = pt_selected;
+}
+
+int Sketcher::is_dragging()
+{
+       drag = 0;
+       cv_selected = -1;
+       pt_selected = -1;
+       send_render_gui(this);
+       return drag;
+}
+
+
+void SketcherConfig::save_data(KeyFrame *keyframe)
 {
        FileXML output;
 // cause data to be stored directly in text
        output.set_shared_output(keyframe->xbuf);
 
        output.tag.set_title("SKETCHER");
-       output.tag.set_property("DRAG", config.drag);
-       output.tag.set_property("ALIASING", config.aliasing);
-       output.tag.set_property("CV_SELECTED", config.cv_selected);
-       output.tag.set_property("PT_SELECTED", config.pt_selected);
+       output.tag.set_property("ALIASING", aliasing);
        output.append_tag();
        output.append_newline();
-       for( int i=0,n=config.curves.size(); i<n; ++i ) {
-               config.curves[i]->save_data(output);
+       for( int i=0,n=curves.size(); i<n; ++i ) {
+               curves[i]->save_data(output);
        }
        output.tag.set_title("/SKETCHER");
        output.append_tag();
@@ -378,25 +391,27 @@ void Sketcher::save_data(KeyFrame *keyframe)
        output.terminate_string();
 }
 
-void Sketcher::read_data(KeyFrame *keyframe)
+void Sketcher::save_data(KeyFrame *keyframe)
+{
+       config.save_data(keyframe);
+}
+
+void SketcherConfig::read_data(KeyFrame *keyframe)
 {
        FileXML input;
        input.set_shared_input(keyframe->xbuf);
-       config.curves.remove_all_objects();
+       curves.remove_all_objects();
        int result = 0;
        SketcherCurve *cv = 0;
 
        while( !(result=input.read_tag()) ) {
                if( input.tag.title_is("SKETCHER") ) {
-                       config.drag = input.tag.get_property("DRAG", config.drag);
-                       config.aliasing = input.tag.get_property("ALIASING", config.aliasing);
-                       config.cv_selected = input.tag.get_property("CV_SELECTED", 0);
-                       config.pt_selected = input.tag.get_property("PT_SELECTED", 0);
+                       aliasing = input.tag.get_property("ALIASING", aliasing);
                }
                else if( !strncmp(input.tag.get_title(),"CURVE_",6) ) {
                        cv = new SketcherCurve();
                        cv->read_data(input);
-                       config.curves.append(cv);
+                       curves.append(cv);
                }
                else if( !strncmp(input.tag.get_title(),"/CURVE_",7) )
                        cv = 0;
@@ -407,13 +422,79 @@ void Sketcher::read_data(KeyFrame *keyframe)
                                cv->points.append(pt);
                        }
                        else
-                               printf("Sketcher::read_data: no curve for point\n");
+                               printf("SketcherConfig::read_data: no curve for point\n");
                }
        }
 
+       limits();
+}
+
+void Sketcher::read_data(KeyFrame *keyframe)
+{
+       config.read_data(keyframe);
        if( !config.curves.size() )
                new_curve();
-       config.limits();
+}
+
+void SketcherPoint::update_parameter(SketcherPoint *prev, SketcherPoint *src)
+{
+       if( prev->arc != src->arc ) arc = src->arc;
+       if( prev->x != src->x ) x = src->x;
+       if( prev->y != src->y ) y = src->y;
+}
+
+void SketcherCurve::update_parameter(SketcherCurve *prev, SketcherCurve *src)
+{
+       if( prev->pen != src->pen ) pen = src->pen;
+       if( prev->width != src->width ) width = src->width;
+       if( prev->color != src->color ) color = src->color;
+       int prev_points = prev->points.size();
+       int src_points = src->points.size();
+       int dst_points = this->points.size();
+       int npoints = bmin(prev_points, bmin(src_points, dst_points));
+       for( int i=0; i<npoints; ++i ) {
+               SketcherPoint *prev_point = prev->points[i];
+               SketcherPoint *src_point = src->points[i];
+               SketcherPoint *dst_point = this->points[i];
+               dst_point->update_parameter(prev_point, src_point);
+       }
+}
+
+void Sketcher::span_keyframes(KeyFrame *src, int64_t start, int64_t end)
+{
+       SketcherConfig src_config;
+       src_config.read_data(src);
+       KeyFrames *keyframes = (KeyFrames *)src->autos;
+       KeyFrame *prev = keyframes->get_prev_keyframe(start, PLAY_FORWARD);
+       SketcherConfig prev_config;
+       prev_config.read_data(prev);
+// Always update the first one
+       update_parameter(prev_config, src_config, prev);
+       KeyFrame *curr = (KeyFrame*)prev->next;
+       while( curr && curr->position < end ) {
+               update_parameter(prev_config, src_config, curr);
+               curr = (KeyFrame*)curr->next;
+       }
+}
+
+void Sketcher::update_parameter(SketcherConfig &prev_config, SketcherConfig &src_config,
+               KeyFrame *keyframe)
+{
+       SketcherConfig dst_config;
+       dst_config.read_data(keyframe);
+       if( prev_config.aliasing != src_config.aliasing )
+               dst_config.aliasing = src_config.aliasing;
+       int src_curves = src_config.curves.size();
+       int dst_curves = dst_config.curves.size();
+       int prev_curves = prev_config.curves.size();
+       int ncurves = bmin(prev_curves, bmin(src_curves, dst_curves));
+       for( int i=0; i<ncurves; ++i ) {
+               SketcherCurve *prev_curve = prev_config.curves[i];
+               SketcherCurve *src_curve = src_config.curves[i];
+               SketcherCurve *dst_curve = dst_config.curves[i];
+               dst_curve->update_parameter(prev_curve, src_curve);
+       }
+       dst_config.save_data(keyframe);
 }
 
 void Sketcher::update_gui()
@@ -767,11 +848,11 @@ int Sketcher::process_realtime(VFrame *input, VFrame *output)
                                1.f, TRANSFER_SRC_OVER, NEAREST_NEIGHBOR);
        }
 
-       if( config.drag ) {
+       if( is_dragging() ) {
                for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
                        SketcherCurve *cv = config.curves[ci];
                        for( int pi=0,m=cv->points.size(); pi<m; ++pi ) {
-                               int color = pi==config.pt_selected && ci==config.cv_selected ?
+                               int color = pi==pt_selected && ci==cv_selected ?
                                        RED : cv->color ; 
                                draw_point(out, cv->points[pi], color);
                        }
@@ -807,8 +888,7 @@ void SketcherCurves::dump()
 }
 void SketcherConfig::dump()
 {
-       printf("Config drag=%d, cv_selected=%d, pt_selected=%d %d curves\n",
-                       drag, cv_selected, pt_selected, curves.size());
+       printf("Config %d curves\n", curves.size());
        curves.dump();
 }