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