4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "automation.inc"
25 #include "localsession.h"
27 #include "maskautos.h"
29 #include "transportque.inc"
31 MaskAutos::MaskAutos(EDL *edl,
35 type = AUTOMATION_TYPE_MASK;
38 MaskAutos::~MaskAutos()
45 void MaskAutos::update_parameter(MaskAuto *src)
47 double selection_start = edl->local_session->get_selectionstart(0);
48 double selection_end = edl->local_session->get_selectionend(0);
50 // Selection is always aligned to frame for masks
52 // Create new keyframe if auto keyframes or replace entire keyframe.
53 if(selection_start == selection_end)
55 // Search for keyframe to write to
56 MaskAuto *dst = (MaskAuto*)get_auto_for_editing();
61 // Replace changed parameter in all selected keyframes.
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,
73 // The first one determines the changed parameters since it is the one displayed
75 MaskAuto *first = current;
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)
82 current->update_parameter(first,
85 first->copy_data(src);
91 void MaskAutos::get_points(ArrayList<MaskPoint*> *points,
96 MaskAuto *begin = 0, *end = 0;
97 position = (direction == PLAY_FORWARD) ? position : (position - 1);
99 // Get auto before and after position
100 for(MaskAuto* current = (MaskAuto*)last;
102 current = (MaskAuto*)PREVIOUS)
104 if(current->position <= position)
107 end = NEXT ? (MaskAuto*)NEXT : current;
112 // Nothing before position found
115 begin = end = (MaskAuto*)first;
118 // Nothing after position found
121 begin = end = (MaskAuto*)default_auto;
125 SubMask *mask1 = begin->get_submask(submask);
126 SubMask *mask2 = end->get_submask(submask);
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++)
132 MaskPoint *point = new MaskPoint;
138 if(i < mask1->points.size())
140 point1.copy_from(*mask1->points.get(i));
144 if(i < mask2->points.size())
146 point2.copy_from(*mask2->points.get(i));
150 if(need_point1) point1.copy_from(point2);
151 if(need_point2) point2.copy_from(point1);
159 points->append(point);
164 float MaskAutos::get_feather(int64_t position, int direction)
166 Auto *begin = 0, *end = 0;
167 position = (direction == PLAY_FORWARD) ? position : (position - 1);
169 get_prev_auto(position, PLAY_FORWARD, begin, 1);
170 get_next_auto(position, PLAY_FORWARD, end, 1);
173 if(end->position != begin->position)
174 weight = (double)(position - begin->position) / (end->position - begin->position);
176 return ((MaskAuto*)begin)->feather * (1.0 - weight) + ((MaskAuto*)end)->feather * weight;
179 int MaskAutos::get_value(int64_t position, int direction)
181 Auto *begin = 0, *end = 0;
182 position = (direction == PLAY_FORWARD) ? position : (position - 1);
184 get_prev_auto(position, PLAY_FORWARD, begin, 1);
185 get_next_auto(position, PLAY_FORWARD, end, 1);
188 if(end->position != begin->position)
189 weight = (double)(position - begin->position) / (end->position - begin->position);
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",
195 // ((MaskAuto*)begin)->value,
197 // ((MaskAuto*)end)->value,
204 void MaskAutos::avg_points(MaskPoint *output,
207 int64_t output_position,
211 if(position2 == position1)
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;
230 Auto* MaskAutos::new_auto()
232 return new MaskAuto(edl, this);
235 void MaskAutos::dump()
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)
244 printf(" position %jd masks %d\n",
246 ((MaskAuto*)current)->masks.total);
247 ((MaskAuto*)current)->dump();
251 int MaskAutos::mask_exists(int64_t position, int direction)
254 position = (direction == PLAY_FORWARD) ? position : (position - 1);
256 MaskAuto* keyframe = (MaskAuto*)get_prev_auto(position, direction, current);
260 for(int i = 0; i < keyframe->masks.total; i++)
262 SubMask *mask = keyframe->get_submask(i);
263 if(mask->points.total > 1)
269 int MaskAutos::total_submasks(int64_t position, int direction)
271 position = (direction == PLAY_FORWARD) ? position : (position - 1);
272 for(MaskAuto* current = (MaskAuto*)last;
274 current = (MaskAuto*)PREVIOUS)
276 if(current->position <= position)
278 return current->masks.total;
282 return ((MaskAuto*)default_auto)->masks.total;
286 void MaskAutos::translate_masks(float translate_x, float translate_y)
288 ((MaskAuto *)default_auto)->translate_submasks(translate_x, translate_y);
289 for(MaskAuto* current = (MaskAuto*)first;
291 current = (MaskAuto*)NEXT)
293 current->translate_submasks(translate_x, translate_y);
297 void MaskAutos::set_proxy(int orig_scale, int new_scale)
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);