workaround for ub16 compiler problem
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / maskautos.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "automation.inc"
23 #include "clip.h"
24 #include "edl.h"
25 #include "localsession.h"
26 #include "maskauto.h"
27 #include "maskautos.h"
28 #include "track.h"
29 #include "transportque.inc"
30
31 MaskAutos::MaskAutos(EDL *edl,
32         Track *track)
33  : Autos(edl, track)
34 {
35         type = AUTOMATION_TYPE_MASK;
36 }
37
38 MaskAutos::~MaskAutos()
39 {
40 }
41
42
43
44
45 void MaskAutos::update_parameter(MaskAuto *src)
46 {
47         double selection_start = edl->local_session->get_selectionstart(0);
48         double selection_end = edl->local_session->get_selectionend(0);
49
50 // Selection is always aligned to frame for masks
51
52 // Create new keyframe if auto keyframes or replace entire keyframe.
53         if(selection_start == selection_end)
54         {
55 // Search for keyframe to write to
56                 MaskAuto *dst = (MaskAuto*)get_auto_for_editing();
57
58                 dst->copy_data(src);
59         }
60         else
61 // Replace changed parameter in all selected keyframes.
62         {
63 // Search all keyframes in selection but don't create a new one.
64                 int64_t start = track->to_units(selection_start, 0);
65                 int64_t end = track->to_units(selection_end, 0);
66                 Auto *current_auto = 0;
67                 MaskAuto *current = 0;
68                 current = (MaskAuto*)get_prev_auto(start,
69                         PLAY_FORWARD,
70                         current_auto,
71                         1);
72
73 // The first one determines the changed parameters since it is the one displayed
74 // in the GUI.
75                 MaskAuto *first = current;
76
77 // Update the first one last, so it is available for comparisons to the changed one.
78                 for(current = (MaskAuto*)NEXT;
79                         current && current->position < end;
80                         current = (MaskAuto*)NEXT)
81                 {
82                         current->update_parameter(first,
83                                 src);
84                 }
85                 first->copy_data(src);
86         }
87 }
88
89
90
91 void MaskAutos::get_points(ArrayList<MaskPoint*> *points,
92         int submask,
93         int64_t position,
94         int direction)
95 {
96         MaskAuto *begin = 0, *end = 0;
97         position = (direction == PLAY_FORWARD) ? position : (position - 1);
98
99 // Get auto before and after position
100         for(MaskAuto* current = (MaskAuto*)last;
101                 current;
102                 current = (MaskAuto*)PREVIOUS)
103         {
104                 if(current->position <= position)
105                 {
106                         begin = current;
107                         end = NEXT ? (MaskAuto*)NEXT : current;
108                         break;
109                 }
110         }
111
112 // Nothing before position found
113         if(!begin)
114         {
115                 begin = end = (MaskAuto*)first;
116         }
117
118 // Nothing after position found
119         if(!begin)
120         {
121                 begin = end = (MaskAuto*)default_auto;
122         }
123
124
125         SubMask *mask1 = begin->get_submask(submask);
126         SubMask *mask2 = end->get_submask(submask);
127
128         points->remove_all_objects();
129         int total_points = MAX(mask1->points.size(), mask2->points.size());
130         for(int i = 0; i < total_points; i++)
131         {
132                 MaskPoint *point = new MaskPoint;
133                 MaskPoint point1;
134                 int need_point1 = 1;
135                 MaskPoint point2;
136                 int need_point2 = 1;
137
138                 if(i < mask1->points.size())
139                 {
140                         point1.copy_from(*mask1->points.get(i));
141                         need_point1 = 0;
142                 }
143
144                 if(i < mask2->points.size())
145                 {
146                         point2.copy_from(*mask2->points.get(i));
147                         need_point2 = 0;
148                 }
149
150                 if(need_point1) point1.copy_from(point2);
151                 if(need_point2) point2.copy_from(point1);
152
153                 avg_points(point,
154                         &point1,
155                         &point2,
156                         position,
157                         begin->position,
158                         end->position);
159                 points->append(point);
160         }
161 }
162
163
164 float MaskAutos::get_feather(int64_t position, int direction)
165 {
166         Auto *begin = 0, *end = 0;
167         position = (direction == PLAY_FORWARD) ? position : (position - 1);
168
169         get_prev_auto(position, PLAY_FORWARD, begin, 1);
170         get_next_auto(position, PLAY_FORWARD, end, 1);
171
172         double weight = 0.0;
173         if(end->position != begin->position)
174                 weight = (double)(position - begin->position) / (end->position - begin->position);
175
176         return ((MaskAuto*)begin)->feather * (1.0 - weight) + ((MaskAuto*)end)->feather * weight;
177 }
178
179 int MaskAutos::get_value(int64_t position, int direction)
180 {
181         Auto *begin = 0, *end = 0;
182         position = (direction == PLAY_FORWARD) ? position : (position - 1);
183
184         get_prev_auto(position, PLAY_FORWARD, begin, 1);
185         get_next_auto(position, PLAY_FORWARD, end, 1);
186
187         double weight = 0.0;
188         if(end->position != begin->position)
189                 weight = (double)(position - begin->position) / (end->position - begin->position);
190
191         int result = (int)((double)((MaskAuto*)begin)->value * (1.0 - weight) +
192                 (double)((MaskAuto*)end)->value * weight + 0.5);
193 // printf("MaskAutos::get_value %d %d %f %d %f %d\n",
194 // __LINE__,
195 // ((MaskAuto*)begin)->value,
196 // 1.0 - weight,
197 // ((MaskAuto*)end)->value,
198 // weight,
199 // result);
200         return result;
201 }
202
203
204 void MaskAutos::avg_points(MaskPoint *output,
205                 MaskPoint *input1,
206                 MaskPoint *input2,
207                 int64_t output_position,
208                 int64_t position1,
209                 int64_t position2)
210 {
211         if(position2 == position1)
212         {
213                 *output = *input1;
214         }
215         else
216         {
217                 float fraction2 = (float)(output_position - position1) / (position2 - position1);
218                 float fraction1 = 1 - fraction2;
219                 output->x = input1->x * fraction1 + input2->x * fraction2;
220                 output->y = input1->y * fraction1 + input2->y * fraction2;
221                 output->control_x1 = input1->control_x1 * fraction1 + input2->control_x1 * fraction2;
222                 output->control_y1 = input1->control_y1 * fraction1 + input2->control_y1 * fraction2;
223                 output->control_x2 = input1->control_x2 * fraction1 + input2->control_x2 * fraction2;
224                 output->control_y2 = input1->control_y2 * fraction1 + input2->control_y2 * fraction2;
225         }
226
227 }
228
229
230 Auto* MaskAutos::new_auto()
231 {
232         return new MaskAuto(edl, this);
233 }
234
235 void MaskAutos::dump()
236 {
237         printf("        MaskAutos::dump %p\n", this);
238         printf("        Default: position %jd submasks %d\n",
239                 default_auto->position,
240                 ((MaskAuto*)default_auto)->masks.total);
241         ((MaskAuto*)default_auto)->dump();
242         for(Auto* current = first; current; current = NEXT)
243         {
244                 printf("        position %jd masks %d\n",
245                         current->position,
246                         ((MaskAuto*)current)->masks.total);
247                 ((MaskAuto*)current)->dump();
248         }
249 }
250
251 int MaskAutos::mask_exists(int64_t position, int direction)
252 {
253         Auto *current = 0;
254         position = (direction == PLAY_FORWARD) ? position : (position - 1);
255
256         MaskAuto* keyframe = (MaskAuto*)get_prev_auto(position, direction, current);
257
258
259
260         for(int i = 0; i < keyframe->masks.total; i++)
261         {
262                 SubMask *mask = keyframe->get_submask(i);
263                 if(mask->points.total > 1)
264                         return 1;
265         }
266         return 0;
267 }
268
269 int MaskAutos::total_submasks(int64_t position, int direction)
270 {
271         position = (direction == PLAY_FORWARD) ? position : (position - 1);
272         for(MaskAuto* current = (MaskAuto*)last;
273                 current;
274                 current = (MaskAuto*)PREVIOUS)
275         {
276                 if(current->position <= position)
277                 {
278                         return current->masks.total;
279                 }
280         }
281
282         return ((MaskAuto*)default_auto)->masks.total;
283 }
284
285
286 void MaskAutos::translate_masks(float translate_x, float translate_y)
287 {
288         ((MaskAuto *)default_auto)->translate_submasks(translate_x, translate_y);
289         for(MaskAuto* current = (MaskAuto*)first;
290                 current;
291                 current = (MaskAuto*)NEXT)
292         {
293                 current->translate_submasks(translate_x, translate_y);
294         }
295 }
296
297 void MaskAutos::set_proxy(int orig_scale, int new_scale)
298 {
299         ((MaskAuto *)default_auto)->scale_submasks(orig_scale, new_scale);
300         for( MaskAuto* current=(MaskAuto*)first; current; current=(MaskAuto*)NEXT ) {
301                 current->scale_submasks(orig_scale, new_scale);
302         }
303 }
304