fix boxblur missed header last checkin
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / rgbshift / rgbshift.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 "rgbshift.h"
23
24
25
26 REGISTER_PLUGIN(RGBShiftEffect)
27
28
29
30
31
32
33 RGBShiftConfig::RGBShiftConfig()
34 {
35         reset(RESET_ALL);
36 }
37
38 void RGBShiftConfig::reset(int clear)
39 {
40         switch(clear) {
41                 case RESET_R_DX : r_dx = 0;
42                         break;
43                 case RESET_R_DY : r_dy = 0;
44                         break;
45                 case RESET_G_DX : g_dx = 0;
46                         break;
47                 case RESET_G_DY : g_dy = 0;
48                         break;
49                 case RESET_B_DX : b_dx = 0;
50                         break;
51                 case RESET_B_DY : b_dy = 0;
52                         break;
53                 case RESET_ALL :
54                 default:
55                         r_dx = r_dy = 0;
56                         g_dx = g_dy = 0;
57                         b_dx = b_dy = 0;
58                         break;
59         }
60 }
61
62 void RGBShiftConfig::copy_from(RGBShiftConfig &src)
63 {
64         r_dx = src.r_dx;  r_dy = src.r_dy;
65         g_dx = src.g_dx;  g_dy = src.g_dy;
66         b_dx = src.b_dx;  b_dy = src.b_dy;
67 }
68
69 int RGBShiftConfig::equivalent(RGBShiftConfig &src)
70 {
71         return EQUIV(r_dx, src.r_dx) && EQUIV(g_dx, src.g_dx) && EQUIV(b_dx, src.b_dx) &&
72                 EQUIV(r_dy, src.r_dy) && EQUIV(g_dy, src.g_dy) && EQUIV(b_dy, src.b_dy);
73 }
74
75 void RGBShiftConfig::interpolate(RGBShiftConfig &prev,
76         RGBShiftConfig &next,
77         long prev_frame,
78         long next_frame,
79         long current_frame)
80 {
81         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
82         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
83
84         r_dx = prev.r_dx * prev_scale + next.r_dx * next_scale;
85         r_dy = prev.r_dy * prev_scale + next.r_dy * next_scale;
86         g_dx = prev.g_dx * prev_scale + next.g_dx * next_scale;
87         g_dy = prev.g_dy * prev_scale + next.g_dy * next_scale;
88         b_dx = prev.b_dx * prev_scale + next.b_dx * next_scale;
89         b_dy = prev.b_dy * prev_scale + next.b_dy * next_scale;
90 }
91
92
93
94
95
96
97 #define MAXVALUE 100
98
99 RGBShiftLevel::RGBShiftLevel(RGBShiftEffect *plugin, int *output, int x, int y)
100  : BC_ISlider(x, y, 0, xS(200), yS(200), -MAXVALUE, MAXVALUE, *output)
101 {
102         this->plugin = plugin;
103         this->output = output;
104 }
105
106 int RGBShiftLevel::handle_event()
107 {
108         *output = get_value();
109         plugin->send_configure_change();
110         return 1;
111 }
112
113
114 RGBShiftReset::RGBShiftReset(RGBShiftEffect *plugin, RGBShiftWindow *window, int x, int y)
115  : BC_GenericButton(x, y, _("Reset"))
116 {
117         this->plugin = plugin;
118         this->window = window;
119 }
120 RGBShiftReset::~RGBShiftReset()
121 {
122 }
123 int RGBShiftReset::handle_event()
124 {
125         plugin->config.reset(RESET_ALL);
126         window->update_gui(RESET_ALL);
127         plugin->send_configure_change();
128         return 1;
129 }
130
131
132 RGBShiftSliderClr::RGBShiftSliderClr(RGBShiftEffect *plugin, RGBShiftWindow *window, int x, int y, int w, int clear)
133  : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
134 {
135         this->plugin = plugin;
136         this->window = window;
137         this->clear = clear;
138 }
139 RGBShiftSliderClr::~RGBShiftSliderClr()
140 {
141 }
142 int RGBShiftSliderClr::handle_event()
143 {
144         // clear==1 ==> r_dx slider --- clear==2 ==> r_dy slider
145         // clear==3 ==> g_dx slider --- clear==4 ==> g_dy slider
146         // clear==5 ==> b_dx slider --- clear==6 ==> b_dy slider
147         plugin->config.reset(clear);
148         window->update_gui(clear);
149         plugin->send_configure_change();
150         return 1;
151 }
152
153
154 RGBShiftWindow::RGBShiftWindow(RGBShiftEffect *plugin)
155  : PluginClientWindow(plugin, xS(320), yS(230), xS(320), yS(230), 0)
156 {
157         this->plugin = plugin;
158 }
159
160 void RGBShiftWindow::create_objects()
161 {
162         int xs10 = xS(10), xs50 = xS(50);
163         int ys10 = yS(10), ys30 = yS(30), ys40 = yS(40);
164         int x = xs10, y = ys10, x1 = xs50;
165         int x2 = 0; int clrBtn_w = xs50;
166
167         add_subwindow(new BC_Title(x, y, _("R_dx:")));
168         add_subwindow(r_dx = new RGBShiftLevel(plugin, &plugin->config.r_dx, x1, y));
169         x2 = x1 + r_dx->get_w() + xs10;
170         add_subwindow(r_dxClr = new RGBShiftSliderClr(plugin, this, x2, y, clrBtn_w, RESET_R_DX));
171
172         y += ys30;
173         add_subwindow(new BC_Title(x, y, _("R_dy:")));
174         add_subwindow(r_dy = new RGBShiftLevel(plugin, &plugin->config.r_dy, x1, y));
175         add_subwindow(r_dyClr = new RGBShiftSliderClr(plugin, this, x2, y, clrBtn_w, RESET_R_DY));
176
177         y += ys30;
178         add_subwindow(new BC_Title(x, y, _("G_dx:")));
179         add_subwindow(g_dx = new RGBShiftLevel(plugin, &plugin->config.g_dx, x1, y));
180         add_subwindow(g_dxClr = new RGBShiftSliderClr(plugin, this, x2, y, clrBtn_w, RESET_G_DX));
181
182         y += ys30;
183         add_subwindow(new BC_Title(x, y, _("G_dy:")));
184         add_subwindow(g_dy = new RGBShiftLevel(plugin, &plugin->config.g_dy, x1, y));
185         add_subwindow(g_dyClr = new RGBShiftSliderClr(plugin, this, x2, y, clrBtn_w, RESET_G_DY));
186
187         y += ys30;
188         add_subwindow(new BC_Title(x, y, _("B_dx:")));
189         add_subwindow(b_dx = new RGBShiftLevel(plugin, &plugin->config.b_dx, x1, y));
190         add_subwindow(b_dxClr = new RGBShiftSliderClr(plugin, this, x2, y, clrBtn_w, RESET_B_DX));
191
192         y += ys30;
193         add_subwindow(new BC_Title(x, y, _("B_dy:")));
194         add_subwindow(b_dy = new RGBShiftLevel(plugin, &plugin->config.b_dy, x1, y));
195         add_subwindow(b_dyClr = new RGBShiftSliderClr(plugin, this, x2, y, clrBtn_w, RESET_B_DY));
196
197         y += ys40;
198         add_subwindow(reset = new RGBShiftReset(plugin, this, x, y));
199
200         show_window();
201         flush();
202 }
203
204
205 // for Reset button
206 void RGBShiftWindow::update_gui(int clear)
207 {
208         switch(clear) {
209                 case RESET_R_DX : r_dx->update(plugin->config.r_dx);
210                         break;
211                 case RESET_R_DY : r_dy->update(plugin->config.r_dy);
212                         break;
213                 case RESET_G_DX : g_dx->update(plugin->config.g_dx);
214                         break;
215                 case RESET_G_DY : g_dy->update(plugin->config.g_dy);
216                         break;
217                 case RESET_B_DX : b_dx->update(plugin->config.b_dx);
218                         break;
219                 case RESET_B_DY : b_dy->update(plugin->config.b_dy);
220                         break;
221                 case RESET_ALL :
222                 default:
223                         r_dx->update(plugin->config.r_dx);
224                         r_dy->update(plugin->config.r_dy);
225                         g_dx->update(plugin->config.g_dx);
226                         g_dy->update(plugin->config.g_dy);
227                         b_dx->update(plugin->config.b_dx);
228                         b_dy->update(plugin->config.b_dy);
229                         break;
230         }
231 }
232
233
234
235
236
237
238 RGBShiftEffect::RGBShiftEffect(PluginServer *server)
239  : PluginVClient(server)
240 {
241         temp_frame = 0;
242 }
243 RGBShiftEffect::~RGBShiftEffect()
244 {
245         delete temp_frame;
246 }
247
248 const char* RGBShiftEffect::plugin_title() { return N_("RGBShift"); }
249 int RGBShiftEffect::is_realtime() { return 1; }
250
251
252 NEW_WINDOW_MACRO(RGBShiftEffect, RGBShiftWindow)
253 LOAD_CONFIGURATION_MACRO(RGBShiftEffect, RGBShiftConfig)
254
255 void RGBShiftEffect::update_gui()
256 {
257         if(thread)
258         {
259                 RGBShiftWindow *yuv_wdw = (RGBShiftWindow*)thread->window;
260                 yuv_wdw->lock_window("RGBShiftEffect::update_gui");
261                 load_configuration();
262                 yuv_wdw->r_dx->update(config.r_dx);
263                 yuv_wdw->r_dy->update(config.r_dy);
264                 yuv_wdw->g_dx->update(config.g_dx);
265                 yuv_wdw->g_dy->update(config.g_dy);
266                 yuv_wdw->b_dx->update(config.b_dx);
267                 yuv_wdw->b_dy->update(config.b_dy);
268                 yuv_wdw->unlock_window();
269         }
270 }
271
272 void RGBShiftEffect::save_data(KeyFrame *keyframe)
273 {
274         FileXML output;
275         output.set_shared_output(keyframe->xbuf);
276         output.tag.set_title("RGBSHIFT");
277         output.tag.set_property("R_DX", config.r_dx);
278         output.tag.set_property("R_DY", config.r_dy);
279         output.tag.set_property("G_DX", config.g_dx);
280         output.tag.set_property("G_DY", config.g_dy);
281         output.tag.set_property("B_DX", config.b_dx);
282         output.tag.set_property("B_DY", config.b_dy);
283         output.append_tag();
284         output.tag.set_title("/RGBSHIFT");
285         output.append_tag();
286         output.append_newline();
287         output.terminate_string();
288 }
289
290 void RGBShiftEffect::read_data(KeyFrame *keyframe)
291 {
292         FileXML input;
293         input.set_shared_input(keyframe->xbuf);
294         while(!input.read_tag())
295         {
296                 if(input.tag.title_is("RGBSHIFT"))
297                 {
298                         config.r_dx = input.tag.get_property("R_DX", config.r_dx);
299                         config.r_dy = input.tag.get_property("R_DY", config.r_dy);
300                         config.g_dx = input.tag.get_property("G_DX", config.g_dx);
301                         config.g_dy = input.tag.get_property("G_DY", config.g_dy);
302                         config.b_dx = input.tag.get_property("B_DX", config.b_dx);
303                         config.b_dy = input.tag.get_property("B_DY", config.b_dy);
304                 }
305         }
306 }
307
308 #define RGB_MACRO(type, temp_type, components) \
309 { \
310         for(int i = 0; i < h; i++) { \
311                 int ri = i + config.r_dy, gi = i + config.g_dy, bi = i + config.b_dy; \
312                 type *in_r = ri >= 0 && ri < h ? (type *)frame->get_rows()[ri] : 0; \
313                 type *in_g = gi >= 0 && gi < h ? (type *)frame->get_rows()[gi] : 0; \
314                 type *in_b = bi >= 0 && bi < h ? (type *)frame->get_rows()[bi] : 0; \
315                 type *out_row = (type *)output->get_rows()[i]; \
316                 for(int j = 0; j < w; j++) { \
317                         int rj = j + config.r_dx, gj = j + config.g_dx, bj = j + config.b_dx; \
318                         type *rp = in_r && rj >= 0 && rj < w ? in_r + rj*components: 0; \
319                         type *gp = in_g && gj >= 0 && gj < w ? in_g + gj*components: 0; \
320                         type *bp = in_b && bj >= 0 && bj < w ? in_b + bj*components: 0; \
321                         out_row[0] = rp ? rp[0] : 0; \
322                         out_row[1] = gp ? gp[1] : 0; \
323                         out_row[2] = bp ? bp[2] : 0; \
324                         out_row += components; \
325                 } \
326         } \
327 }
328
329 #define YUV_MACRO(type, temp_type, components) \
330 { \
331         for(int i = 0; i < h; i++) { \
332                 int ri = i + config.r_dy, gi = i + config.g_dy, bi = i + config.b_dy; \
333                 uint8_t *in_r = ri >= 0 && ri < h ? (uint8_t *)frame->get_rows()[ri] : 0; \
334                 uint8_t *in_g = gi >= 0 && gi < h ? (uint8_t *)frame->get_rows()[gi] : 0; \
335                 uint8_t *in_b = bi >= 0 && bi < h ? (uint8_t *)frame->get_rows()[bi] : 0; \
336                 type *out_row = (type *)output->get_rows()[i]; \
337                 for(int j = 0; j < w; j++) { \
338                         int rj = j + config.r_dx, gj = j + config.g_dx, bj = j + config.b_dx; \
339                         uint8_t *rp = in_r && rj >= 0 && rj < w ? in_r + rj*3: 0; \
340                         uint8_t *gp = in_g && gj >= 0 && gj < w ? in_g + gj*3: 0; \
341                         uint8_t *bp = in_b && bj >= 0 && bj < w ? in_b + bj*3: 0; \
342                         temp_type y, u, v; \
343                         temp_type r = rp ? rp[0] : 0; \
344                         temp_type g = gp ? gp[1] : 0; \
345                         temp_type b = bp ? bp[2] : 0; \
346                         if( sizeof(type) == 4 ) \
347                                 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v); \
348                         else if( sizeof(type) == 2 ) \
349                                 YUV::yuv.rgb_to_yuv_16(r, g, b, y, u, v); \
350                         else \
351                                 YUV::yuv.rgb_to_yuv_8(r, g, b, y, u, v); \
352                         out_row[0] = y; \
353                         out_row[1] = u; \
354                         out_row[2] = v; \
355                         out_row += components; \
356                 } \
357         } \
358 }
359
360 int RGBShiftEffect::process_realtime(VFrame *input, VFrame *output)
361 {
362         load_configuration();
363
364         if( EQUIV(config.r_dx, 0) && EQUIV(config.g_dx, 0) && EQUIV(config.b_dx, 0) &&
365                 EQUIV(config.r_dy, 0) && EQUIV(config.g_dy, 0) && EQUIV(config.b_dy, 0) ) {
366                 if(input->get_rows()[0] != output->get_rows()[0])
367                         output->copy_from(input);
368                 return 0;
369         }
370
371         int w = input->get_w(), h = input->get_h();
372         int color_model = input->get_color_model();
373         int is_yuv = BC_CModels::is_yuv(color_model);
374         if( is_yuv ) color_model = BC_RGB888;
375         VFrame *frame = input;
376         if( input->get_rows()[0] == output->get_rows()[0] || !is_yuv ) {
377                 if( temp_frame && ( temp_frame->get_color_model() != color_model ||
378                         temp_frame->get_w() != w || temp_frame->get_h() != h ) ) {
379                         delete temp_frame;  temp_frame = 0;
380                 }
381                 if( !temp_frame )
382                         temp_frame = new VFrame(w, h, color_model, 0);
383                 frame = temp_frame;
384                 if( color_model != input->get_color_model() )
385                         BC_CModels::transfer(frame->get_rows(), input->get_rows(),
386                                 frame->get_y(), frame->get_u(), frame->get_v(),
387                                 input->get_y(), input->get_u(), input->get_v(),
388                                 0, 0, input->get_w(), input->get_h(),
389                                 0, 0, frame->get_w(), frame->get_h(),
390                                 input->get_color_model(), frame->get_color_model(), 0,
391                                 input->get_bytes_per_line(), w);
392                 else
393                         frame->copy_from(input);
394         }
395
396         switch( input->get_color_model() ) {
397         case BC_YUV888:       YUV_MACRO(unsigned char, int, 3);  break;
398         case BC_YUV161616:    YUV_MACRO(uint16_t, int, 3);       break;
399         case BC_YUVA8888:     YUV_MACRO(unsigned char, int, 4);  break;
400         case BC_YUVA16161616: YUV_MACRO(uint16_t, int, 4);       break;
401         case BC_RGB_FLOAT:    RGB_MACRO(float, float, 3);        break;
402         case BC_RGB888:       RGB_MACRO(unsigned char, int, 3);  break;
403         case BC_RGB161616:    RGB_MACRO(uint16_t, int, 3);       break;
404         case BC_RGBA_FLOAT:   RGB_MACRO(float, float, 4);        break;
405         case BC_RGBA8888:     RGB_MACRO(unsigned char, int, 4);  break;
406         case BC_RGBA16161616: RGB_MACRO(uint16_t, int, 4);       break;
407         }
408
409         return 0;
410 }
411
412