Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / plugins / histogram / histogramconfig.C
blob34f650b78a9632fe1ecf07da066cc6513f418b6a
1 #include "clip.h"
2 #include "histogramconfig.h"
3 #include "units.h"
5 #include <math.h>
11 HistogramPoint::HistogramPoint()
12  : ListItem<HistogramPoint>()
16 HistogramPoint::~HistogramPoint()
20 int HistogramPoint::equivalent(HistogramPoint *src)
22         return EQUIV(x, src->x) && EQUIV(y, src->y);
28 HistogramPoints::HistogramPoints()
29  : List<HistogramPoint>()
33 HistogramPoints::~HistogramPoints()
37 HistogramPoint* HistogramPoints::insert(float x, float y)
39         HistogramPoint *current = first;
41 // Get existing point after new point
42         while(current)
43         {
44                 if(current->x > x)
45                         break;
46                 else
47                         current = NEXT;
48         }
50 // Insert new point before current point
51         HistogramPoint *new_point = new HistogramPoint;
52         if(current)
53         {
54                 insert_before(current, new_point);
55         }
56         else
57 // Append new point to list
58         {
59                 append(new_point);
60         }
62         new_point->x = x;
63         new_point->y = y;
66         return new_point;
69 void HistogramPoints::boundaries()
71         HistogramPoint *current = first;
72         while(current)
73         {
74                 CLAMP(current->x, 0.0, 1.0);
75                 CLAMP(current->y, 0.0, 1.0);
76                 current = NEXT;
77         }
80 int HistogramPoints::equivalent(HistogramPoints *src)
82         HistogramPoint *current_this = first;
83         HistogramPoint *current_src = src->first;
84         while(current_this && current_src)
85         {
86                 if(!current_this->equivalent(current_src)) return 0;
87                 current_this = current_this->next;
88                 current_src = current_src->next;
89         }
91         if(!current_this && current_src ||
92                 current_this && !current_src)
93                 return 0;
94         return 1;
97 void HistogramPoints::copy_from(HistogramPoints *src)
99         while(last)
100                 delete last;
101         HistogramPoint *current = src->first;
102         while(current)
103         {
104                 HistogramPoint *new_point = new HistogramPoint;
105                 new_point->x = current->x;
106                 new_point->y = current->y;
107                 append(new_point);
108                 current = NEXT;
109         }
112 void HistogramPoints::interpolate(HistogramPoints *prev, 
113         HistogramPoints *next,
114         double prev_scale,
115         double next_scale)
117         HistogramPoint *current = first;
118         HistogramPoint *current_prev = prev->first;
119         HistogramPoint *current_next = next->first;
121         while(current && current_prev && current_next)
122         {
123                 current->x = current_prev->x * prev_scale +
124                         current_next->x * next_scale;
125                 current->y = current_prev->y * prev_scale +
126                         current_next->y * next_scale;
127                 current = NEXT;
128                 current_prev = current_prev->next;
129                 current_next = current_next->next;
130         }
146 HistogramConfig::HistogramConfig()
148         plot = 1;
149         split = 0;
150         reset(1);
153 void HistogramConfig::reset(int do_mode)
155         reset_points(0);
157         
158         for(int i = 0; i < HISTOGRAM_MODES; i++)
159         {
160                 output_min[i] = 0.0;
161                 output_max[i] = 1.0;
162         }
164         if(do_mode) 
165         {
166                 automatic = 0;
167                 threshold = 0.1;
168         }
171 void HistogramConfig::reset_points(int colors_only)
173         for(int i = 0; i < HISTOGRAM_MODES; i++)
174         {
175                 if(i != HISTOGRAM_VALUE || !colors_only)
176                         while(points[i].last) delete points[i].last;
177         }
181 void HistogramConfig::boundaries()
183         for(int i = 0; i < HISTOGRAM_MODES; i++)
184         {
185                 points[i].boundaries();
186                 CLAMP(output_min[i], MIN_INPUT, MAX_INPUT);
187                 CLAMP(output_max[i], MIN_INPUT, MAX_INPUT);
188                 output_min[i] = Units::quantize(output_min[i], PRECISION);
189                 output_max[i] = Units::quantize(output_max[i], PRECISION);
190         }
191         CLAMP(threshold, 0, 1);
194 int HistogramConfig::equivalent(HistogramConfig &that)
196         for(int i = 0; i < HISTOGRAM_MODES; i++)
197         {
198                 if(!points[i].equivalent(&that.points[i]) ||
199                         !EQUIV(output_min[i], that.output_min[i]) ||
200                         !EQUIV(output_max[i], that.output_max[i])) return 0;
201         }
203         if(automatic != that.automatic ||
204                 !EQUIV(threshold, that.threshold)) return 0;
206         if(plot != that.plot ||
207                 split != that.split) return 0;
208         return 1;
211 void HistogramConfig::copy_from(HistogramConfig &that)
213         for(int i = 0; i < HISTOGRAM_MODES; i++)
214         {
215                 points[i].copy_from(&that.points[i]);
216                 output_min[i] = that.output_min[i];
217                 output_max[i] = that.output_max[i];
218         }
220         automatic = that.automatic;
221         threshold = that.threshold;
222         plot = that.plot;
223         split = that.split;
226 void HistogramConfig::interpolate(HistogramConfig &prev, 
227         HistogramConfig &next, 
228         int64_t prev_frame, 
229         int64_t next_frame, 
230         int64_t current_frame)
232         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
233         double prev_scale = 1.0 - next_scale;
235         for(int i = 0; i < HISTOGRAM_MODES; i++)
236         {
237                 points[i].interpolate(&prev.points[i], &next.points[i], prev_scale, next_scale);
238                 output_min[i] = prev.output_min[i] * prev_scale + next.output_min[i] * next_scale;
239                 output_max[i] = prev.output_max[i] * prev_scale + next.output_max[i] * next_scale;
240         }
242         threshold = prev.threshold * prev_scale + next.threshold * next_scale;
243         automatic = prev.automatic;
244         plot = prev.plot;
245         split = prev.split;
249 void HistogramConfig::dump()
251         for(int j = 0; j < HISTOGRAM_MODES; j++)
252         {
253                 printf("HistogramConfig::dump mode=%d plot=%d split=%d\n", j, plot, split);
254                 HistogramPoints *points = &this->points[j];
255                 HistogramPoint *current = points->first;
256                 while(current)
257                 {
258                         printf("%f,%f ", current->x, current->y);
259                         fflush(stdout);
260                         current = NEXT;
261                 }
262                 printf("\n");
263         }