Condense the xml-cleanup into a short feature-branch
[cinelerra_cv/mob.git] / plugins / translate / translate.C
blobfe64a1569a7f34f1e3d65a881dcf56a8e18f60ba
1 #include "clip.h"
2 #include "filexml.h"
3 #include "picon_png.h"
4 #include "translate.h"
5 #include "translatewin.h"
7 #include <string.h>
9 #include <libintl.h>
10 #define _(String) gettext(String)
11 #define gettext_noop(String) String
12 #define N_(String) gettext_noop (String)
15 REGISTER_PLUGIN(TranslateMain)
17 TranslateConfig::TranslateConfig()
19         in_x = 0;
20         in_y = 0;
21         in_w = 720;
22         in_h = 480;
23         out_x = 0;
24         out_y = 0;
25         out_w = 720;
26         out_h = 480;
29 int TranslateConfig::equivalent(TranslateConfig &that)
31         return EQUIV(in_x, that.in_x) && 
32                 EQUIV(in_y, that.in_y) && 
33                 EQUIV(in_w, that.in_w) && 
34                 EQUIV(in_h, that.in_h) &&
35                 EQUIV(out_x, that.out_x) && 
36                 EQUIV(out_y, that.out_y) && 
37                 EQUIV(out_w, that.out_w) &&
38                 EQUIV(out_h, that.out_h);
41 void TranslateConfig::copy_from(TranslateConfig &that)
43         in_x = that.in_x;
44         in_y = that.in_y;
45         in_w = that.in_w;
46         in_h = that.in_h;
47         out_x = that.out_x;
48         out_y = that.out_y;
49         out_w = that.out_w;
50         out_h = that.out_h;
53 void TranslateConfig::interpolate(TranslateConfig &prev, 
54         TranslateConfig &next, 
55         int64_t prev_frame, 
56         int64_t next_frame, 
57         int64_t current_frame)
59         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
60         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
62         this->in_x = prev.in_x * prev_scale + next.in_x * next_scale;
63         this->in_y = prev.in_y * prev_scale + next.in_y * next_scale;
64         this->in_w = prev.in_w * prev_scale + next.in_w * next_scale;
65         this->in_h = prev.in_h * prev_scale + next.in_h * next_scale;
66         this->out_x = prev.out_x * prev_scale + next.out_x * next_scale;
67         this->out_y = prev.out_y * prev_scale + next.out_y * next_scale;
68         this->out_w = prev.out_w * prev_scale + next.out_w * next_scale;
69         this->out_h = prev.out_h * prev_scale + next.out_h * next_scale;
79 TranslateMain::TranslateMain(PluginServer *server)
80  : PluginVClient(server)
82         temp_frame = 0;
83         overlayer = 0;
84         PLUGIN_CONSTRUCTOR_MACRO
87 TranslateMain::~TranslateMain()
89         PLUGIN_DESTRUCTOR_MACRO
91         if(temp_frame) delete temp_frame;
92         temp_frame = 0;
93         if(overlayer) delete overlayer;
94         overlayer = 0;
97 char* TranslateMain::plugin_title() { return N_("Translate"); }
98 int TranslateMain::is_realtime() { return 1; }
100 NEW_PICON_MACRO(TranslateMain)
102 int TranslateMain::load_defaults()
104         char directory[1024], string[1024];
105 // set the default directory
106         sprintf(directory, "%stranslate.rc", BCASTDIR);
108 // load the defaults
109         defaults = new BC_Hash(directory);
110         defaults->load();
113         config.in_x = defaults->get("IN_X", config.in_x);
114         config.in_y = defaults->get("IN_Y", config.in_y);
115         config.in_w = defaults->get("IN_W", config.in_w);
116         config.in_h = defaults->get("IN_H", config.in_h);
117         config.out_x = defaults->get("OUT_X", config.out_x);
118         config.out_y = defaults->get("OUT_Y", config.out_y);
119         config.out_w = defaults->get("OUT_W", config.out_w);
120         config.out_h = defaults->get("OUT_H", config.out_h);
123 int TranslateMain::save_defaults()
125         defaults->update("IN_X", config.in_x);
126         defaults->update("IN_Y", config.in_y);
127         defaults->update("IN_W", config.in_w);
128         defaults->update("IN_H", config.in_h);
129         defaults->update("OUT_X", config.out_x);
130         defaults->update("OUT_Y", config.out_y);
131         defaults->update("OUT_W", config.out_w);
132         defaults->update("OUT_H", config.out_h);
133         defaults->save();
136 LOAD_CONFIGURATION_MACRO(TranslateMain, TranslateConfig)
138 void TranslateMain::save_data(KeyFrame *keyframe)
140         FileXML output;
142 // cause data to be stored directly in text
143         output.set_shared_string(keyframe->data, MESSAGESIZE);
145 // Store data
146         output.tag.set_title("TRANSLATE");
147         output.tag.set_property("IN_X", config.in_x);
148         output.tag.set_property("IN_Y", config.in_y);
149         output.tag.set_property("IN_W", config.in_w);
150         output.tag.set_property("IN_H", config.in_h);
151         output.tag.set_property("OUT_X", config.out_x);
152         output.tag.set_property("OUT_Y", config.out_y);
153         output.tag.set_property("OUT_W", config.out_w);
154         output.tag.set_property("OUT_H", config.out_h);
155         output.append_tag();
156         output.tag.set_title("/TRANSLATE");
157         output.append_tag();
159         output.terminate_string();
160 // data is now in *text
163 void TranslateMain::read_data(KeyFrame *keyframe)
165         FileXML input;
167         input.set_shared_string(keyframe->data, strlen(keyframe->data));
169         int result = 0;
171         while(!result)
172         {
173                 result = input.read_tag();
175                 if(!result)
176                 {
177                         if(input.tag.title_is("TRANSLATE"))
178                         {
179                                 config.in_x = input.tag.get_property("IN_X", config.in_x);
180                                 config.in_y = input.tag.get_property("IN_Y", config.in_y);
181                                 config.in_w = input.tag.get_property("IN_W", config.in_w);
182                                 config.in_h = input.tag.get_property("IN_H", config.in_h);
183                                 config.out_x =  input.tag.get_property("OUT_X", config.out_x);
184                                 config.out_y =  input.tag.get_property("OUT_Y", config.out_y);
185                                 config.out_w =  input.tag.get_property("OUT_W", config.out_w);
186                                 config.out_h =  input.tag.get_property("OUT_H", config.out_h);
187                         }
188                 }
189         }
199 int TranslateMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
201         VFrame *input, *output;
202         
203         
204         input = input_ptr;
205         output = output_ptr;
207         load_configuration();
209 //printf("TranslateMain::process_realtime 1 %p\n", input);
210         if(input->get_rows()[0] == output->get_rows()[0])
211         {
212                 if(!temp_frame) 
213                         temp_frame = new VFrame(0, 
214                                 input_ptr->get_w(), 
215                                 input_ptr->get_h(),
216                                 input->get_color_model());
217                 temp_frame->copy_from(input);
218                 input = temp_frame;
219         }
220 //printf("TranslateMain::process_realtime 2 %p\n", input);
223         if(!overlayer)
224         {
225                 overlayer = new OverlayFrame(smp + 1);
226         }
228         output->clear_frame();
231 // printf("TranslateMain::process_realtime 3 output=%p input=%p config.w=%f config.h=%f"
232 //      "%f %f %f %f -> %f %f %f %f\n", 
233 //      output,
234 //      input,
235 //      config.w, 
236 //      config.h,
237 //      in_x1, 
238 //      in_y1, 
239 //      in_x2, 
240 //      in_y2,
241 //      out_x1, 
242 //      out_y1, 
243 //      out_x2, 
244 //      out_y2);
245                 overlayer->overlay(output, 
246                         input,
247                         config.in_x, 
248                         config.in_y, 
249                         config.in_x + config.in_w,
250                         config.in_y + config.in_h,
251                         config.out_x, 
252                         config.out_y, 
253                         config.out_x + config.out_w,
254                         config.out_y + config.out_h,
255                         1,
256                         TRANSFER_REPLACE,
257                         get_interpolation_type());
264 SHOW_GUI_MACRO(TranslateMain, TranslateThread)
266 RAISE_WINDOW_MACRO(TranslateMain)
268 SET_STRING_MACRO(TranslateMain)
270 void TranslateMain::update_gui()
272         if(thread)
273         {
274                 if(load_configuration())
275                 {
276                         thread->window->lock_window();
277                         thread->window->in_x->update(config.in_x);
278                         thread->window->in_y->update(config.in_y);
279                         thread->window->in_w->update(config.in_w);
280                         thread->window->in_h->update(config.in_h);
281                         thread->window->out_x->update(config.out_x);
282                         thread->window->out_y->update(config.out_y);
283                         thread->window->out_w->update(config.out_w);
284                         thread->window->out_h->update(config.out_h);
285                         thread->window->unlock_window();
286                 }
287         }