r864: Merge 2.1:
[cinelerra_cv/ct.git] / plugins / invertvideo / invert.C
blob59927c0d6440d1ae3944d069f599c1f9971b54e9
1 #include "bcdisplayinfo.h"
2 #include "clip.h"
3 #include "bchash.h"
4 #include "filexml.h"
5 #include "guicast.h"
6 #include "language.h"
7 #include "picon_png.h"
8 #include "plugincolors.h"
9 #include "pluginvclient.h"
10 #include "vframe.h"
12 #include <stdint.h>
13 #include <string.h>
17 class InvertVideoEffect;
20 class InvertVideoConfig
22 public:
23         InvertVideoConfig();
25         void copy_from(InvertVideoConfig &src);
26         int equivalent(InvertVideoConfig &src);
27         void interpolate(InvertVideoConfig &prev, 
28                 InvertVideoConfig &next, 
29                 long prev_frame, 
30                 long next_frame, 
31                 long current_frame);
33         int r, g, b, a;
36 class InvertVideoEnable : public BC_CheckBox
38 public:
39         InvertVideoEnable(InvertVideoEffect *plugin, int *output, int x, int y, char *text);
40         int handle_event();
41         InvertVideoEffect *plugin;
42         int *output;
45 class InvertVideoWindow : public BC_Window
47 public:
48         InvertVideoWindow(InvertVideoEffect *plugin, int x, int y);
49         void create_objects();
50         int close_event();
51         InvertVideoEnable *r, *g, *b, *a;
52         InvertVideoEffect *plugin;
55 PLUGIN_THREAD_HEADER(InvertVideoEffect, InvertVideoThread, InvertVideoWindow)
57 class InvertVideoEffect : public PluginVClient
59 public:
60         InvertVideoEffect(PluginServer *server);
61         ~InvertVideoEffect();
62         int process_buffer(VFrame *frame,
63                 int64_t start_position,
64                 double frame_rate);
65         int is_realtime();
66         char* plugin_title();
67         VFrame* new_picon();
68         int load_defaults();
69         int save_defaults();
70         void save_data(KeyFrame *keyframe);
71         void read_data(KeyFrame *keyframe);
72         void update_gui();
73         int show_gui();
74         void raise_window();
75         int set_string();
76         int load_configuration();
78         InvertVideoConfig config;
79         InvertVideoThread *thread;
80         BC_Hash *defaults;
87 REGISTER_PLUGIN(InvertVideoEffect)
95 InvertVideoConfig::InvertVideoConfig()
97         r = 1;
98         g = 1;
99         b = 1;
100         a = 1;
103 void InvertVideoConfig::copy_from(InvertVideoConfig &src)
105         r = src.r;
106         g = src.g;
107         b = src.b;
108         a = src.a;
111 int InvertVideoConfig::equivalent(InvertVideoConfig &src)
113         return r == src.r && 
114                 g == src.g && 
115                 b == src.b && 
116                 a == src.a;
119 void InvertVideoConfig::interpolate(InvertVideoConfig &prev, 
120         InvertVideoConfig &next, 
121         long prev_frame, 
122         long next_frame, 
123         long current_frame)
125         r = prev.r;
126         g = prev.g;
127         b = prev.b;
128         a = prev.a;
134 InvertVideoEnable::InvertVideoEnable(InvertVideoEffect *plugin, int *output, int x, int y, char *text)
135  : BC_CheckBox(x, y, *output, text)
137         this->plugin = plugin;
138         this->output = output;
140 int InvertVideoEnable::handle_event()
142         *output = get_value();
143         plugin->send_configure_change();
144         return 1;
151 InvertVideoWindow::InvertVideoWindow(InvertVideoEffect *plugin, int x, int y)
152  : BC_Window(plugin->gui_string, 
153         x, 
154         y, 
155         260, 
156         130, 
157         260, 
158         130, 
159         0, 
160         0,
161         1)
163         this->plugin = plugin;
166 void InvertVideoWindow::create_objects()
168         int x = 10, y = 10;
169         add_subwindow(r = new InvertVideoEnable(plugin, &plugin->config.r, x, y, _("Invert R")));
170         y += 30;
171         add_subwindow(g = new InvertVideoEnable(plugin, &plugin->config.g, x, y, _("Invert G")));
172         y += 30;
173         add_subwindow(b = new InvertVideoEnable(plugin, &plugin->config.b, x, y, _("Invert B")));
174         y += 30;
175         add_subwindow(a = new InvertVideoEnable(plugin, &plugin->config.a, x, y, _("Invert A")));
177         show_window();
178         flush();
181 WINDOW_CLOSE_EVENT(InvertVideoWindow)
187 PLUGIN_THREAD_OBJECT(InvertVideoEffect, InvertVideoThread, InvertVideoWindow)
194 InvertVideoEffect::InvertVideoEffect(PluginServer *server)
195  : PluginVClient(server)
197         PLUGIN_CONSTRUCTOR_MACRO
199 InvertVideoEffect::~InvertVideoEffect()
201         PLUGIN_DESTRUCTOR_MACRO
204 char* InvertVideoEffect::plugin_title() { return N_("Invert Video"); }
205 int InvertVideoEffect::is_realtime() { return 1; }
207 NEW_PICON_MACRO(InvertVideoEffect)
208 SHOW_GUI_MACRO(InvertVideoEffect, InvertVideoThread)
209 RAISE_WINDOW_MACRO(InvertVideoEffect)
210 SET_STRING_MACRO(InvertVideoEffect)
211 LOAD_CONFIGURATION_MACRO(InvertVideoEffect, InvertVideoConfig)
213 void InvertVideoEffect::update_gui()
215         if(thread)
216         {
217                 thread->window->lock_window();
218                 load_configuration();
219                 thread->window->r->update(config.r);
220                 thread->window->g->update(config.g);
221                 thread->window->b->update(config.b);
222                 thread->window->a->update(config.a);
223                 thread->window->unlock_window();
224         }
227 int InvertVideoEffect::load_defaults()
229         char directory[BCTEXTLEN];
230         sprintf(directory, "%sinvertvideo.rc", BCASTDIR);
231         defaults = new BC_Hash(directory);
232         defaults->load();
233         config.r = defaults->get("R", config.r);
234         config.g = defaults->get("G", config.g);
235         config.b = defaults->get("B", config.b);
236         config.a = defaults->get("A", config.a);
237         return 0;
240 int InvertVideoEffect::save_defaults()
242         defaults->update("R", config.r);
243         defaults->update("G", config.g);
244         defaults->update("B", config.b);
245         defaults->update("A", config.a);
246         defaults->save();
247         return 0;
250 void InvertVideoEffect::save_data(KeyFrame *keyframe)
252         FileXML output;
253         output.set_shared_string(keyframe->data, MESSAGESIZE);
254         output.tag.set_title("INVERTVIDEO");
255         output.tag.set_property("R", config.r);
256         output.tag.set_property("G", config.g);
257         output.tag.set_property("B", config.b);
258         output.tag.set_property("A", config.a);
259         output.append_tag();
260         output.terminate_string();
263 void InvertVideoEffect::read_data(KeyFrame *keyframe)
265         FileXML input;
266         input.set_shared_string(keyframe->data, strlen(keyframe->data));
267         while(!input.read_tag())
268         {
269                 if(input.tag.title_is("INVERTVIDEO"))
270                 {
271                         config.r = input.tag.get_property("R", config.r);
272                         config.g = input.tag.get_property("G", config.g);
273                         config.b = input.tag.get_property("B", config.b);
274                         config.a = input.tag.get_property("A", config.a);
275                 }
276         }
280 #define INVERT_MACRO(type, components, max) \
281 { \
282         for(int i = 0; i < frame->get_h(); i++) \
283         { \
284                 type *in_row = (type*)frame->get_rows()[i]; \
285                 type *out_row = (type*)frame->get_rows()[i]; \
287                 for(int j = 0; j < w; j++) \
288                 { \
289                         if(config.r) out_row[0] = max - in_row[0]; \
290                         if(config.g) out_row[1] = max - in_row[1]; \
291                         if(config.b) out_row[2] = max - in_row[2]; \
292                         if(components == 4) \
293                                 if(config.a) out_row[3] = max - in_row[3]; \
295                         in_row += components; \
296                         out_row += components; \
297                 } \
298         } \
301 int InvertVideoEffect::process_buffer(VFrame *frame,
302         int64_t start_position,
303         double frame_rate)
305         load_configuration();
307         read_frame(frame, 
308                 0, 
309                 start_position, 
310                 frame_rate);
313         if(config.r || config.g || config.b || config.a)
314         {
315                 int w = frame->get_w();
317                 switch(frame->get_color_model())
318                 {
319                         case BC_RGB_FLOAT:
320                                 INVERT_MACRO(float, 3, 1.0)
321                                 break;
322                         case BC_RGB888:
323                         case BC_YUV888:
324                                 INVERT_MACRO(unsigned char, 3, 0xff)
325                                 break;
326                         case BC_RGBA_FLOAT:
327                                 INVERT_MACRO(float, 4, 1.0)
328                                 break;
329                         case BC_RGBA8888:
330                         case BC_YUVA8888:
331                                 INVERT_MACRO(unsigned char, 4, 0xff)
332                                 break;
333                         case BC_RGB161616:
334                         case BC_YUV161616:
335                                 INVERT_MACRO(uint16_t, 3, 0xffff)
336                                 break;
337                         case BC_RGBA16161616:
338                         case BC_YUVA16161616:
339                                 INVERT_MACRO(uint16_t, 4, 0xffff)
340                                 break;
341                 }
342         }
344         return 0;