no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / histogram / histogramconfig.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2011 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 "clip.h"
23 #include "histogramconfig.h"
24 #include "units.h"
25
26 #include <math.h>
27
28
29
30 HistogramConfig::HistogramConfig()
31 {
32         plot = 1;
33         split = 0;
34         sum_frames = 0;
35         reset(1);
36 }
37
38 void HistogramConfig::reset(int do_mode)
39 {
40         for(int i = 0; i < HISTOGRAM_MODES; i++)
41         {
42                 low_input[i] = 0.0;
43                 high_input[i] = 1.0;
44                 low_output[i] = 0.0;
45                 high_output[i] = 1.0;
46                 gamma[i] = 1.0;
47         }
48
49         if(do_mode)
50         {
51                 automatic = 0;
52                 threshold = 0.97;
53         }
54
55         log_slider = .5;
56 }
57
58 void HistogramConfig::reset_points(int colors_only)
59 {
60         for(int i = 0; i < HISTOGRAM_MODES; i++)
61         {
62                 if(i != HISTOGRAM_VALUE || !colors_only)
63                 {
64                         low_input[i] = 0.0;
65                         high_input[i] = 1.0;
66                 }
67         }
68 }
69
70
71 void HistogramConfig::boundaries()
72 {
73         for(int i = 0; i < HISTOGRAM_MODES; i++)
74         {
75                 CLAMP(low_input[i], HIST_MIN_INPUT, HIST_MAX_INPUT);
76                 CLAMP(high_input[i], HIST_MIN_INPUT, HIST_MAX_INPUT);
77                 CLAMP(low_output[i], HIST_MIN_INPUT, HIST_MAX_INPUT);
78                 CLAMP(high_output[i], HIST_MIN_INPUT, HIST_MAX_INPUT);
79                 CLAMP(gamma[i], MIN_GAMMA, MAX_GAMMA);
80                 low_output[i] = Units::quantize(low_output[i], PRECISION);
81                 high_output[i] = Units::quantize(high_output[i], PRECISION);
82         }
83         CLAMP(threshold, 0, 1);
84         CLAMP(log_slider, 0, 1);
85 }
86
87 int HistogramConfig::equivalent(HistogramConfig &that)
88 {
89 // EQUIV isn't precise enough to detect changes in points
90         for(int i = 0; i < HISTOGRAM_MODES; i++) {
91 //              if(!EQUIV(low_input[i], that.low_input[i]) ||
92 //                      !EQUIV(high_input[i], that.high_input[i]) ||
93 //                      !EQUIV(gamma[i], that.gamma[i]) ||
94 //                      !EQUIV(low_output[i], that.low_output[i]) ||
95 //                      !EQUIV(high_output[i], that.high_output[i])) return 0;
96                 if( low_input[i] != that.low_input[i] ||
97                     high_input[i] != that.high_input[i] ||
98                     gamma[i] != that.gamma[i] ||
99                     low_output[i] != that.low_output[i] ||
100                     high_output[i] != that.high_output[i] ) return 0;
101         }
102
103         if( automatic != that.automatic ||
104             threshold != that.threshold ) return 0;
105
106         if( plot != that.plot ||
107             split != that.split ||
108             sum_frames != that.sum_frames ||
109             log_slider != that.log_slider ) return 0;
110
111         return 1;
112 }
113
114 void HistogramConfig::copy_from(HistogramConfig &that)
115 {
116         for(int i = 0; i < HISTOGRAM_MODES; i++)
117         {
118                 low_input[i] = that.low_input[i];
119                 high_input[i] = that.high_input[i];
120                 gamma[i] = that.gamma[i];
121                 low_output[i] = that.low_output[i];
122                 high_output[i] = that.high_output[i];
123         }
124
125         automatic = that.automatic;
126         threshold = that.threshold;
127         plot = that.plot;
128         split = that.split;
129         sum_frames = that.sum_frames;
130         log_slider = that.log_slider;
131 }
132
133 void HistogramConfig::interpolate(HistogramConfig &prev, HistogramConfig &next,
134                 int64_t prev_frame, int64_t next_frame, int64_t current_frame)
135 {
136         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
137         double prev_scale = 1.0 - next_scale;
138
139         for( int i=0; i<HISTOGRAM_MODES; ++i ) {
140                 low_input[i] = prev.low_input[i] * prev_scale + next.low_input[i] * next_scale;
141                 high_input[i] = prev.high_input[i] * prev_scale + next.high_input[i] * next_scale;
142                 gamma[i] = prev.gamma[i] * prev_scale + next.gamma[i] * next_scale;
143                 low_output[i] = prev.low_output[i] * prev_scale + next.low_output[i] * next_scale;
144                 high_output[i] = prev.high_output[i] * prev_scale + next.high_output[i] * next_scale;
145         }
146
147         threshold = prev.threshold * prev_scale + next.threshold * next_scale;
148         automatic = prev.automatic;
149         plot = prev.plot;
150         split = prev.split;
151         sum_frames = prev.sum_frames;
152         log_slider = prev.log_slider;
153 }
154
155
156 void HistogramConfig::dump()
157 {
158         for( int j=0; j<HISTOGRAM_MODES; ++j ) {
159                 printf("HistogramConfig::dump mode=%d plot=%d split=%d sum_frames=%d\n",
160                         j, plot, split, sum_frames);
161         }
162 }
163