1c11878c84655f1c4a449c63b5fbb480900100f7
[goodguy/history.git] / cinelerra-5.1 / plugins / findobj / findobjwindow.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2012 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 "bcdisplayinfo.h"
23 #include "clip.h"
24 #include "dragcheckbox.h"
25 #include "language.h"
26 #include "findobj.h"
27 #include "findobjwindow.h"
28 #include "plugin.h"
29 #include "pluginserver.h"
30 #include "theme.h"
31 #include "track.h"
32
33
34 FindObjWindow::FindObjWindow(FindObjMain *plugin)
35  : PluginClientWindow(plugin, 340, 660, 340, 660, 0)
36 {
37         this->plugin = plugin;
38 }
39
40 FindObjWindow::~FindObjWindow()
41 {
42 }
43
44 void FindObjWindow::create_objects()
45 {
46         int x = 10, y = 10, x1 = x, x2 = get_w()/2;
47         plugin->load_configuration();
48
49         BC_Title *title;
50         add_subwindow(title = new BC_Title(x1, y, _("Algorithm:")));
51         add_subwindow(algorithm = new FindObjAlgorithm(plugin, this,
52                 x1 + title->get_w() + 10, y));
53         algorithm->create_objects();
54         y += algorithm->get_h() + plugin->get_theme()->widget_border;
55
56         add_subwindow(use_flann = new FindObjUseFlann(plugin, this, x, y));
57         y += use_flann->get_h() + plugin->get_theme()->widget_border + 20;
58
59         int x0 = x + 200;
60         add_subwindow(title = new BC_Title(x, y, _("Output/scene layer:")));
61         scene_layer = new FindObjLayer(plugin, this, x0, y,
62                 &plugin->config.scene_layer);
63         scene_layer->create_objects();
64         y += scene_layer->get_h() + plugin->get_theme()->widget_border;
65
66         add_subwindow(title = new BC_Title(x, y, _("Object layer:")));
67         object_layer = new FindObjLayer(plugin, this, x0, y,
68                 &plugin->config.object_layer);
69         object_layer->create_objects();
70         y += object_layer->get_h() + plugin->get_theme()->widget_border;
71
72         add_subwindow(title = new BC_Title(x, y, _("Replacement object layer:")));
73         replace_layer = new FindObjLayer(plugin, this, x0, y,
74                 &plugin->config.replace_layer);
75         replace_layer->create_objects();
76         y += replace_layer->get_h() + plugin->get_theme()->widget_border + 10;
77
78         add_subwindow(title = new BC_Title(x+15, y, _("Units: 0 to 100 percent")));
79         y += title->get_h();
80
81         int y1 = y;
82         add_subwindow(title = new BC_Title(x1, y + 10, _("Scene X:")));
83         Track *track = plugin->server->plugin->track;
84         int trk_w = track->track_w, trk_h = track->track_h;
85         float drag_w = trk_w * plugin->config.scene_w / 100.;
86         float drag_h = trk_h * plugin->config.scene_h / 100.;
87         float ctr_x  = trk_w * plugin->config.scene_x / 100.;
88         float ctr_y  = trk_h * plugin->config.scene_y / 100.;
89         float drag_x = ctr_x - drag_w/2, drag_y = ctr_y - drag_h/2;
90         drag_scene = new FindObjDragScene(plugin, this, x1+title->get_w()+10, y+5,
91                 drag_x, drag_y, drag_w, drag_h);
92         add_subwindow(drag_scene);
93         drag_scene->create_objects();
94         y += title->get_h() + 15;
95
96         add_subwindow(scene_x = new FindObjScanFloat(plugin, this,
97                 x1, y, &plugin->config.scene_x));
98         add_subwindow(scene_x_text = new FindObjScanFloatText(plugin, this,
99                 x1 + scene_x->get_w() + 10, y + 10, &plugin->config.scene_x));
100         scene_x->center_text = scene_x_text;
101         scene_x_text->center = scene_x;
102
103         y += 40;
104         add_subwindow(title = new BC_Title(x1, y + 10, _("Scene Y:")));
105         y += title->get_h() + 15;
106         add_subwindow(scene_y = new FindObjScanFloat(plugin, this,
107                 x1, y, &plugin->config.scene_y));
108         add_subwindow(scene_y_text = new FindObjScanFloatText(plugin, this,
109                 x1 + scene_y->get_w() + 10, y + 10, &plugin->config.scene_y));
110         scene_y->center_text = scene_y_text;
111         scene_y_text->center = scene_y;
112
113         y += 40;
114         add_subwindow(new BC_Title(x1, y + 10, _("Scene W:")));
115         y += title->get_h() + 15;
116         add_subwindow(scene_w = new FindObjScanFloat(plugin, this,
117                 x1, y, &plugin->config.scene_w));
118         add_subwindow(scene_w_text = new FindObjScanFloatText(plugin, this,
119                 x1 + scene_w->get_w() + 10, y + 10, &plugin->config.scene_w));
120         scene_w->center_text = scene_w_text;
121         scene_w_text->center = scene_w;
122
123         y += 40;
124         add_subwindow(title = new BC_Title(x1, y + 10, _("Scene H:")));
125         y += title->get_h() + 15;
126         add_subwindow(scene_h = new FindObjScanFloat(plugin, this,
127                 x1, y, &plugin->config.scene_h));
128         add_subwindow(scene_h_text = new FindObjScanFloatText(plugin, this,
129                 x1 + scene_h->get_w() + 10, y + 10,
130                 &plugin->config.scene_h));
131         scene_h->center_text = scene_h_text;
132         scene_h_text->center = scene_h;
133
134         y = y1;
135         add_subwindow(title = new BC_Title(x2, y + 10, _("Object X:")));
136         drag_w = trk_w * plugin->config.object_w / 100.;
137         drag_h = trk_h * plugin->config.object_h / 100.;
138         ctr_x  = trk_w * plugin->config.object_x / 100.,
139         ctr_y  = trk_h * plugin->config.object_y / 100.;
140         drag_x = ctr_x - drag_w/2;  drag_y = ctr_y - drag_h/2;
141         drag_object = new FindObjDragObject(plugin, this, x2+title->get_w()+10, y+5,
142                 drag_x, drag_y, drag_w, drag_h);
143         add_subwindow(drag_object);
144         drag_object->create_objects();
145         y += title->get_h() + 15;
146
147         add_subwindow(object_x = new FindObjScanFloat(plugin, this,
148                 x2, y, &plugin->config.object_x));
149         add_subwindow(object_x_text = new FindObjScanFloatText(plugin, this,
150                 x2 + object_x->get_w() + 10, y + 10, &plugin->config.object_x));
151         object_x->center_text = object_x_text;
152         object_x_text->center = object_x;
153
154         y += 40;
155         add_subwindow(title = new BC_Title(x2, y + 10, _("Object Y:")));
156         y += title->get_h() + 15;
157         add_subwindow(object_y = new FindObjScanFloat(plugin, this,
158                 x2, y, &plugin->config.object_y));
159         add_subwindow(object_y_text = new FindObjScanFloatText(plugin, this,
160                 x2 + object_y->get_w() + 10, y + 10, &plugin->config.object_y));
161         object_y->center_text = object_y_text;
162         object_y_text->center = object_y;
163
164         y += 40;
165         add_subwindow(new BC_Title(x2, y + 10, _("Object W:")));
166         y += title->get_h() + 15;
167         add_subwindow(object_w = new FindObjScanFloat(plugin, this,
168                 x2, y, &plugin->config.object_w));
169         add_subwindow(object_w_text = new FindObjScanFloatText(plugin, this,
170                 x2 + object_w->get_w() + 10, y + 10, &plugin->config.object_w));
171         object_w->center_text = object_w_text;
172         object_w_text->center = object_w;
173
174         y += 40;
175         add_subwindow(title = new BC_Title(x2, y + 10, _("Object H:")));
176         y += title->get_h() + 15;
177         add_subwindow(object_h = new FindObjScanFloat(plugin, this,
178                 x2, y, &plugin->config.object_h));
179         add_subwindow(object_h_text = new FindObjScanFloatText(plugin, this,
180                 x2 + object_h->get_w() + 10, y + 10,
181                 &plugin->config.object_h));
182         object_h->center_text = object_h_text;
183         object_h_text->center = object_h;
184
185         y += 40 + 15;
186         add_subwindow(draw_keypoints = new FindObjDrawKeypoints(plugin, this, x, y));
187         y += draw_keypoints->get_h() + plugin->get_theme()->widget_border;
188         add_subwindow(replace_object = new FindObjReplace(plugin, this, x, y));
189         y += replace_object->get_h() + plugin->get_theme()->widget_border;
190         add_subwindow(draw_scene_border = new FindObjDrawSceneBorder(plugin, this, x, y));
191         y += draw_scene_border->get_h() + plugin->get_theme()->widget_border;
192         add_subwindow(draw_object_border = new FindObjDrawObjectBorder(plugin, this, x, y));
193         y += draw_object_border->get_h() + plugin->get_theme()->widget_border;
194
195         add_subwindow(title = new BC_Title(x, y + 10, _("Object blend amount:")));
196         add_subwindow(blend = new FindObjBlend(plugin,
197                 x + title->get_w() + plugin->get_theme()->widget_border, y,
198                 &plugin->config.blend));
199         y += blend->get_h();
200
201         show_window(1);
202 }
203
204 void FindObjWindow::update_drag()
205 {
206         Track *track = drag_scene->get_drag_track();
207         int trk_w = track->track_w, trk_h = track->track_h;
208         drag_scene->drag_w = trk_w * plugin->config.scene_w/100.;
209         drag_scene->drag_h = trk_h * plugin->config.scene_h/100.;
210         drag_scene->drag_x = trk_w * plugin->config.scene_x/100. - drag_scene->drag_w/2;
211         drag_scene->drag_y = trk_h * plugin->config.scene_y/100. - drag_scene->drag_h/2;
212         track = drag_object->get_drag_track();
213         trk_w = track->track_w, trk_h = track->track_h;
214         drag_object->drag_w = trk_w * plugin->config.object_w/100.;
215         drag_object->drag_h = trk_h * plugin->config.object_h/100.;
216         drag_object->drag_x = trk_w * plugin->config.object_x/100. - drag_object->drag_w/2;
217         drag_object->drag_y = trk_h * plugin->config.object_y/100. - drag_object->drag_h/2;
218         plugin->send_configure_change();
219 }
220
221 FindObjScanFloat::FindObjScanFloat(FindObjMain *plugin, FindObjWindow *gui,
222         int x, int y, float *value)
223  : BC_FPot(x, y, *value, (float)0, (float)100)
224 {
225         this->plugin = plugin;
226         this->gui = gui;
227         this->value = value;
228         this->center_text = 0;
229         set_precision(0.1);
230 }
231
232 int FindObjScanFloat::handle_event()
233 {
234         *value = get_value();
235         center_text->update(*value);
236         gui->update_drag();
237         return 1;
238 }
239
240 void FindObjScanFloat::update(float v)
241 {
242         BC_FPot::update(*value = v);
243         center_text->update(v);
244         plugin->send_configure_change();
245 }
246
247
248 FindObjScanFloatText::FindObjScanFloatText(FindObjMain *plugin, FindObjWindow *gui,
249         int x, int y, float *value)
250  : BC_TextBox(x, y, 75, 1, *value)
251 {
252         this->plugin = plugin;
253         this->gui = gui;
254         this->value = value;
255         this->center = 0;
256         set_precision(1);
257 }
258
259 int FindObjScanFloatText::handle_event()
260 {
261         *value = atof(get_text());
262         center->update(*value);
263         gui->update_drag();
264         return 1;
265 }
266
267
268 FindObjDrawSceneBorder::FindObjDrawSceneBorder(FindObjMain *plugin, FindObjWindow *gui,
269         int x, int y)
270  : BC_CheckBox(x, y, plugin->config.draw_scene_border, _("Draw scene border"))
271 {
272         this->gui = gui;
273         this->plugin = plugin;
274 }
275
276 int FindObjDrawSceneBorder::handle_event()
277 {
278         plugin->config.draw_scene_border = get_value();
279         plugin->send_configure_change();
280         return 1;
281 }
282
283 FindObjDrawObjectBorder::FindObjDrawObjectBorder(FindObjMain *plugin, FindObjWindow *gui,
284         int x, int y)
285  : BC_CheckBox(x, y, plugin->config.draw_object_border, _("Draw object border"))
286 {
287         this->gui = gui;
288         this->plugin = plugin;
289 }
290
291
292 FindObjDrawKeypoints::FindObjDrawKeypoints(FindObjMain *plugin, FindObjWindow *gui,
293         int x, int y)
294  : BC_CheckBox(x, y, plugin->config.draw_keypoints, _("Draw keypoints"))
295 {
296         this->gui = gui;
297         this->plugin = plugin;
298 }
299
300 int FindObjDrawKeypoints::handle_event()
301 {
302         plugin->config.draw_keypoints = get_value();
303         plugin->send_configure_change();
304         return 1;
305 }
306
307
308 FindObjReplace::FindObjReplace(FindObjMain *plugin, FindObjWindow *gui,
309         int x, int y)
310  : BC_CheckBox(x, y, plugin->config.replace_object, _("Replace object"))
311 {
312         this->gui = gui;
313         this->plugin = plugin;
314 }
315
316 int FindObjReplace::handle_event()
317 {
318         plugin->config.replace_object = get_value();
319         plugin->send_configure_change();
320         return 1;
321 }
322
323
324 int FindObjDrawObjectBorder::handle_event()
325 {
326         plugin->config.draw_object_border = get_value();
327         plugin->send_configure_change();
328         return 1;
329 }
330
331
332 FindObjDragScene::FindObjDragScene(FindObjMain *plugin, FindObjWindow *gui, int x, int y,
333                 float drag_x, float drag_y, float drag_w, float drag_h)
334  : DragCheckBox(plugin->server->mwindow, x, y, _("Drag"), &plugin->config.drag_scene,
335                 drag_x, drag_y, drag_w, drag_h)
336 {
337         this->plugin = plugin;
338         this->gui = gui;
339 }
340
341 FindObjDragScene::~FindObjDragScene()
342 {
343 }
344
345 int FindObjDragScene::handle_event()
346 {
347         int ret = DragCheckBox::handle_event();
348         plugin->send_configure_change();
349         return ret;
350 }
351 Track *FindObjDragScene::get_drag_track()
352 {
353         return plugin->server->plugin->track;
354 }
355 int64_t FindObjDragScene::get_drag_position()
356 {
357         return plugin->get_source_position();
358 }
359 void FindObjDragScene::update_gui()
360 {
361         Track *track = get_drag_track();
362         int trk_w = track->track_w, trk_h = track->track_h;
363         float ctr_x = drag_x + drag_w/2, ctr_y = drag_y + drag_h/2;
364 printf("scene=%f,%f wxh=%fx%f  ctr=%f,%f\n",drag_x,drag_y,drag_w,drag_h, ctr_x,ctr_y);
365         gui->scene_x->update( plugin->config.scene_x = 100. * ctr_x / trk_w );
366         gui->scene_y->update( plugin->config.scene_y = 100. * ctr_y / trk_h );
367         gui->scene_w->update( plugin->config.scene_w = 100. * drag_w / trk_w );
368         gui->scene_h->update( plugin->config.scene_h = 100. * drag_h / trk_h );
369         plugin->send_configure_change();
370 }
371
372 FindObjDragObject::FindObjDragObject(FindObjMain *plugin, FindObjWindow *gui, int x, int y,
373                 float drag_x, float drag_y, float drag_w, float drag_h)
374  : DragCheckBox(plugin->server->mwindow, x, y, _("Drag"), &plugin->config.drag_object,
375                 drag_x, drag_y, drag_w, drag_h)
376 {
377         this->plugin = plugin;
378         this->gui = gui;
379 }
380
381 FindObjDragObject::~FindObjDragObject()
382 {
383 }
384
385 int FindObjDragObject::handle_event()
386 {
387         int ret = DragCheckBox::handle_event();
388         plugin->send_configure_change();
389         return ret;
390 }
391 Track *FindObjDragObject::get_drag_track()
392 {
393         return plugin->server->plugin->track;
394 }
395 int64_t FindObjDragObject::get_drag_position()
396 {
397         return plugin->get_source_position();
398 }
399 void FindObjDragObject::update_gui()
400 {
401         Track *track = get_drag_track();
402         int trk_w = track->track_w, trk_h = track->track_h;
403         float ctr_x = drag_x + drag_w/2, ctr_y = drag_y + drag_h/2;
404 printf("object %f,%f  %fx%f   ctr %f,%f\n", drag_x,drag_y,drag_w,drag_h,ctr_x,ctr_y);
405         gui->object_x->update( plugin->config.object_x = 100. * ctr_x / trk_w );
406         gui->object_y->update( plugin->config.object_y = 100. * ctr_y / trk_h );
407         gui->object_w->update( plugin->config.object_w = 100. * drag_w / trk_w );
408         gui->object_h->update( plugin->config.object_h = 100. * drag_h / trk_h );
409         plugin->send_configure_change();
410 }
411
412
413
414 FindObjAlgorithm::FindObjAlgorithm(FindObjMain *plugin, FindObjWindow *gui, int x, int y)
415  : BC_PopupMenu(x, y, calculate_w(gui), to_text(plugin->config.algorithm))
416 {
417         this->plugin = plugin;
418         this->gui = gui;
419 }
420
421 int FindObjAlgorithm::handle_event()
422 {
423         plugin->config.algorithm = from_text(get_text());
424         plugin->send_configure_change();
425         return 1;
426 }
427
428 void FindObjAlgorithm::create_objects()
429 {
430         add_item(new BC_MenuItem(to_text(NO_ALGORITHM)));
431 #ifdef _SIFT
432         add_item(new BC_MenuItem(to_text(ALGORITHM_SIFT)));
433 #endif
434 #ifdef _SURF
435         add_item(new BC_MenuItem(to_text(ALGORITHM_SURF)));
436 #endif
437 #ifdef _ORB
438         add_item(new BC_MenuItem(to_text(ALGORITHM_ORB)));
439 #endif
440 #ifdef _AKAZE
441         add_item(new BC_MenuItem(to_text(ALGORITHM_AKAZE)));
442 #endif
443 #ifdef _BRISK
444         add_item(new BC_MenuItem(to_text(ALGORITHM_BRISK)));
445 #endif
446 }
447
448 int FindObjAlgorithm::from_text(char *text)
449 {
450 #ifdef _SIFT
451         if(!strcmp(text, _("SIFT"))) return ALGORITHM_SIFT;
452 #endif
453 #ifdef _SURF
454         if(!strcmp(text, _("SURF"))) return ALGORITHM_SURF;
455 #endif
456 #ifdef _ORB
457         if(!strcmp(text, _("ORB"))) return ALGORITHM_ORB;
458 #endif
459 #ifdef _AKAZE
460         if(!strcmp(text, _("AKAZE"))) return ALGORITHM_AKAZE;
461 #endif
462 #ifdef _BRISK
463         if(!strcmp(text, _("BRISK"))) return ALGORITHM_BRISK;
464 #endif
465         return NO_ALGORITHM;
466 }
467
468 char* FindObjAlgorithm::to_text(int mode)
469 {
470         switch( mode ) {
471 #ifdef _SIFT
472         case ALGORITHM_SIFT:    return _("SIFT");
473 #endif
474 #ifdef _SURF
475         case ALGORITHM_SURF:    return _("SURF");
476 #endif
477 #ifdef _ORB
478         case ALGORITHM_ORB:     return _("ORB");
479 #endif
480 #ifdef _AKAZE
481         case ALGORITHM_AKAZE:   return _("AKAZE");
482 #endif
483 #ifdef _BRISK
484         case ALGORITHM_BRISK:   return _("BRISK");
485 #endif
486         }
487         return _("Don't Calculate");
488 }
489
490 int FindObjAlgorithm::calculate_w(FindObjWindow *gui)
491 {
492         int result = 0;
493         result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(NO_ALGORITHM)));
494 #ifdef _SIFT
495         result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(ALGORITHM_SIFT)));
496 #endif
497 #ifdef _SURF
498         result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(ALGORITHM_SURF)));
499 #endif
500 #ifdef _ORB
501         result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(ALGORITHM_ORB)));
502 #endif
503 #ifdef _AKAZE
504         result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(ALGORITHM_AKAZE)));
505 #endif
506 #ifdef _BRISK
507         result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(ALGORITHM_BRISK)));
508 #endif
509         return result + 50;
510 }
511
512
513 FindObjUseFlann::FindObjUseFlann(FindObjMain *plugin, FindObjWindow *gui,
514         int x, int y)
515  : BC_CheckBox(x, y, plugin->config.use_flann, _("Use FLANN"))
516 {
517         this->gui = gui;
518         this->plugin = plugin;
519 }
520
521 int FindObjUseFlann::handle_event()
522 {
523         plugin->config.use_flann = get_value();
524         plugin->send_configure_change();
525         return 1;
526 }
527
528
529 FindObjLayer::FindObjLayer(FindObjMain *plugin, FindObjWindow *gui,
530         int x, int y, int *value)
531  : BC_TumbleTextBox(gui, *value, MIN_LAYER, MAX_LAYER, x, y, calculate_w(gui))
532 {
533         this->plugin = plugin;
534         this->gui = gui;
535         this->value = value;
536 }
537
538 int FindObjLayer::handle_event()
539 {
540         *value = atoi(get_text());
541         plugin->send_configure_change();
542         return 1;
543 }
544
545 int FindObjLayer::calculate_w(FindObjWindow *gui)
546 {
547         int result = 0;
548         result = gui->get_text_width(MEDIUMFONT, "000");
549         return result + 50;
550 }
551
552
553 FindObjBlend::FindObjBlend(FindObjMain *plugin,
554         int x,
555         int y,
556         int *value)
557  : BC_IPot(x,
558                 y,
559                 (int64_t)*value,
560                 (int64_t)MIN_BLEND,
561                 (int64_t)MAX_BLEND)
562 {
563         this->plugin = plugin;
564         this->value = value;
565 }
566
567 int FindObjBlend::handle_event()
568 {
569         *value = (int)get_value();
570         plugin->send_configure_change();
571         return 1;
572 }
573