r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / plugins / rgb601 / rgb601.C
blob789607f8d3e412f6f10be0bbf2ad62e702baf459
1 #include "clip.h"
2 #include "colormodels.h"
3 #include "filexml.h"
4 #include "picon_png.h"
5 #include "rgb601.h"
6 #include "rgb601window.h"
8 #include <stdio.h>
9 #include <string.h>
11 #include <libintl.h>
12 #define _(String) gettext(String)
13 #define gettext_noop(String) String
14 #define N_(String) gettext_noop (String)
16 REGISTER_PLUGIN(RGB601Main)
21 RGB601Config::RGB601Config()
23         direction = 0;
26 RGB601Main::RGB601Main(PluginServer *server)
27  : PluginVClient(server)
29         PLUGIN_CONSTRUCTOR_MACRO
32 RGB601Main::~RGB601Main()
34         PLUGIN_DESTRUCTOR_MACRO
37 char* RGB601Main::plugin_title() { return _("RGB - 601"); }
38 int RGB601Main::is_realtime() { return 1; }
41 SHOW_GUI_MACRO(RGB601Main, RGB601Thread)
43 SET_STRING_MACRO(RGB601Main)
45 RAISE_WINDOW_MACRO(RGB601Main)
47 NEW_PICON_MACRO(RGB601Main)
49 void RGB601Main::update_gui()
51         if(thread)
52         {
53                 load_configuration();
54                 thread->window->lock_window();
55                 thread->window->forward->update(config.direction == 1);
56                 thread->window->reverse->update(config.direction == 2);
57                 thread->window->unlock_window();
58         }
61 int RGB601Main::load_defaults()
63         char directory[1024], string[1024];
64 // set the default directory
65         sprintf(directory, "%srgb601.rc", BCASTDIR);
67 // load the defaults
68         defaults = new Defaults(directory);
69         defaults->load();
71         config.direction = defaults->get("DIRECTION", config.direction);
72         return 0;
75 int RGB601Main::save_defaults()
77         defaults->update("DIRECTION", config.direction);
78         defaults->save();
79         return 0;
82 void RGB601Main::load_configuration()
84         KeyFrame *prev_keyframe;
86         prev_keyframe = get_prev_keyframe(get_source_position());
87 // Must also switch between interpolation between keyframes and using first keyframe
88         read_data(prev_keyframe);
92 void RGB601Main::save_data(KeyFrame *keyframe)
94         FileXML output;
96 // cause data to be stored directly in text
97         output.set_shared_string(keyframe->data, MESSAGESIZE);
98         output.tag.set_title("RGB601");
99         output.tag.set_property("DIRECTION", config.direction);
100         output.append_tag();
101         output.terminate_string();
104 void RGB601Main::read_data(KeyFrame *keyframe)
106         FileXML input;
108         input.set_shared_string(keyframe->data, strlen(keyframe->data));
110         int result = 0;
111         float new_threshold;
113         while(!result)
114         {
115                 result = input.read_tag();
117                 if(!result)
118                 {
119                         if(input.tag.title_is("RGB601"))
120                         {
121                                 config.direction = input.tag.get_property("DIRECTION", config.direction);
122                         }
123                 }
124         }
126         if(thread) 
127         {
128                 thread->window->update();
129         }
133 #define CREATE_TABLE(max) \
134 { \
135         for(int i = 0; i < max; i++) \
136         { \
137                 forward_table[i] = (int)((double)0.8588 * i + max * 0.0627 + 0.5); \
138                 reverse_table[i] = (int)((double)1.1644 * i - max * 0.0627 + 0.5); \
139                 CLAMP(forward_table[i], 0, max); \
140                 CLAMP(reverse_table[i], 0, max); \
141         } \
144 void RGB601Main::create_table(VFrame *input_ptr)
146         switch(input_ptr->get_color_model())
147         {
148                 case BC_RGB888:
149                 case BC_YUV888:
150                 case BC_RGBA8888:
151                 case BC_YUVA8888:
152                         CREATE_TABLE(0xff);
153                         break;
155                 case BC_RGB161616:
156                 case BC_YUV161616:
157                 case BC_RGBA16161616:
158                 case BC_YUVA16161616:
159                         CREATE_TABLE(0xffff);
160                         break;
161         }
164 #define PROCESS(table, type, components, yuv) \
165 { \
166         int bytes = w * components; \
167         for(int i = 0; i < h; i++) \
168         { \
169                 type *in_row = (type*)input_ptr->get_rows()[i]; \
170                 type *out_row = (type*)output_ptr->get_rows()[i]; \
172                 if(yuv) \
173                 { \
174 /* Just do Y */ \
175                         for(int j = 0; j < w; j++) \
176                         { \
177                                 out_row[j * components] = table[in_row[j * components]]; \
178                                 out_row[j * components + 1] = in_row[j * components + 1]; \
179                                 out_row[j * components + 2] = in_row[j * components + 2]; \
180                                 if(components == 4) out_row[j * components + 3] = in_row[j * components + 3]; \
181                         } \
182                 } \
183                 else \
184                 { \
185                         for(int j = 0; j < bytes; j++) \
186                         { \
187                                 out_row[j * components] = in_row[j * components]; \
188                                 out_row[j * components + 1] = table[in_row[j * components + 1]]; \
189                                 out_row[j * components + 2] = table[in_row[j * components + 2]]; \
190                                 if(components == 4) out_row[j * components + 3] = table[in_row[j * components + 3]]; \
191                         } \
192                 } \
193         } \
196 void RGB601Main::process(int *table, VFrame *input_ptr, VFrame *output_ptr)
198         int w = input_ptr->get_w();
199         int h = input_ptr->get_h();
200         
201         if(config.direction == 1)
202                 switch(input_ptr->get_color_model())
203                 {
204                         case BC_YUV888:
205                                 PROCESS(forward_table, unsigned char, 3, 1);
206                                 break;
207                         case BC_YUVA8888:
208                                 PROCESS(forward_table, unsigned char, 4, 1);
209                                 break;
210                         case BC_YUV161616:
211                                 PROCESS(forward_table, u_int16_t, 3, 1);
212                                 break;
213                         case BC_YUVA16161616:
214                                 PROCESS(forward_table, u_int16_t, 4, 1);
215                                 break;
216                         case BC_RGB888:
217                                 PROCESS(forward_table, unsigned char, 3, 0);
218                                 break;
219                         case BC_RGBA8888:
220                                 PROCESS(forward_table, unsigned char, 4, 0);
221                                 break;
222                         case BC_RGB161616:
223                                 PROCESS(forward_table, u_int16_t, 3, 0);
224                                 break;
225                         case BC_RGBA16161616:
226                                 PROCESS(forward_table, u_int16_t, 4, 0);
227                                 break;
228                 }
229         else
230         if(config.direction == 2)
231                 switch(input_ptr->get_color_model())
232                 {
233                         case BC_YUV888:
234                                 PROCESS(reverse_table, unsigned char, 3, 1);
235                                 break;
236                         case BC_YUVA8888:
237                                 PROCESS(reverse_table, unsigned char, 4, 1);
238                                 break;
239                         case BC_YUV161616:
240                                 PROCESS(reverse_table, u_int16_t, 3, 1);
241                                 break;
242                         case BC_YUVA16161616:
243                                 PROCESS(reverse_table, u_int16_t, 4, 1);
244                                 break;
245                         case BC_RGB888:
246                                 PROCESS(reverse_table, unsigned char, 3, 0);
247                                 break;
248                         case BC_RGBA8888:
249                                 PROCESS(reverse_table, unsigned char, 4, 0);
250                                 break;
251                         case BC_RGB161616:
252                                 PROCESS(reverse_table, u_int16_t, 3, 0);
253                                 break;
254                         case BC_RGBA16161616:
255                                 PROCESS(reverse_table, u_int16_t, 4, 0);
256                                 break;
257                 }
258         else
259         if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
260                 output_ptr->copy_from(input_ptr);
263 int RGB601Main::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
265         load_configuration();
267         create_table(input_ptr);
268         if(config.direction == 1)
269                 process(forward_table, input_ptr, output_ptr);
270         else
271         if(config.direction == 2)
272                 process(reverse_table, input_ptr, output_ptr);
273         else
274         if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
275                 output_ptr->copy_from(input_ptr);
277         return 0;