4 * Copyright (C) 2016 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
23 #include "bcsignals.h"
25 #include "motionscan.h"
26 #include "rotatescan.h"
39 RotateScanPackage::RotateScanPackage()
44 RotateScanUnit::RotateScanUnit(RotateScan *server, MotionMain *plugin)
47 this->server = server;
48 this->plugin = plugin;
53 RotateScanUnit::~RotateScanUnit()
59 void RotateScanUnit::process_package(LoadPackage *package)
61 if(server->skip) return;
62 RotateScanPackage *pkg = (RotateScanPackage*)package;
64 if((pkg->difference = server->get_cache(pkg->angle)) < 0)
66 //printf("RotateScanUnit::process_package %d\n", __LINE__);
67 int color_model = server->previous_frame->get_color_model();
68 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
69 int row_bytes = server->previous_frame->get_bytes_per_line();
72 rotater = new AffineEngine(1, 1);
73 if(!temp) temp = new VFrame(0,
75 server->previous_frame->get_w(),
76 server->previous_frame->get_h(),
79 //printf("RotateScanUnit::process_package %d\n", __LINE__);
82 // Rotate original block size
83 // rotater->set_viewport(server->block_x1,
85 // server->block_x2 - server->block_x1,
86 // server->block_y2 - server->block_y1);
87 rotater->set_in_viewport(server->block_x1,
89 server->block_x2 - server->block_x1,
90 server->block_y2 - server->block_y1);
91 rotater->set_out_viewport(server->block_x1,
93 server->block_x2 - server->block_x1,
94 server->block_y2 - server->block_y1);
95 // rotater->set_pivot(server->block_x, server->block_y);
96 rotater->set_in_pivot(server->block_x, server->block_y);
97 rotater->set_out_pivot(server->block_x, server->block_y);
98 //printf("RotateScanUnit::process_package %d\n", __LINE__);
100 server->previous_frame,
103 // Scan reduced block size
104 //plugin->output_frame->copy_from(server->current_frame);
105 //plugin->output_frame->copy_from(temp);
106 // printf("RotateScanUnit::process_package %d %d %d %d %d\n",
113 int x1 = server->scan_x;
114 int y1 = server->scan_y;
115 int x2 = x1 + server->scan_w;
116 int y2 = y1 + server->scan_h;
117 x2 = MIN(temp->get_w(), x2);
118 y2 = MIN(temp->get_h(), y2);
119 x2 = MIN(server->current_frame->get_w(), x2);
120 y2 = MIN(server->current_frame->get_h(), y2);
124 if(x2 > x1 && y2 > y1)
126 pkg->difference = MotionScan::abs_diff(
127 temp->get_rows()[y1] + x1 * pixel_size,
128 server->current_frame->get_rows()[y1] + x1 * pixel_size,
133 //printf("RotateScanUnit::process_package %d\n", __LINE__);
134 server->put_cache(pkg->angle, pkg->difference);
137 // printf("RotateScanUnit::process_package 10 x=%d y=%d w=%d h=%d block_x=%d block_y=%d angle=%f scan_w=%d scan_h=%d diff=%lld\n",
140 // server->block_x2 - server->block_x1,
141 // server->block_y2 - server->block_y1,
172 RotateScan::RotateScan(MotionMain *plugin,
177 total_clients, total_packages
180 this->plugin = plugin;
181 cache_lock = new Mutex("RotateScan::cache_lock");
185 RotateScan::~RotateScan()
190 void RotateScan::init_packages()
192 for(int i = 0; i < get_total_packages(); i++)
194 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
196 (scan_angle2 - scan_angle1) /
202 LoadClient* RotateScan::new_client()
204 return new RotateScanUnit(this, plugin);
207 LoadPackage* RotateScan::new_package()
209 return new RotateScanPackage;
213 float RotateScan::scan_frame(VFrame *previous_frame,
214 VFrame *current_frame,
219 this->block_x = block_x;
220 this->block_y = block_y;
222 //printf("RotateScan::scan_frame %d\n", __LINE__);
223 switch(plugin->config.tracking_type)
225 case MotionScan::NO_CALCULATE:
226 result = plugin->config.rotation_center;
230 case MotionScan::LOAD:
232 char string[BCTEXTLEN];
236 plugin->get_source_position());
237 FILE *input = fopen(string, "r");
240 int temp = fscanf(input, "%f", &result);
246 perror("RotateScan::scan_frame LOAD");
259 this->previous_frame = previous_frame;
260 this->current_frame = current_frame;
261 int w = current_frame->get_w();
262 int h = current_frame->get_h();
263 int block_w = w * plugin->config.global_block_w / 100;
264 int block_h = h * plugin->config.global_block_h / 100;
266 if(this->block_x - block_w / 2 < 0) block_w = this->block_x * 2;
267 if(this->block_y - block_h / 2 < 0) block_h = this->block_y * 2;
268 if(this->block_x + block_w / 2 > w) block_w = (w - this->block_x) * 2;
269 if(this->block_y + block_h / 2 > h) block_h = (h - this->block_y) * 2;
271 block_x1 = this->block_x - block_w / 2;
272 block_x2 = this->block_x + block_w / 2;
273 block_y1 = this->block_y - block_h / 2;
274 block_y2 = this->block_y + block_h / 2;
277 // Calculate the maximum area available to scan after rotation.
278 // Must be calculated from the starting range because of cache.
279 // Get coords of rectangle after rotation.
280 double center_x = this->block_x;
281 double center_y = this->block_y;
282 double max_angle = plugin->config.rotation_range;
283 double base_angle1 = atan((float)block_h / block_w);
284 double base_angle2 = atan((float)block_w / block_h);
285 double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
286 double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
287 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
288 double x1 = center_x - cos(target_angle1) * radius;
289 double y1 = center_y - sin(target_angle1) * radius;
290 double x2 = center_x + sin(target_angle2) * radius;
291 double y2 = center_y - cos(target_angle2) * radius;
292 double x3 = center_x - sin(target_angle2) * radius;
293 double y3 = center_y + cos(target_angle2) * radius;
295 // Track top edge to find greatest area.
296 double max_area1 = 0;
299 for(double x = x1; x < x2; x++)
301 double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
302 if(x >= center_x && x < block_x2 && y >= block_y1 && y < center_y)
304 double area = fabs(x - center_x) * fabs(y - center_y);
314 // Track left edge to find greatest area.
315 double max_area2 = 0;
318 for(double y = y1; y < y3; y++)
320 double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
321 if(x >= block_x1 && x < center_x && y >= block_y1 && y < center_y)
323 double area = fabs(x - center_x) * fabs(y - center_y);
337 // Get reduced scan coords
338 scan_w = (int)(fabs(max_x - center_x) * 2);
339 scan_h = (int)(fabs(max_y - center_y) * 2);
340 scan_x = (int)(center_x - scan_w / 2);
341 scan_y = (int)(center_y - scan_h / 2);
342 // printf("RotateScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
343 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
344 // printf(" angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
346 // Determine min angle from size of block
347 double angle1 = atan((double)block_h / block_w);
348 double angle2 = atan((double)(block_h - 1) / (block_w + 1));
349 double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
350 min_angle = MAX(min_angle, MIN_ANGLE);
352 //printf("RotateScan::scan_frame %d min_angle=%f\n", __LINE__, min_angle * 360 / 2 / M_PI);
354 cache.remove_all_objects();
359 if(previous_frame->data_matches(current_frame))
361 //printf("RotateScan::scan_frame: frames match. Skipping.\n");
362 result = plugin->config.rotation_center;
369 // Initial search range
370 float angle_range = max_angle;
371 result = plugin->config.rotation_center;
372 total_steps = plugin->config.rotate_positions;
375 while(angle_range >= min_angle * total_steps)
377 scan_angle1 = result - angle_range;
378 scan_angle2 = result + angle_range;
381 set_package_count(total_steps);
382 //set_package_count(1);
385 int64_t min_difference = -1;
386 for(int i = 0; i < get_total_packages(); i++)
388 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
389 if(pkg->difference < min_difference || min_difference == -1)
391 min_difference = pkg->difference;
403 //printf("RotateScan::scan_frame %d\n", __LINE__);
405 if(!skip && plugin->config.tracking_type == MotionScan::SAVE)
407 char string[BCTEXTLEN];
411 plugin->get_source_position());
412 FILE *output = fopen(string, "w");
415 fprintf(output, "%f\n", result);
420 perror("RotateScan::scan_frame SAVE");
424 //printf("RotateScan::scan_frame %d angle=%f\n", __LINE__, result);
431 int64_t RotateScan::get_cache(float angle)
434 cache_lock->lock("RotateScan::get_cache");
435 for(int i = 0; i < cache.total; i++)
437 RotateScanCache *ptr = cache.values[i];
438 if(fabs(ptr->angle - angle) <= MIN_ANGLE)
440 result = ptr->difference;
444 cache_lock->unlock();
448 void RotateScan::put_cache(float angle, int64_t difference)
450 RotateScanCache *ptr = new RotateScanCache(angle, difference);
451 cache_lock->lock("RotateScan::put_cache");
453 cache_lock->unlock();
464 RotateScanCache::RotateScanCache(float angle, int64_t difference)
467 this->difference = difference;