fix trace locks hang, drag handle rework-again, 12 reset btns on plugins, booby on
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / swapchannels / swapchannels.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 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 "bchash.h"
25 #include "filexml.h"
26 #include "language.h"
27 #include "swapchannels.h"
28 #include "vframe.h"
29
30
31
32 #include <stdint.h>
33 #include <string.h>
34
35
36
37
38
39
40 REGISTER_PLUGIN(SwapMain)
41
42
43
44
45
46
47
48
49 SwapConfig::SwapConfig()
50 {
51         reset_Config();
52 }
53
54 void SwapConfig::reset_Config()
55 {
56         red = RED_SRC;
57         green = GREEN_SRC;
58         blue = BLUE_SRC;
59         alpha = ALPHA_SRC;
60 }
61
62
63 int SwapConfig::equivalent(SwapConfig &that)
64 {
65         return (red == that.red &&
66                 green == that.green &&
67                 blue == that.blue &&
68                 alpha == that.alpha);
69 }
70
71 void SwapConfig::copy_from(SwapConfig &that)
72 {
73         red = that.red;
74         green = that.green;
75         blue = that.blue;
76         alpha = that.alpha;
77 }
78
79
80
81
82
83
84
85
86 SwapWindow::SwapWindow(SwapMain *plugin)
87  : PluginClientWindow(plugin,
88         250,
89         200,
90         250,
91         200,
92         0)
93 {
94         this->plugin = plugin;
95 }
96
97 SwapWindow::~SwapWindow()
98 {
99 }
100
101
102 void SwapWindow::create_objects()
103 {
104         int x = 10, y = 10;
105         int margin = 30;
106
107         add_subwindow(new BC_Title(x, y, _("Swap channels")));
108         y += margin;
109         add_subwindow(new BC_Title(x + 160, y + 5, _("-> Red")));
110         add_subwindow(red = new SwapMenu(plugin, &(plugin->config.red), x, y));
111         red->create_objects();
112         y += margin;
113         add_subwindow(new BC_Title(x + 160, y + 5, _("-> Green")));
114         add_subwindow(green = new SwapMenu(plugin, &(plugin->config.green), x, y));
115         green->create_objects();
116         y += margin;
117         add_subwindow(new BC_Title(x + 160, y + 5, _("-> Blue")));
118         add_subwindow(blue = new SwapMenu(plugin, &(plugin->config.blue), x, y));
119         blue->create_objects();
120         y += margin;
121         add_subwindow(new BC_Title(x + 160, y + 5, _("-> Alpha")));
122         add_subwindow(alpha = new SwapMenu(plugin, &(plugin->config.alpha), x, y));
123         alpha->create_objects();
124
125         y += 40;
126         add_subwindow(reset = new SwapReset(plugin, this, x, y));
127
128         show_window();
129         flush();
130 }
131
132
133
134
135
136
137
138
139 SwapMenu::SwapMenu(SwapMain *client, int *output, int x, int y)
140  : BC_PopupMenu(x, y, 150, client->output_to_text(*output))
141 {
142         this->client = client;
143         this->output = output;
144 }
145
146 int SwapMenu::handle_event()
147 {
148         client->send_configure_change();
149         return 1;
150 }
151
152 void SwapMenu::create_objects()
153 {
154         add_item(new SwapItem(this, client->output_to_text(RED_SRC)));
155         add_item(new SwapItem(this, client->output_to_text(GREEN_SRC)));
156         add_item(new SwapItem(this, client->output_to_text(BLUE_SRC)));
157         add_item(new SwapItem(this, client->output_to_text(ALPHA_SRC)));
158         add_item(new SwapItem(this, client->output_to_text(NO_SRC)));
159         add_item(new SwapItem(this, client->output_to_text(MAX_SRC)));
160 }
161
162
163
164
165 SwapItem::SwapItem(SwapMenu *menu, const char *title)
166  : BC_MenuItem(title)
167 {
168         this->menu = menu;
169 }
170
171 int SwapItem::handle_event()
172 {
173         menu->set_text(get_text());
174         *(menu->output) = menu->client->text_to_output(get_text());
175         menu->handle_event();
176         return 1;
177 }
178
179
180
181 SwapReset::SwapReset(SwapMain *plugin, SwapWindow *gui, int x, int y)
182  : BC_GenericButton(x, y, _("Reset"))
183 {
184         this->plugin = plugin;
185         this->gui = gui;
186 }
187 SwapReset::~SwapReset()
188 {
189 }
190 int SwapReset::handle_event()
191 {
192         plugin->config.reset_Config();
193         plugin->send_configure_change();
194         plugin->update_gui();
195         return 1;
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 SwapMain::SwapMain(PluginServer *server)
214  : PluginVClient(server)
215 {
216         reset();
217
218 }
219
220 SwapMain::~SwapMain()
221 {
222
223
224 //      if(temp) delete temp;
225 }
226
227 void SwapMain::reset()
228 {
229         temp = 0;
230 }
231
232
233 const char* SwapMain::plugin_title() { return N_("Swap channels"); }
234 int SwapMain::is_synthesis() { return 1; }
235 int SwapMain::is_realtime()  { return 1; }
236
237 NEW_WINDOW_MACRO(SwapMain, SwapWindow)
238
239
240 void SwapMain::save_data(KeyFrame *keyframe)
241 {
242         FileXML output;
243
244 // cause data to be stored directly in text
245         output.set_shared_output(keyframe->xbuf);
246         output.tag.set_title("SWAPCHANNELS");
247         output.tag.set_property("RED", config.red);
248         output.tag.set_property("GREEN", config.green);
249         output.tag.set_property("BLUE", config.blue);
250         output.tag.set_property("ALPHA", config.alpha);
251         output.append_tag();
252         output.tag.set_title("/SWAPCHANNELS");
253         output.append_tag();
254         output.append_newline();
255         output.terminate_string();
256 // data is now in *text
257 }
258
259 void SwapMain::read_data(KeyFrame *keyframe)
260 {
261         FileXML input;
262
263         input.set_shared_input(keyframe->xbuf);
264
265         int result = 0;
266
267         while(!result)
268         {
269                 result = input.read_tag();
270
271                 if(!result)
272                 {
273                         if(input.tag.title_is("SWAPCHANNELS"))
274                         {
275                                 config.red = input.tag.get_property("RED", config.red);
276                                 config.green = input.tag.get_property("GREEN", config.green);
277                                 config.blue = input.tag.get_property("BLUE", config.blue);
278                                 config.alpha = input.tag.get_property("ALPHA", config.alpha);
279                         }
280                 }
281         }
282 }
283
284 void SwapMain::update_gui()
285 {
286         if(thread)
287         {
288                 load_configuration();
289                 thread->window->lock_window();
290                 ((SwapWindow*)thread->window)->red->set_text(output_to_text(config.red));
291                 ((SwapWindow*)thread->window)->green->set_text(output_to_text(config.green));
292                 ((SwapWindow*)thread->window)->blue->set_text(output_to_text(config.blue));
293                 ((SwapWindow*)thread->window)->alpha->set_text(output_to_text(config.alpha));
294                 thread->window->unlock_window();
295         }
296 }
297
298
299 int SwapMain::load_configuration()
300 {
301         KeyFrame *prev_keyframe;
302         prev_keyframe = get_prev_keyframe(get_source_position());
303
304         read_data(prev_keyframe);
305         return 1;
306 }
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331 #define MAXMINSRC(src, min, max) \
332         (src == MAX_SRC ? max : min)
333
334 #define SWAP_CHANNELS(type, min, max, components) \
335 { \
336         int h = frame->get_h(); \
337         int w = frame->get_w(); \
338         int red = config.red; \
339         int green = config.green; \
340         int blue = config.blue; \
341         int alpha = config.alpha; \
342  \
343         if(components == 3) \
344         { \
345                 if(red == ALPHA_SRC) red = MAX_SRC; \
346                 if(green == ALPHA_SRC) green = MAX_SRC; \
347                 if(blue == ALPHA_SRC) blue = MAX_SRC; \
348         } \
349  \
350  \
351         for(int i = 0; i < h; i++) \
352         { \
353                 type *inrow = (type*)frame->get_rows()[i]; \
354                 type *outrow = (type*)temp->get_rows()[i]; \
355  \
356                 for(int j = 0; j < w; j++) \
357                 { \
358                         if(red < 4) \
359                                 *outrow++ = *(inrow + red); \
360                         else \
361                                 *outrow++ = MAXMINSRC(red, 0, max); \
362  \
363                         if(green < 4) \
364                                 *outrow++ = *(inrow + green); \
365                         else \
366                                 *outrow++ = MAXMINSRC(green, min, max); \
367  \
368                         if(blue < 4) \
369                                 *outrow++ = *(inrow + blue); \
370                         else \
371                                 *outrow++ = MAXMINSRC(blue, min, max); \
372  \
373                         if(components == 4) \
374                         { \
375                                 if(alpha < 4) \
376                                         *outrow++ = *(inrow + alpha); \
377                                 else \
378                                         *outrow++ = MAXMINSRC(alpha, 0, max); \
379                         } \
380  \
381                         inrow += components; \
382                 } \
383         } \
384  \
385         frame->copy_from(temp); \
386 }
387
388
389
390 int SwapMain::process_buffer(VFrame *frame,
391         int64_t start_position,
392         double frame_rate)
393 {
394         load_configuration();
395
396         read_frame(frame,
397                 0,
398                 start_position,
399                 frame_rate,
400                 get_use_opengl());
401
402
403 // Use hardware
404         if(get_use_opengl())
405         {
406                 run_opengl();
407                 return 0;
408         }
409
410
411         temp = new_temp(frame->get_w(),
412                 frame->get_h(),
413                 frame->get_color_model());
414
415         switch(frame->get_color_model())
416         {
417                 case BC_RGB_FLOAT:
418                         SWAP_CHANNELS(float, 0, 1, 3);
419                         break;
420                 case BC_RGBA_FLOAT:
421                         SWAP_CHANNELS(float, 0, 1, 4);
422                         break;
423                 case BC_RGB888:
424                         SWAP_CHANNELS(unsigned char, 0, 0xff, 3);
425                         break;
426                 case BC_YUV888:
427                         SWAP_CHANNELS(unsigned char, 0x80, 0xff, 3);
428                         break;
429                 case BC_RGBA8888:
430                         SWAP_CHANNELS(unsigned char, 0, 0xff, 4);
431                         break;
432                 case BC_YUVA8888:
433                         SWAP_CHANNELS(unsigned char, 0x80, 0xff, 4);
434                         break;
435         }
436
437
438         return 0;
439 }
440
441
442 const char* SwapMain::output_to_text(int value)
443 {
444         switch(value)
445         {
446                 case RED_SRC:
447                         return _("Red");
448                         break;
449                 case GREEN_SRC:
450                         return _("Green");
451                         break;
452                 case BLUE_SRC:
453                         return _("Blue");
454                         break;
455                 case ALPHA_SRC:
456                         return _("Alpha");
457                         break;
458                 case NO_SRC:
459                         return "0%";
460                         break;
461                 case MAX_SRC:
462                         return "100%";
463                         break;
464                 default:
465                         return "";
466                         break;
467         }
468 }
469
470 int SwapMain::text_to_output(const char *text)
471 {
472         if(!strcmp(text, _("Red"))) return RED_SRC;
473         if(!strcmp(text, _("Green"))) return GREEN_SRC;
474         if(!strcmp(text, _("Blue"))) return BLUE_SRC;
475         if(!strcmp(text, _("Alpha"))) return ALPHA_SRC;
476         if(!strcmp(text, "0%")) return NO_SRC;
477         if(!strcmp(text, "100%")) return MAX_SRC;
478         return 0;
479 }
480
481 int SwapMain::handle_opengl()
482 {
483 #ifdef HAVE_GL
484
485         char output_frag[BCTEXTLEN];
486         sprintf(output_frag,
487                 "uniform sampler2D tex;\n"
488                 "uniform float chroma_offset;\n"
489                 "void main()\n"
490                 "{\n"
491                 "       vec4 in_color = texture2D(tex, gl_TexCoord[0].st);\n"
492                 "       vec4 out_color;\n");
493
494 #define COLOR_SWITCH(config, variable) \
495         strcat(output_frag, "   out_color." variable " = "); \
496         switch(config) \
497         { \
498                 case RED_SRC: strcat(output_frag, "in_color.r;\n"); break; \
499                 case GREEN_SRC: strcat(output_frag, "in_color.g;\n"); break; \
500                 case BLUE_SRC: strcat(output_frag, "in_color.b;\n"); break; \
501                 case ALPHA_SRC: strcat(output_frag, "in_color.a;\n"); break; \
502                 case NO_SRC: strcat(output_frag, "chroma_offset;\n"); break; \
503                 case MAX_SRC: strcat(output_frag, "1.0;\n"); break; \
504         }
505
506
507         COLOR_SWITCH(config.red, "r");
508         COLOR_SWITCH(config.green, "g");
509         COLOR_SWITCH(config.blue, "b");
510         COLOR_SWITCH(config.alpha, "a");
511
512         strcat(output_frag,
513                 "       gl_FragColor = out_color;\n"
514                 "}\n");
515
516         get_output()->to_texture();
517         get_output()->enable_opengl();
518         get_output()->init_screen();
519         get_output()->clear_pbuffer();
520         get_output()->bind_texture(0);
521
522         unsigned int shader = VFrame::make_shader(0, output_frag, 0);
523         if( shader > 0 ) {
524                 glUseProgram(shader);
525                 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
526                 glUniform1f(glGetUniformLocation(shader, "chroma_offset"),
527                         BC_CModels::is_yuv(get_output()->get_color_model()) ? 0.5 : 0.0);
528         }
529
530         get_output()->draw_texture();
531         glUseProgram(0);
532         get_output()->set_opengl_state(VFrame::SCREEN);
533 #endif
534         return 0;
535 }
536
537
538