r705: More gettextizations (mostly plugins) by Jean-Luc Coulon.
[cinelerra_cv.git] / plugins / overlay / overlay.C
blob4f22f31645a9f85f3ec396da7fc9277585f7e8b9
1 #include "bcdisplayinfo.h"
2 #include "clip.h"
3 #include "defaults.h"
4 #include "filexml.h"
5 #include "guicast.h"
6 #include "keyframe.h"
7 #include "language.h"
8 #include "overlayframe.h"
9 #include "picon_png.h"
10 #include "pluginvclient.h"
11 #include "vframe.h"
13 #include <string.h>
14 #include <stdint.h>
17 class Overlay;
18 class OverlayWindow;
21 class OverlayConfig
23 public:
24         OverlayConfig();
26         static char* mode_to_text(int mode);
27         int mode;
29         static char* direction_to_text(int direction);
30         int direction;
31         enum
32         {
33                 BOTTOM_FIRST,
34                 TOP_FIRST
35         };
37         static char* output_to_text(int output_layer);
38         int output_layer;
39         enum
40         {
41                 TOP,
42                 BOTTOM
43         };
50 class OverlayMode : public BC_PopupMenu
52 public:
53         OverlayMode(Overlay *plugin,
54                 int x, 
55                 int y);
56         void create_objects();
57         int handle_event();
58         Overlay *plugin;
61 class OverlayDirection : public BC_PopupMenu
63 public:
64         OverlayDirection(Overlay *plugin,
65                 int x, 
66                 int y);
67         void create_objects();
68         int handle_event();
69         Overlay *plugin;
72 class OverlayOutput : public BC_PopupMenu
74 public:
75         OverlayOutput(Overlay *plugin,
76                 int x, 
77                 int y);
78         void create_objects();
79         int handle_event();
80         Overlay *plugin;
84 class OverlayWindow : public BC_Window
86 public:
87         OverlayWindow(Overlay *plugin, int x, int y);
88         ~OverlayWindow();
90         void create_objects();
91         int close_event();
93         Overlay *plugin;
94         OverlayMode *mode;
95         OverlayDirection *direction;
96         OverlayOutput *output;
100 PLUGIN_THREAD_HEADER(Overlay, OverlayThread, OverlayWindow)
104 class Overlay : public PluginVClient
106 public:
107         Overlay(PluginServer *server);
108         ~Overlay();
111         PLUGIN_CLASS_MEMBERS(OverlayConfig, OverlayThread);
113         int process_buffer(VFrame **frame,
114                 int64_t start_position,
115                 double frame_rate);
116         int is_realtime();
117         int is_multichannel();
118         int load_defaults();
119         int save_defaults();
120         void save_data(KeyFrame *keyframe);
121         void read_data(KeyFrame *keyframe);
122         void update_gui();
124         OverlayFrame *overlayer;
125         VFrame *temp;
139 OverlayConfig::OverlayConfig()
141         mode = TRANSFER_NORMAL;
142         direction = OverlayConfig::BOTTOM_FIRST;
143         output_layer = OverlayConfig::TOP;
146 char* OverlayConfig::mode_to_text(int mode)
148         switch(mode)
149         {
150                 case TRANSFER_NORMAL:
151                         return "Normal";
152                         break;
154                 case TRANSFER_REPLACE:
155                         return "Replace";
156                         break;
158                 case TRANSFER_ADDITION:
159                         return "Addition";
160                         break;
162                 case TRANSFER_SUBTRACT:
163                         return "Subtract";
164                         break;
166                 case TRANSFER_MULTIPLY:
167                         return "Multiply";
168                         break;
170                 case TRANSFER_DIVIDE:
171                         return "Divide";
172                         break;
174                 default:
175                         return "Normal";
176                         break;
177         }
178         return "";
181 char* OverlayConfig::direction_to_text(int direction)
183         switch(direction)
184         {
185                 case OverlayConfig::BOTTOM_FIRST: return _("Bottom first");
186                 case OverlayConfig::TOP_FIRST:    return _("Top first");
187         }
188         return "";
191 char* OverlayConfig::output_to_text(int output_layer)
193         switch(output_layer)
194         {
195                 case OverlayConfig::TOP:    return _("Top");
196                 case OverlayConfig::BOTTOM: return _("Bottom");
197         }
198         return "";
209 OverlayWindow::OverlayWindow(Overlay *plugin, int x, int y)
210  : BC_Window(plugin->gui_string, 
211         x, 
212         y, 
213         300, 
214         160, 
215         300, 
216         160, 
217         0, 
218         0,
219         1)
221         this->plugin = plugin;
224 OverlayWindow::~OverlayWindow()
228 void OverlayWindow::create_objects()
230         int x = 10, y = 10;
232         BC_Title *title;
233         add_subwindow(title = new BC_Title(x, y, _("Mode:")));
234         add_subwindow(mode = new OverlayMode(plugin, 
235                 x + title->get_w() + 5, 
236                 y));
237         mode->create_objects();
239         y += 30;
240         add_subwindow(title = new BC_Title(x, y, _("Layer order:")));
241         add_subwindow(direction = new OverlayDirection(plugin, 
242                 x + title->get_w() + 5, 
243                 y));
244         direction->create_objects();
246         y += 30;
247         add_subwindow(title = new BC_Title(x, y, _("Output layer:")));
248         add_subwindow(output = new OverlayOutput(plugin, 
249                 x + title->get_w() + 5, 
250                 y));
251         output->create_objects();
253         show_window();
254         flush();
257 WINDOW_CLOSE_EVENT(OverlayWindow)
263 OverlayMode::OverlayMode(Overlay *plugin,
264         int x, 
265         int y)
266  : BC_PopupMenu(x,
267         y,
268         150,
269         OverlayConfig::mode_to_text(plugin->config.mode),
270         1)
272         this->plugin = plugin;
275 void OverlayMode::create_objects()
277         for(int i = 0; i < TRANSFER_TYPES; i++)
278                 add_item(new BC_MenuItem(OverlayConfig::mode_to_text(i)));
281 int OverlayMode::handle_event()
283         char *text = get_text();
285         for(int i = 0; i < TRANSFER_TYPES; i++)
286         {
287                 if(!strcmp(text, OverlayConfig::mode_to_text(i)))
288                 {
289                         plugin->config.mode = i;
290                         break;
291                 }
292         }
294         plugin->send_configure_change();
295         return 1;
299 OverlayDirection::OverlayDirection(Overlay *plugin,
300         int x, 
301         int y)
302  : BC_PopupMenu(x,
303         y,
304         150,
305         OverlayConfig::direction_to_text(plugin->config.direction),
306         1)
308         this->plugin = plugin;
311 void OverlayDirection::create_objects()
313         add_item(new BC_MenuItem(
314                 OverlayConfig::direction_to_text(
315                         OverlayConfig::TOP_FIRST)));
316         add_item(new BC_MenuItem(
317                 OverlayConfig::direction_to_text(
318                         OverlayConfig::BOTTOM_FIRST)));
321 int OverlayDirection::handle_event()
323         char *text = get_text();
325         if(!strcmp(text, 
326                 OverlayConfig::direction_to_text(
327                         OverlayConfig::TOP_FIRST)))
328                 plugin->config.direction = OverlayConfig::TOP_FIRST;
329         else
330         if(!strcmp(text, 
331                 OverlayConfig::direction_to_text(
332                         OverlayConfig::BOTTOM_FIRST)))
333                 plugin->config.direction = OverlayConfig::BOTTOM_FIRST;
335         plugin->send_configure_change();
336         return 1;
340 OverlayOutput::OverlayOutput(Overlay *plugin,
341         int x, 
342         int y)
343  : BC_PopupMenu(x,
344         y,
345         100,
346         OverlayConfig::output_to_text(plugin->config.output_layer),
347         1)
349         this->plugin = plugin;
352 void OverlayOutput::create_objects()
354         add_item(new BC_MenuItem(
355                 OverlayConfig::output_to_text(
356                         OverlayConfig::TOP)));
357         add_item(new BC_MenuItem(
358                 OverlayConfig::output_to_text(
359                         OverlayConfig::BOTTOM)));
362 int OverlayOutput::handle_event()
364         char *text = get_text();
366         if(!strcmp(text, 
367                 OverlayConfig::output_to_text(
368                         OverlayConfig::TOP)))
369                 plugin->config.output_layer = OverlayConfig::TOP;
370         else
371         if(!strcmp(text, 
372                 OverlayConfig::output_to_text(
373                         OverlayConfig::BOTTOM)))
374                 plugin->config.output_layer = OverlayConfig::BOTTOM;
376         plugin->send_configure_change();
377         return 1;
388 PLUGIN_THREAD_OBJECT(Overlay, OverlayThread, OverlayWindow)
399 REGISTER_PLUGIN(Overlay)
406 Overlay::Overlay(PluginServer *server)
407  : PluginVClient(server)
409         PLUGIN_CONSTRUCTOR_MACRO
410         overlayer = 0;
411         temp = 0;
415 Overlay::~Overlay()
417         PLUGIN_DESTRUCTOR_MACRO
418         if(overlayer) delete overlayer;
419         if(temp) delete temp;
424 int Overlay::process_buffer(VFrame **frame,
425         int64_t start_position,
426         double frame_rate)
428         load_configuration();
430         if(!temp) temp = new VFrame(0,
431                 frame[0]->get_w(),
432                 frame[0]->get_h(),
433                 frame[0]->get_color_model(),
434                 -1);
436         if(!overlayer)
437                 overlayer = new OverlayFrame(get_project_smp() + 1);
439 // Inclusive layer numbers
440         int input_layer1;
441         int input_layer2;
442         int step;
443         int output_layer;
444         VFrame *output;
446         if(config.direction == OverlayConfig::BOTTOM_FIRST)
447         {
448                 input_layer1 = get_total_buffers() - 1;
449                 input_layer2 = -1;
450                 step = -1;
451         }
452         else
453         {
454                 input_layer1 = 0;
455                 input_layer2 = get_total_buffers();
456                 step = 1;
457         }
459         if(config.output_layer == OverlayConfig::TOP)
460         {
461                 output_layer = 0;
462         }
463         else
464         {
465                 output_layer = get_total_buffers() - 1;
466         }
470 // Direct copy the first layer
471         output = frame[output_layer];
472         read_frame(output, 
473                 input_layer1, 
474                 start_position,
475                 frame_rate);
476         for(int i = input_layer1 + step; i != input_layer2; i += step)
477         {
478                 read_frame(temp, 
479                         i, 
480                         start_position,
481                         frame_rate);
482                 overlayer->overlay(output,
483                         temp,
484                         0,
485                         0,
486                         output->get_w(),
487                         output->get_h(),
488                         0,
489                         0,
490                         output->get_w(),
491                         output->get_h(),
492                         1,
493                         config.mode,
494                         NEAREST_NEIGHBOR);
495         }
498         return 0;
504 char* Overlay::plugin_title() { return N_("Overlay"); }
505 int Overlay::is_realtime() { return 1; }
506 int Overlay::is_multichannel() { return 1; }
510 NEW_PICON_MACRO(Overlay) 
512 SHOW_GUI_MACRO(Overlay, OverlayThread)
514 RAISE_WINDOW_MACRO(Overlay)
516 SET_STRING_MACRO(Overlay);
518 int Overlay::load_configuration()
520         KeyFrame *prev_keyframe;
521         prev_keyframe = get_prev_keyframe(get_source_position());
522         read_data(prev_keyframe);
523         return 0;
526 int Overlay::load_defaults()
528         char directory[BCTEXTLEN];
529 // set the default directory
530         sprintf(directory, "%soverlay.rc", BCASTDIR);
532 // load the defaults
533         defaults = new Defaults(directory);
534         defaults->load();
536         config.mode = defaults->get("MODE", config.mode);
537         config.direction = defaults->get("DIRECTION", config.direction);
538         config.output_layer = defaults->get("OUTPUT_LAYER", config.output_layer);
539         return 0;
542 int Overlay::save_defaults()
544         defaults->update("MODE", config.mode);
545         defaults->update("DIRECTION", config.direction);
546         defaults->update("OUTPUT_LAYER", config.output_layer);
547         defaults->save();
548         return 0;
551 void Overlay::save_data(KeyFrame *keyframe)
553         FileXML output;
555 // cause data to be stored directly in text
556         output.set_shared_string(keyframe->data, MESSAGESIZE);
557         output.tag.set_title("OVERLAY");
558         output.tag.set_property("MODE", config.mode);
559         output.tag.set_property("DIRECTION", config.direction);
560         output.tag.set_property("OUTPUT_LAYER", config.output_layer);
561         output.append_tag();
562         output.terminate_string();
565 void Overlay::read_data(KeyFrame *keyframe)
567         FileXML input;
569         input.set_shared_string(keyframe->data, strlen(keyframe->data));
571         int result = 0;
573         while(!input.read_tag())
574         {
575                 if(input.tag.title_is("OVERLAY"))
576                 {
577                         config.mode = input.tag.get_property("MODE", config.mode);
578                         config.direction = input.tag.get_property("DIRECTION", config.direction);
579                         config.output_layer = input.tag.get_property("OUTPUT_LAYER", config.output_layer);
580                 }
581         }
584 void Overlay::update_gui()
586         if(thread)
587         {
588                 thread->window->lock_window("Overlay::update_gui");
589                 thread->window->mode->set_text(OverlayConfig::mode_to_text(config.mode));
590                 thread->window->direction->set_text(OverlayConfig::direction_to_text(config.direction));
591                 thread->window->output->set_text(OverlayConfig::output_to_text(config.output_layer));
592                 thread->window->unlock_window();
593         }