bsd lang segv fix, enable bsd lv2, lv2 gui enable fix, proxy/ffmpeg toggle resize...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / maskengine.C
index a9186daa09fefddd61181572d6f28a57c4e509d1..ed7cbaaebdbddfafcf72709f205eb891f5542e42 100644 (file)
@@ -2,21 +2,21 @@
 /*
  * 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 "bcsignals.h"
@@ -63,60 +63,30 @@ MaskUnit::~MaskUnit()
 
 #define OVERSAMPLE 8
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-void MaskUnit::draw_line_clamped(VFrame *frame, 
-       int &x1, 
-       int &y1, 
-       int x2, 
-       int y2,
-       unsigned char k)
+void MaskUnit::draw_line_clamped(VFrame *frame,
+       int x1, int y1, int x2, int y2, unsigned char k)
 {
-       int draw_x1;
-       int draw_y1;
-       int draw_x2;
-       int draw_y2;
+       int draw_x1, draw_y1;
+       int draw_x2, draw_y2;
 
-       if(y2 < y1)
-       {
-               draw_x1 = x2;
-               draw_y1 = y2;
-               draw_x2 = x1;
-               draw_y2 = y1;
+       if(y2 < y1) {
+               draw_x1 = x2;  draw_y1 = y2;
+               draw_x2 = x1;  draw_y2 = y1;
        }
-       else
-       {
-               draw_x1 = x1;
-               draw_y1 = y1;
-               draw_x2 = x2;
-               draw_y2 = y2;
+       else {
+               draw_x1 = x1;  draw_y1 = y1;
+               draw_x2 = x2;  draw_y2 = y2;
        }
 
        unsigned char **rows = (unsigned char**)frame->get_rows();
 
-       if(draw_y2 != draw_y1)
-       {
+       if(draw_y2 != draw_y1) {
                float slope = ((float)draw_x2 - draw_x1) / ((float)draw_y2 - draw_y1);
                int w = frame->get_w() - 1;
                int h = frame->get_h();
 
-               for(float y = draw_y1; y < draw_y2; y++)
-               {
-                       if(y >= 0 && y < h)
-                       {
+               for(float y = draw_y1; y < draw_y2; y++) {
+                       if(y >= 0 && y < h) {
                                int x = (int)((y - draw_y1) * slope + draw_x1);
                                int y_i = (int)y;
                                int x_i = CLIP(x, 0, w);
@@ -130,10 +100,10 @@ void MaskUnit::draw_line_clamped(VFrame *frame,
        }
 }
 
-void MaskUnit::blur_strip(double *val_p, 
-       double *val_m, 
-       double *dst, 
-       double *src, 
+void MaskUnit::blur_strip(double *val_p,
+       double *val_m,
+       double *dst,
+       double *src,
        int size,
        int max)
 {
@@ -175,11 +145,11 @@ void MaskUnit::blur_strip(double *val_p,
 }
 
 void MaskUnit::do_feather(VFrame *output,
-       VFrame *input, 
-       double feather, 
-       int start_y, 
-       int end_y, 
-       int start_x, 
+       VFrame *input,
+       double feather,
+       int start_y,
+       int end_y,
+       int start_x,
        int end_x)
 {
 //printf("MaskUnit::do_feather %f\n", feather);
@@ -206,18 +176,18 @@ void MaskUnit::do_feather(VFrame *output,
                                (2 * constants[6] + constants[4]) * cos(constants[2]));
 
        n_p[2] = 2 * exp(constants[0] + constants[1]) *
-                               ((constants[4] + constants[6]) * cos(constants[3]) * 
-                               cos(constants[2]) - constants[5] * 
+                               ((constants[4] + constants[6]) * cos(constants[3]) *
+                               cos(constants[2]) - constants[5] *
                                cos(constants[3]) * sin(constants[2]) -
                                constants[7] * cos(constants[2]) * sin(constants[3])) +
                                constants[6] * exp(2 * constants[0]) +
                                constants[4] * exp(2 * constants[1]);
 
        n_p[3] = exp(constants[1] + 2 * constants[0]) *
-                               (constants[7] * sin(constants[3]) - 
+                               (constants[7] * sin(constants[3]) -
                                constants[6] * cos(constants[3])) +
                                exp(constants[0] + 2 * constants[1]) *
-                               (constants[5] * sin(constants[2]) - constants[4] * 
+                               (constants[5] * sin(constants[2]) - constants[4] *
                                cos(constants[2]));
        n_p[4] = 0.0;
 
@@ -225,7 +195,7 @@ void MaskUnit::do_feather(VFrame *output,
        d_p[1] = -2 * exp(constants[1]) * cos(constants[3]) -
                                2 * exp(constants[0]) * cos(constants[2]);
 
-       d_p[2] = 4 * cos(constants[3]) * cos(constants[2]) * 
+       d_p[2] = 4 * cos(constants[3]) * cos(constants[2]) *
                                exp(constants[0] + constants[1]) +
                                exp(2 * constants[1]) + exp (2 * constants[0]);
 
@@ -361,11 +331,11 @@ void MaskUnit::do_feather(VFrame *output,
                case BC_A8:
                        DO_FEATHER(unsigned char, 0xff);
                        break;
-               
+
                case BC_A16:
                        DO_FEATHER(uint16_t, 0xffff);
                        break;
-               
+
                case BC_A_FLOAT:
                        DO_FEATHER(float, 1);
                        break;
@@ -380,11 +350,11 @@ void MaskUnit::process_package(LoadPackage *package)
 {
        MaskPackage *ptr = (MaskPackage*)package;
 
-       if(engine->recalculate && 
+       if(engine->recalculate &&
                engine->step == DO_MASK)
        {
                VFrame *mask;
-               if(engine->feather > 0) 
+               if(engine->feather > 0)
                        mask = engine->temp_mask;
                else
                        mask = engine->mask;
@@ -398,24 +368,16 @@ SET_TRACE
 //printf("MaskUnit::process_package 1\n");
 
 SET_TRACE
-               if(temp && 
+               if(temp &&
                        (temp->get_w() != oversampled_package_w ||
-                       temp->get_h() != oversampled_package_h))
-               {
-                       delete temp;
-                       temp = 0;
+                       temp->get_h() != oversampled_package_h)) {
+                       delete temp;  temp = 0;
                }
 //printf("MaskUnit::process_package 1\n");
 
 SET_TRACE
-               if(!temp)
-               {
-                       temp = new VFrame(0, 
-                               -1,
-                               oversampled_package_w, 
-                               oversampled_package_h,
-                               BC_A8,
-                               -1);
+               if(!temp) {
+                       temp = new VFrame(oversampled_package_w, oversampled_package_h, BC_A8, 0);
                }
 
 SET_TRACE
@@ -436,8 +398,8 @@ SET_TRACE
                        for(int i = 0; i < points->total; i++)
                        {
                                MaskPoint *point1 = points->values[i];
-                               MaskPoint *point2 = (i >= points->total - 1) ? 
-                                       points->values[0] : 
+                               MaskPoint *point2 = (i >= points->total - 1) ?
+                                       points->values[0] :
                                        points->values[i + 1];
 
                                float x, y;
@@ -467,11 +429,11 @@ SET_TRACE
 
                                        x = (        invtpow3 * x0
                                                + 3 * t     * invtpow2 * x1
-                                               + 3 * tpow2 * invt     * x2 
+                                               + 3 * tpow2 * invt     * x2
                                                +     tpow3            * x3);
-                                       y = (        invtpow3 * y0 
+                                       y = (        invtpow3 * y0
                                                + 3 * t     * invtpow2 * y1
-                                               + 3 * tpow2 * invt     * y2 
+                                               + 3 * tpow2 * invt     * y2
                                                +     tpow3            * y3);
 
                                        y -= ptr->start_y;
@@ -603,10 +565,10 @@ SET_TRACE
                if(engine->recalculate)
                {
 // Feather polygon
-                       if(engine->feather > 0) do_feather(engine->mask, 
-                               engine->temp_mask, 
-                               engine->feather, 
-                               ptr->start_y, 
+                       if(engine->feather > 0) do_feather(engine->mask,
+                               engine->temp_mask,
+                               engine->feather,
+                               ptr->start_y,
                                ptr->end_y,
                                0,
                                0);
@@ -619,10 +581,10 @@ SET_TRACE
                if(engine->recalculate)
                {
 // Feather polygon
-                       if(engine->feather > 0) do_feather(engine->mask, 
-                               engine->temp_mask, 
-                               engine->feather, 
-                               0, 
+                       if(engine->feather > 0) do_feather(engine->mask,
+                               engine->temp_mask,
+                               engine->feather,
+                               0,
                                0,
                                ptr->start_x,
                                ptr->end_x);
@@ -788,7 +750,7 @@ MaskEngine::MaskEngine(int cpus)
 
 MaskEngine::~MaskEngine()
 {
-       if(mask) 
+       if(mask)
        {
                delete mask;
                delete temp_mask;
@@ -802,23 +764,23 @@ MaskEngine::~MaskEngine()
        point_sets.remove_all_objects();
 }
 
-int MaskEngine::points_equivalent(ArrayList<MaskPoint*> *new_points, 
+int MaskEngine::points_equivalent(ArrayList<MaskPoint*> *new_points,
        ArrayList<MaskPoint*> *points)
 {
 //printf("MaskEngine::points_equivalent %d %d\n", new_points->total, points->total);
        if(new_points->total != points->total) return 0;
-       
+
        for(int i = 0; i < new_points->total; i++)
        {
                if(!(*new_points->values[i] == *points->values[i])) return 0;
        }
-       
+
        return 1;
 }
 
-void MaskEngine::do_mask(VFrame *output, 
+void MaskEngine::do_mask(VFrame *output,
        int64_t start_position_project,
-       MaskAutos *keyframe_set, 
+       MaskAutos *keyframe_set,
        MaskAuto *keyframe,
        MaskAuto *default_auto)
 {
@@ -850,7 +812,7 @@ void MaskEngine::do_mask(VFrame *output,
 // Determine if recalculation is needed
 SET_TRACE
 
-       if(mask && 
+       if(mask &&
                (mask->get_w() != output->get_w() ||
                mask->get_h() != output->get_h() ||
                mask->get_color_model() != new_color_model))
@@ -863,22 +825,22 @@ SET_TRACE
 
        if(!recalculate)
        {
-               if(point_sets.total != keyframe_set->total_submasks(start_position_project, 
+               if(point_sets.total != keyframe_set->total_submasks(start_position_project,
                        PLAY_FORWARD))
                        recalculate = 1;
        }
 
        if(!recalculate)
        {
-               for(int i = 0; 
-                       i < keyframe_set->total_submasks(start_position_project, 
-                               PLAY_FORWARD) && !recalculate; 
+               for(int i = 0;
+                       i < keyframe_set->total_submasks(start_position_project,
+                               PLAY_FORWARD) && !recalculate;
                        i++)
                {
                        ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
-                       keyframe_set->get_points(new_points, 
-                               i, 
-                               start_position_project, 
+                       keyframe_set->get_points(new_points,
+                               i,
+                               start_position_project,
                                PLAY_FORWARD);
                        if(!points_equivalent(new_points, point_sets.values[i])) recalculate = 1;
                        new_points->remove_all_objects();
@@ -886,9 +848,9 @@ SET_TRACE
                }
        }
 
-       int new_value = keyframe_set->get_value(start_position_project, 
+       int new_value = keyframe_set->get_value(start_position_project,
                PLAY_FORWARD);
-       float new_feather = keyframe_set->get_feather(start_position_project, 
+       float new_feather = keyframe_set->get_feather(start_position_project,
                PLAY_FORWARD);
 
        if(recalculate ||
@@ -896,20 +858,12 @@ SET_TRACE
                !EQUIV(new_value, value))
        {
                recalculate = 1;
-               if(!mask) 
+               if(!mask)
                {
-                       mask = new VFrame(0, 
-                                       -1,
-                                       output->get_w(), 
-                                       output->get_h(),
-                                       new_color_model,
-                                       -1);
-                       temp_mask = new VFrame(0, 
-                                       -1,
-                                       output->get_w(), 
-                                       output->get_h(),
-                                       new_color_model,
-                                       -1);
+                       mask = new VFrame(output->get_w(), output->get_h(),
+                                       new_color_model, 0);
+                       temp_mask = new VFrame(output->get_w(), output->get_h(),
+                                       new_color_model, 0);
                }
                if(new_feather > 0)
                        temp_mask->clear_frame();
@@ -923,15 +877,15 @@ SET_TRACE
                }
                point_sets.remove_all_objects();
 
-               for(int i = 0; 
-                       i < keyframe_set->total_submasks(start_position_project, 
-                               PLAY_FORWARD); 
+               for(int i = 0;
+                       i < keyframe_set->total_submasks(start_position_project,
+                               PLAY_FORWARD);
                        i++)
                {
                        ArrayList<MaskPoint*> *new_points = new ArrayList<MaskPoint*>;
-                       keyframe_set->get_points(new_points, 
-                               i, 
-                               start_position_project, 
+                       keyframe_set->get_points(new_points,
+                               i,
+                               start_position_project,
                                PLAY_FORWARD);
                        point_sets.append(new_points);
                }
@@ -964,19 +918,15 @@ void MaskEngine::init_packages()
 {
 SET_TRACE
 //printf("MaskEngine::init_packages 1\n");
-       int division = (int)((float)output->get_h() / (get_total_packages() / 2) + 0.5);
-       if(division < 1) division = 1;
-
+       int x0 = 0, y0 = 0, i = 0, n = get_total_packages();
+       int out_w = output->get_w(), out_h = output->get_h();
 SET_TRACE
-       for(int i = 0; i < get_total_packages(); i++)
-       {
-               MaskPackage *ptr = (MaskPackage*)get_package(i);
-
-               ptr->start_y = output->get_h() * i / get_total_packages();
-               ptr->end_y = output->get_h() * (i + 1) / get_total_packages();
-
-               ptr->start_x = output->get_w() * i / get_total_packages();
-               ptr->end_x = output->get_w() * (i + 1) / get_total_packages();
+       while( i < n ) {
+               MaskPackage *ptr = (MaskPackage*)get_package(i++);
+               int x1 = (out_w * i) / n, y1 = (out_h * i) / n;
+               ptr->start_x = x0;  ptr->end_x = x1;
+               ptr->start_y = y0;  ptr->end_y = y1;
+               x0 = x1;  y0 = y1;
        }
 SET_TRACE
 //printf("MaskEngine::init_packages 2\n");