r370: Heroine Virutal's official release 1.2.1
[cinelerra_cv/mob.git] / hvirtual / plugins / swapchannels / swapchannels.C
blobe7d7967dd4cd5cc8b6af3a5847b060a75ef8f7f8
1 #include "bcdisplayinfo.h"
2 #include "clip.h"
3 #include "defaults.h"
4 #include "filexml.h"
5 #include "language.h"
6 #include "picon_png.h"
7 #include "swapchannels.h"
8 #include "vframe.h"
12 #include <stdint.h>
13 #include <string.h>
20 REGISTER_PLUGIN(SwapMain)
29 SwapConfig::SwapConfig()
31         red = RED_SRC;
32         green = GREEN_SRC;
33         blue = BLUE_SRC;
34     alpha = ALPHA_SRC;
38 int SwapConfig::equivalent(SwapConfig &that)
40         return (red == that.red &&
41                 green == that.green &&
42                 blue == that.blue &&
43                 alpha == that.alpha);
46 void SwapConfig::copy_from(SwapConfig &that)
48         red = that.red;
49         green = that.green;
50         blue = that.blue;
51         alpha = that.alpha;
61 SwapWindow::SwapWindow(SwapMain *plugin, int x, int y)
62  : BC_Window(plugin->gui_string, 
63         x,
64         y,
65         250, 
66         170, 
67         250, 
68         170, 
69         0, 
70         0,
71         1)
73         this->plugin = plugin;
76 SwapWindow::~SwapWindow()
80         
81 void SwapWindow::create_objects()
83         int x = 10, y = 10;
84         int margin = 30;
86         add_subwindow(new BC_Title(x, y, _("Swap channels")));
87         y += margin;
88         add_subwindow(new BC_Title(x + 160, y + 5, _("-> Red")));
89         add_subwindow(red = new SwapMenu(plugin, &(plugin->config.red), x, y));
90         red->create_objects();
91         y += margin;
92         add_subwindow(new BC_Title(x + 160, y + 5, _("-> Green")));
93         add_subwindow(green = new SwapMenu(plugin, &(plugin->config.green), x, y));
94         green->create_objects();
95         y += margin;
96         add_subwindow(new BC_Title(x + 160, y + 5, _("-> Blue")));
97         add_subwindow(blue = new SwapMenu(plugin, &(plugin->config.blue), x, y));
98         blue->create_objects();
99         y += margin;
100         add_subwindow(new BC_Title(x + 160, y + 5, _("-> Alpha")));
101         add_subwindow(alpha = new SwapMenu(plugin, &(plugin->config.alpha), x, y));
102         alpha->create_objects();
104         show_window();
105         flush();
108 WINDOW_CLOSE_EVENT(SwapWindow)
116 SwapMenu::SwapMenu(SwapMain *client, int *output, int x, int y)
117  : BC_PopupMenu(x, y, 150, client->output_to_text(*output))
119         this->client = client;
120         this->output = output;
123 int SwapMenu::handle_event()
125         client->send_configure_change();
126         return 1;
129 int SwapMenu::create_objects()
131         add_item(new SwapItem(this, client->output_to_text(RED_SRC)));
132         add_item(new SwapItem(this, client->output_to_text(GREEN_SRC)));
133         add_item(new SwapItem(this, client->output_to_text(BLUE_SRC)));
134         add_item(new SwapItem(this, client->output_to_text(ALPHA_SRC)));
135         add_item(new SwapItem(this, client->output_to_text(NO_SRC)));
136         add_item(new SwapItem(this, client->output_to_text(MAX_SRC)));
137         return 0;
143 SwapItem::SwapItem(SwapMenu *menu, char *title)
144  : BC_MenuItem(title)
146         this->menu = menu;
149 int SwapItem::handle_event()
151         menu->set_text(get_text());
152         *(menu->output) = menu->client->text_to_output(get_text());
153         menu->handle_event();
154         return 1;
165 PLUGIN_THREAD_OBJECT(SwapMain, SwapThread, SwapWindow)
176 SwapMain::SwapMain(PluginServer *server)
177  : PluginVClient(server)
179         reset();
180         PLUGIN_CONSTRUCTOR_MACRO
183 SwapMain::~SwapMain()
185         PLUGIN_DESTRUCTOR_MACRO
186         
187         if(temp) delete temp;
190 void SwapMain::reset()
192         temp = 0;
196 char* SwapMain::plugin_title()  { return N_("Swap channels"); }
197 int SwapMain::is_synthesis() { return 1; }
198 int SwapMain::is_realtime()  { return 1; }
201 SHOW_GUI_MACRO(SwapMain, SwapThread)
202 NEW_PICON_MACRO(SwapMain)
203 SET_STRING_MACRO(SwapMain)
204 RAISE_WINDOW_MACRO(SwapMain)
206 int SwapMain::load_defaults()
208         char directory[1024], string[1024];
209 // set the default directory
210         sprintf(directory, "%sswapchannels.rc", BCASTDIR);
212 // load the defaults
213         defaults = new Defaults(directory);
214         defaults->load();
216         config.red = defaults->get("RED", config.red);
217         config.green = defaults->get("GREEN", config.green);
218         config.blue = defaults->get("BLUE", config.blue);
219         config.alpha = defaults->get("ALPHA", config.alpha);
220         return 0;
223 int SwapMain::save_defaults()
225         defaults->update("RED", config.red);
226         defaults->update("GREEN", config.green);
227         defaults->update("BLUE", config.blue);
228         defaults->update("ALPHA", config.alpha);
229         defaults->save();
230         return 0;
233 void SwapMain::save_data(KeyFrame *keyframe)
235         FileXML output;
237 // cause data to be stored directly in text
238         output.set_shared_string(keyframe->data, MESSAGESIZE);
239         output.tag.set_title("SWAPCHANNELS");
240         output.tag.set_property("RED", config.red);
241         output.tag.set_property("GREEN", config.green);
242         output.tag.set_property("BLUE", config.blue);
243         output.tag.set_property("ALPHA", config.alpha);
244         output.append_tag();
245         output.append_newline();
246         output.terminate_string();
247 // data is now in *text
250 void SwapMain::read_data(KeyFrame *keyframe)
252         FileXML input;
254         input.set_shared_string(keyframe->data, strlen(keyframe->data));
256         int result = 0;
258         while(!result)
259         {
260                 result = input.read_tag();
262                 if(!result)
263                 {
264                         if(input.tag.title_is("SWAPCHANNELS"))
265                         {
266                                 config.red = input.tag.get_property("RED", config.red);
267                                 config.green = input.tag.get_property("GREEN", config.green);
268                                 config.blue = input.tag.get_property("BLUE", config.blue);
269                                 config.alpha = input.tag.get_property("ALPHA", config.alpha);
270                         }
271                 }
272         }
275 void SwapMain::update_gui()
277         if(thread) 
278         {
279                 load_configuration();
280                 thread->window->lock_window();
281                 thread->window->red->set_text(output_to_text(config.red));
282                 thread->window->green->set_text(output_to_text(config.green));
283                 thread->window->blue->set_text(output_to_text(config.blue));
284                 thread->window->alpha->set_text(output_to_text(config.alpha));
285                 thread->window->unlock_window();
286         }
290 void SwapMain::load_configuration()
292         KeyFrame *prev_keyframe;
293         prev_keyframe = get_prev_keyframe(get_source_position());
294         
295         read_data(prev_keyframe);
321 #define MAXMINSRC(src, max) \
322         (src == MAX_SRC ? max : 0)
324 #define SWAP_CHANNELS(type, max, components) \
325 { \
326         int h = input_ptr->get_h(); \
327         int w = input_ptr->get_w(); \
328         int red = config.red; \
329         int green = config.green; \
330         int blue = config.blue; \
331         int alpha = config.alpha; \
333         if(components == 3) \
334         { \
335                 if(red == ALPHA_SRC) red = MAX_SRC; \
336                 if(green == ALPHA_SRC) green = MAX_SRC; \
337                 if(blue == ALPHA_SRC) blue = MAX_SRC; \
338         } \
341         for(int i = 0; i < h; i++) \
342         { \
343                 type *inrow = (type*)input_ptr->get_rows()[i]; \
344                 type *outrow = (type*)temp->get_rows()[i]; \
346                 for(int j = 0; j < w; j++) \
347                 { \
348                         if(red < 4) \
349                                 *outrow++ = *(inrow + red); \
350                         else \
351                                 *outrow++ = MAXMINSRC(red, max); \
353                         if(green < 4) \
354                                 *outrow++ = *(inrow + green); \
355                         else \
356                                 *outrow++ = MAXMINSRC(green, max); \
358                         if(blue < 4) \
359                                 *outrow++ = *(inrow + blue); \
360                         else \
361                                 *outrow++ = MAXMINSRC(blue, max); \
363                         if(components == 4) \
364                         { \
365                                 if(alpha < 4) \
366                                         *outrow++ = *(inrow + alpha); \
367                                 else \
368                                         *outrow++ = MAXMINSRC(alpha, max); \
369                         } \
371                         inrow += components; \
372                 } \
373         } \
375         output_ptr->copy_from(temp); \
380 int SwapMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
382         load_configuration();
385         if(!temp) 
386                 temp = new VFrame(0, 
387                         input_ptr->get_w(), 
388                         input_ptr->get_h(), 
389                         input_ptr->get_color_model());
391         switch(input_ptr->get_color_model())
392         {
393                 case BC_RGB_FLOAT:
394                         SWAP_CHANNELS(float, 1, 3);
395                         break;
396                 case BC_RGBA_FLOAT:
397                         SWAP_CHANNELS(float, 1, 4);
398                         break;
399                 case BC_RGB888:
400                 case BC_YUV888:
401                         SWAP_CHANNELS(unsigned char, 0xff, 3);
402                         break;
403                 case BC_RGBA8888:
404                 case BC_YUVA8888:
405                         SWAP_CHANNELS(unsigned char, 0xff, 4);
406                         break;
407                 case BC_RGB161616:
408                 case BC_YUV161616:
409                         SWAP_CHANNELS(uint16_t, 0xffff, 3);
410                         break;
411                 case BC_RGBA16161616:
412                 case BC_YUVA16161616:
413                         SWAP_CHANNELS(uint16_t, 0xffff, 4);
414                         break;
415         }
416         
417         
418         return 0;
422 char* SwapMain::output_to_text(int value)
424         switch(value)
425         {
426                 case RED_SRC:
427                         return _("Red");
428                         break;
429                 case GREEN_SRC:
430                         return _("Green");
431                         break;
432                 case BLUE_SRC:
433                         return _("Blue");
434                         break;
435                 case ALPHA_SRC:
436                         return _("Alpha");
437                         break;
438                 case NO_SRC:
439                         return _("0%");
440                         break;
441                 case MAX_SRC:
442                         return _("100%");
443                         break;
444                 default:
445                         return "";
446                         break;
447         }
450 int SwapMain::text_to_output(char *text)
452         if(!strcmp(text, _("Red"))) return RED_SRC;
453         if(!strcmp(text, _("Green"))) return GREEN_SRC;
454         if(!strcmp(text, _("Blue"))) return BLUE_SRC;
455         if(!strcmp(text, _("Alpha"))) return ALPHA_SRC;
456         if(!strcmp(text, _("0%"))) return NO_SRC;
457         if(!strcmp(text, _("100%"))) return MAX_SRC;
458         return 0;