r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / plugins / despike / despike.C
blobc8895e28189a0c0bf305b715d6c8fa66bf1ceffd
1 #include "clip.h"
2 #include "confirmsave.h"
3 #include "defaults.h"
4 #include "errorbox.h"
5 #include "filexml.h"
6 #include "picon_png.h"
7 #include "despike.h"
8 #include "despikewindow.h"
10 #include "vframe.h"
12 #include <string.h>
14 #include <libintl.h>
15 #define _(String) gettext(String)
16 #define gettext_noop(String) String
17 #define N_(String) gettext_noop (String)
20 REGISTER_PLUGIN(Despike)
24 Despike::Despike(PluginServer *server)
25  : PluginAClient(server)
27         PLUGIN_CONSTRUCTOR_MACRO
28         last_sample = 0;
31 Despike::~Despike()
33         PLUGIN_DESTRUCTOR_MACRO
36 char* Despike::plugin_title() { return _("Despike"); }
37 int Despike::is_realtime() { return 1; }
39 NEW_PICON_MACRO(Despike)
42 SHOW_GUI_MACRO(Despike, DespikeThread)
43 SET_STRING_MACRO(Despike)
44 RAISE_WINDOW_MACRO(Despike)
46 LOAD_CONFIGURATION_MACRO(Despike, DespikeConfig)
49 int Despike::process_realtime(int64_t size, double *input_ptr, double *output_ptr)
51         load_configuration();
53         double threshold = db.fromdb(config.level);
54         double change = db.fromdb(config.slope);
56 //printf("Despike::process_realtime 1\n");
57         for(int64_t i = 0; i < size; i++)
58         {
59                 if(fabs(input_ptr[i]) > threshold || 
60                         fabs(input_ptr[i]) - fabs(last_sample) > change) 
61                 {
62                         output_ptr[i] = last_sample;
63                 }
64                 else
65                 {
66                         output_ptr[i] = input_ptr[i];
67                         last_sample = input_ptr[i];
68                 }
69         }
70 //printf("Despike::process_realtime 2\n");
72         return 0;
76 int Despike::load_defaults()
78         char directory[1024];
80 // set the default directory
81         sprintf(directory, "%sdespike.rc", get_defaultdir());
82         
83 // load the defaults
85         defaults = new Defaults(directory);
87         defaults->load();
89         config.level = defaults->get("LEVEL", (double)0);
90         config.slope = defaults->get("SLOPE", (double)0);
92         return 0;
95 int Despike::save_defaults()
97         defaults->update("LEVEL", config.level);
98         defaults->update("SLOPE", config.slope);
99         defaults->save();
100         return 0;
104 void Despike::save_data(KeyFrame *keyframe)
106         FileXML output;
108 // cause xml file to store data directly in text
109         output.set_shared_string(keyframe->data, MESSAGESIZE);
111         output.tag.set_title("DESPIKE");
112         output.tag.set_property("LEVEL", config.level);
113         output.tag.set_property("SLOPE", config.slope);
114         output.append_tag();
115         output.append_newline();
116         output.terminate_string();
119 void Despike::read_data(KeyFrame *keyframe)
121         FileXML input;
122 // cause xml file to read directly from text
123         input.set_shared_string(keyframe->data, strlen(keyframe->data));
124         int result = 0;
126         result = input.read_tag();
128         if(!result)
129         {
130                 if(input.tag.title_is("DESPIKE"))
131                 {
132                         config.level = input.tag.get_property("LEVEL", config.level);
133                         config.slope = input.tag.get_property("SLOPE", config.slope);
134                 }
135         }
138 void Despike::update_gui()
140         if(thread)
141         {
142                 load_configuration();
143                 thread->window->lock_window();
144                 thread->window->level->update(config.level);
145                 thread->window->slope->update(config.slope);
146                 thread->window->unlock_window();
147         }
165 DespikeConfig::DespikeConfig()
167         level = 0;
168         slope = 0;
171 int DespikeConfig::equivalent(DespikeConfig &that)
173         return EQUIV(level, that.level) && 
174                 EQUIV(slope, that.slope);
177 void DespikeConfig::copy_from(DespikeConfig &that)
179         level = that.level;
180         slope = that.slope;
183 void DespikeConfig::interpolate(DespikeConfig &prev, 
184                 DespikeConfig &next, 
185                 int64_t prev_frame, 
186                 int64_t next_frame, 
187                 int64_t current_frame)
189         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
190         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
192         this->level = prev.level * prev_scale + next.level * next_scale;
193         this->slope = prev.slope * prev_scale + next.slope * next_scale;