2 * Cinelerra :: Blue Banana - color modification plugin for Cinelerra-CV
3 * Copyright (C) 2012-2013 Monty <monty@xiph.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "bluebanana.h"
26 #include "bluebananaconfig.h"
28 static float halfsin2(float x){
29 if(x<-M_PI/2)return 0;
34 static float scale_gamma (float hdel, float lo, float hi, float gamma){
40 /* this is contrived, so do something that looks good */
41 /* poor man's tangent */
42 hdel *= pow(.001,1/gamma)*1000;
44 hdel = pow(hdel,1/gamma);
47 // apply range scaling
48 return hdel*(hi-lo)+lo;
52 static int select_setup(char *pattern, int change){
55 float gsq = change*change+1.1;
60 while( x*x+y*y < gsq){
63 fprintf(stderr,"Internal error; pattern array overflow\n");
66 pattern[n++]=(change>0 ? 'H' : 'h');
71 fprintf(stderr,"Internal error; pattern array overflow\n");
74 pattern[n++]=(change>0 ? 'V' : 'v');
81 void BluebananaMain::update_lookups(int serverside){
82 // compare config contents with current lookup table state. Update
87 // Alpha lookup table for hue selection
88 if(lookup_cache.Hsel_lo != config.Hsel_lo ||
89 lookup_cache.Hsel_hi != config.Hsel_hi ||
90 lookup_cache.Hsel_over != config.Hsel_over){
92 float scale = M_PI/(config.Hsel_over+.0000001);
93 for(i=0; i<SELECT_LOOKUP_SIZE+2; i++){
94 /* this is a circular scale; the overlap wraps around to the opposite side */
95 float hue = (float)i/SELECT_LOOKUP_SIZE*360.;
96 float l0 = (hue-config.Hsel_lo-360.f)*scale;
97 float l1 = (hue-config.Hsel_lo)*scale;
98 float l2 = (hue-config.Hsel_lo+360.f)*scale;
99 float l3 = (hue-config.Hsel_lo+720.f)*scale;
100 float h0 = (hue-config.Hsel_hi-360.f)*scale;
101 float h1 = (hue-config.Hsel_hi)*scale;
102 float h2 = (hue-config.Hsel_hi+360.f)*scale;
103 float h3 = (hue-config.Hsel_hi+720.f)*scale;
105 float a0 = halfsin2(l0) * (1.-halfsin2(h0));
106 float a1 = halfsin2(l1) * (1.-halfsin2(h1));
107 float a2 = halfsin2(l2) * (1.-halfsin2(h2));
108 float a3 = halfsin2(l3) * (1.-halfsin2(h3));
110 hue_select_alpha_lookup[i]=a0+a1+a2+a3;
114 // Alpha lookup table for saturation selection
115 if(lookup_cache.Ssel_lo != config.Ssel_lo ||
116 lookup_cache.Ssel_hi != config.Ssel_hi ||
117 lookup_cache.Ssel_over != config.Ssel_over){
119 float scale = M_PI/(config.Ssel_over+.0000001);
120 for(i=0; i<SELECT_LOOKUP_SIZE+2; i++){
121 /* this is a bounded scale; the overlap reflects off the boundary */
122 float sat = (float)i/SELECT_LOOKUP_SIZE*100.;
123 float l0 = (-sat-config.Ssel_lo)*scale;
124 float h0 = (-sat-config.Ssel_hi)*scale;
125 float l1 = (sat-config.Ssel_lo)*scale;
126 float h1 = (sat-config.Ssel_hi)*scale;
127 float l2 = ((200.-sat)-config.Ssel_lo)*scale;
128 float h2 = ((200.-sat)-config.Ssel_hi)*scale;
129 sat_select_alpha_lookup[i] =
130 halfsin2(l0) * (1.-halfsin2(h0))+
131 halfsin2(l1) * (1.-halfsin2(h1))+
132 halfsin2(l2) * (1.-halfsin2(h2));
136 // Alpha lookup table for value selection
137 if(lookup_cache.Vsel_lo != config.Vsel_lo ||
138 lookup_cache.Vsel_hi != config.Vsel_hi ||
139 lookup_cache.Vsel_over != config.Vsel_over){
141 float scale = M_PI/(config.Vsel_over+.0000001);
142 for(i=0; i<SELECT_LOOKUP_SIZE+2; i++){
143 /* this is a bounded scale; the overlap reflects off the boundary */
144 float val = (float)i/SELECT_LOOKUP_SIZE*100.;
145 float l0 = (-val-config.Vsel_lo)*scale;
146 float h0 = (-val-config.Vsel_hi)*scale;
147 float l1 = (val-config.Vsel_lo)*scale;
148 float h1 = (val-config.Vsel_hi)*scale;
149 float l2 = ((200.-val)-config.Vsel_lo)*scale;
150 float h2 = ((200.-val)-config.Vsel_hi)*scale;
151 val_select_alpha_lookup[i] =
152 halfsin2(l0) * (1.-halfsin2(h0)) +
153 halfsin2(l1) * (1.-halfsin2(h1)) +
154 halfsin2(l2) * (1.-halfsin2(h2));
160 // lookup table for saturation adjustment
161 if(lookup_cache.Sadj_lo != config.Sadj_lo ||
162 lookup_cache.Sadj_hi != config.Sadj_hi ||
163 lookup_cache.Sadj_gamma != config.Sadj_gamma){
165 for(i=0; i<ADJUST_LOOKUP_SIZE+2; i++){
166 sat_adj_lookup[i] = scale_gamma ((float)i/ADJUST_LOOKUP_SIZE*MAX_ADJ_HEADROOM,
167 config.Sadj_lo,config.Sadj_hi,
170 sat_adj_toe_slope = -scale_gamma (-1,config.Sadj_lo,config.Sadj_hi,config.Sadj_gamma);
173 // lookup table for value adjustment
174 if(lookup_cache.Vadj_lo != config.Vadj_lo ||
175 lookup_cache.Vadj_hi != config.Vadj_hi ||
176 lookup_cache.Vadj_gamma != config.Vadj_gamma){
178 if(config.Vadj_gamma<1.0){
179 /* scaling mode for 'darkening' gamma*/
180 for(i=0; i<ADJUST_LOOKUP_SIZE+2; i++){
181 float V0 = (float)i/ADJUST_LOOKUP_SIZE*MAX_ADJ_HEADROOM;
182 float V1 = scale_gamma ((float)i/ADJUST_LOOKUP_SIZE*MAX_ADJ_HEADROOM,
183 0,config.Vadj_hi-config.Vadj_lo,
185 val_adj_lookup[i] = V0?V1/V0:0;
187 val_adj_toe_slope = -scale_gamma (-1,0,config.Vadj_hi-config.Vadj_lo,config.Vadj_gamma);
189 /* shifting mode for 'brightening' gamma */
190 for(i=0; i<ADJUST_LOOKUP_SIZE+2; i++){
191 float V0 = (float)i/ADJUST_LOOKUP_SIZE*MAX_ADJ_HEADROOM;
192 float V1 = scale_gamma ((float)i/ADJUST_LOOKUP_SIZE*MAX_ADJ_HEADROOM,
193 config.Vadj_lo,config.Vadj_lo+100,
195 val_adj_lookup[i] = V1-V0;
197 val_adj_toe_slope = -scale_gamma (-1,config.Vadj_lo,config.Vadj_lo+100.,config.Vadj_gamma);
201 // lookup table for red adjustment
202 if(lookup_cache.Radj_lo != config.Radj_lo ||
203 lookup_cache.Radj_hi != config.Radj_hi ||
204 lookup_cache.Radj_gamma != config.Radj_gamma){
206 for(i=0; i<ADJUST_LOOKUP_SIZE+2; i++)
207 red_adj_lookup[i] = scale_gamma ((float)i/ADJUST_LOOKUP_SIZE*MAX_ADJ_HEADROOM,
208 config.Radj_lo,config.Radj_hi,
210 red_adj_toe_slope = -scale_gamma (-1,config.Radj_lo,config.Radj_hi,config.Radj_gamma);
213 // lookup table for green adjustment
214 if(lookup_cache.Gadj_lo != config.Gadj_lo ||
215 lookup_cache.Gadj_hi != config.Gadj_hi ||
216 lookup_cache.Gadj_gamma != config.Gadj_gamma){
218 for(i=0; i<ADJUST_LOOKUP_SIZE+2; i++)
219 green_adj_lookup[i] = scale_gamma ((float)i/ADJUST_LOOKUP_SIZE*MAX_ADJ_HEADROOM,
220 config.Gadj_lo,config.Gadj_hi,
222 green_adj_toe_slope = -scale_gamma (-1,config.Gadj_lo,config.Gadj_hi,config.Gadj_gamma);
225 // lookup table for blue adjustment
226 if(lookup_cache.Badj_lo != config.Badj_lo ||
227 lookup_cache.Badj_hi != config.Badj_hi ||
228 lookup_cache.Badj_gamma != config.Badj_gamma){
230 for(i=0; i<ADJUST_LOOKUP_SIZE+2; i++)
231 blue_adj_lookup[i] = scale_gamma ((float)i/ADJUST_LOOKUP_SIZE*MAX_ADJ_HEADROOM,
232 config.Badj_lo,config.Badj_hi,
234 blue_adj_toe_slope = -scale_gamma (-1,config.Badj_lo,config.Badj_hi,config.Badj_gamma);
239 if(lookup_cache.Fsel_erode != config.Fsel_erode ||
240 lookup_cache.Fsel_lo != config.Fsel_lo ||
241 lookup_cache.Fsel_mid != config.Fsel_mid ||
242 lookup_cache.Fsel_hi != config.Fsel_hi){
248 if(config.Fsel_erode){
249 /* shrink-first mode */
250 select_one_n = select_setup(select_one,config.Fsel_lo);
252 select_two_n = select_setup(select_two,config.Fsel_hi-config.Fsel_lo);
253 select_three_n = select_setup(select_three,config.Fsel_mid-config.Fsel_hi);
255 select_two_n = select_setup(select_two,config.Fsel_mid-config.Fsel_lo);
258 /* grow-first mode */
259 select_one_n = select_setup(select_one,config.Fsel_hi);
261 select_two_n = select_setup(select_two,config.Fsel_lo-config.Fsel_hi);
262 select_three_n = select_setup(select_three,config.Fsel_mid-config.Fsel_lo);
264 select_two_n = select_setup(select_two,config.Fsel_mid-config.Fsel_hi);
269 lookup_cache.copy_from(config);
273 float BluebananaMain::hue_select_alpha(float hue){
274 if(config.Hsel_active){
275 float huewarp = hue*(SELECT_LOOKUP_SIZE*.002777778f);
276 float hueint = floor(huewarp);
277 float del = huewarp-hueint;
278 return hue_select_alpha_lookup[(int)hueint]*(1.f-del)+hue_select_alpha_lookup[(int)hueint+1]*del;
284 float BluebananaMain::sat_select_alpha(float sat){
285 if(config.Ssel_active){
286 float satwarp = sat*SELECT_LOOKUP_SIZE;
287 float satint = floor(satwarp);
288 float del = satwarp-satint;
289 return sat_select_alpha_lookup[(int)satint]*(1.f-del)+sat_select_alpha_lookup[(int)satint+1]*del;
295 float BluebananaMain::val_select_alpha(float val){
296 if(config.Vsel_active){
297 float valwarp = val*SELECT_LOOKUP_SIZE;
298 float valint = floor(valwarp);
299 float del = valwarp-valint;
300 return val_select_alpha_lookup[(int)valint]*(1.f-del)+val_select_alpha_lookup[(int)valint+1]*del;
305 // [0..1] HSV selection lookup must be sanitized as S and V values can
306 // exceed [0..1]. Clamping is OK, as out of range values will still
307 // be selected at range limits.
308 float sel_lookup(float val, float *lookup){
310 float del = modff(CLAMP(val,0.f,1.f)*SELECT_LOOKUP_SCALE,&bin);
311 return lookup[(int)bin]*(1.f-del)+lookup[(int)bin+1]*del;
314 // [0..4] == lookup range
315 // below 0, lookups are linear
317 float adj_lookup(float val, float *lookup, float toe_slope){
319 return val*toe_slope+lookup[0];
323 float del = modff(val*ADJUST_LOOKUP_SCALE,&bin);
324 return lookup[(int)bin]*(1.f-del)+lookup[(int)bin+1]*del;