summaryrefslogtreecommitdiffstats
path: root/src/filter/edgeglow/edgeglow.cpp
blob: f0c0eb651efc209f346cd21b89915b1b86a498a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* edgeglow.cpp
 * Copyright (C) 2008 Salsaman (salsaman@gmail.com)
 * This file is a Frei0r plugin.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "frei0r.hpp"
#include "frei0r_math.h"
#include <stdlib.h>

class edgeglow : public frei0r::filter
{
public:

  f0r_param_double lthresh;
  f0r_param_double lupscale;
  f0r_param_double lredscale;


  edgeglow(unsigned int width, unsigned int height)
  {
    lthresh = 0.0;
    lupscale = 0.0;
    lredscale = 0.0;
    register_param(lthresh, "lthresh", "threshold for edge lightening");
    register_param(lupscale, "lupscale", "multiplier for upscaling edge brightness");
    register_param(lredscale, "lredscale", "multiplier for downscaling non-edge brightness");
  }
  
  virtual void update()
  {
    std::copy(in, in + width*height, out);
    for (unsigned int y=1; y<height-1; ++y)
    {
      for (unsigned int x=1; x<width-1; ++x)
      {
        // XXX faut eviter le * width en y allant par rangee
        unsigned char *p1 = (unsigned char *)&in[(y-1)*width+(x-1)];
        unsigned char *p2 = (unsigned char *)&in[(y-1)*width+x];
        unsigned char *p3 = (unsigned char *)&in[(y-1)*width+(x+1)];
        unsigned char *p4 = (unsigned char *)&in[y*width+(x-1)];
        unsigned char *p6 = (unsigned char *)&in[y*width+(x+1)];
        unsigned char *p7 = (unsigned char *)&in[(y+1)*width+(x-1)];
        unsigned char *p8 = (unsigned char *)&in[(y+1)*width+x];
        unsigned char *p9 = (unsigned char *)&in[(y+1)*width+(x+1)];
        
        unsigned char *g = (unsigned char *)&out[y*width+x];
        
        for (int i=0; i<3; ++i)
          g[i] = CLAMP0255(
                           abs(p1[i] + p2[i]*2 + p3[i] - p7[i] - p8[i]*2 - p9[i]) +
                           abs(p3[i] + p6[i]*2 + p9[i] - p1[i] - p4[i]*2 - p7[i]) );


	unsigned char *p5 = (unsigned char *)&in[y*width+x];

        g[3] = p5[3]; // copy alpha

	float lt;

	// calculate "lightness"
	unsigned char R=g[0];
	unsigned char G=g[1];
	unsigned char B=g[2];

	unsigned char max=R;
	if (G>max) max=G;
	if (B>max) max=B;

	unsigned char min=R;
	if (G<min) min=G;
	if (B<min) min=B;

	unsigned char l=(unsigned char) (((float)max+(float)min)/2.);



	// convert in and out to HSL, add L value of out
	
	unsigned int h;
	float s;
	
	R=p5[0];
	G=p5[1];
	B=p5[2];
	
	max=R;
	if (G>max) max=G;
	if (B>max) max=B;
	
	min=R;
	if (G<min) min=G;
	if (B<min) min=B;
	

	if (l>(lt=lthresh*255.)) {
	  // if lightness > threshold, we add it to the lightness of the original
	  l=CLAMP0255((int32_t)((float)l*lupscale+((float)max+(float)min)/2.));
	}
	// otherwise reduce
	else if (lredscale>0.) {
	  l=((float)max+(float)min)/2.*(1.-lredscale);
	}

	if (lredscale>0.||l>lt) {
	  if (max==min) {
	    h=0;
	    s=0.;
	  }
	  else {
	    if (max==R) {
	      h=(unsigned int)(60.*((float)G-(float)B)/((float)max-(float)min));
	      if (G<B) h+=360;
	    }
	    else {
	      if (max==G) h=(unsigned int)(60.*((float)B-(float)R)/((float)max-(float)min)+120.);
	      else h=(unsigned int)(60.*(float)(R-G)/float(max-min)+240.);
	    }
	    
	    if (l<=0.5) s=((float)max-(float)min)/((float)max+(float)min);
	    else s=((float)max-(float)min)/(2.-((float)max+(float)min));
	  }
	  
	  
	  // convert back to RGB
	  
	  float q;
	  
	  if (l<0.5) q=l*(1.+s);
	  else q=l+s-(l*s);
	  
	  float p=2.*l-q;
	  
	  float hk=(float)h/360.;
	  
	  float tr=hk+1./3.;
	  float tg=hk;
	  float tb=hk-1./3.;
	  
	  if (tr>1.) tr-=1.;
	  if (tb<0.) tb+=1.;
	  
	  if (tr<1./6.) g[0]=CLAMP0255(p+((q-p)*6.*tr));
	  else if (tr<0.5) g[0]=CLAMP0255((int32_t)q);
	  else if (tr<1./6.) g[0]=CLAMP0255(p+((q-p)*6.*(2./3.-tr)));
	  else g[0]=CLAMP0255((int32_t)p);
	  
	  if (tg<1./6.) g[1]=CLAMP0255(p+((q-p)*6.*tg));
	  else if (tg<0.5) g[1]=CLAMP0255((int32_t)q);
	  else if (tg<1./6.) g[1]=CLAMP0255(p+((q-p)*6.*(2./3.-tg)));
	  else g[1]=CLAMP0255((int32_t)p);
	  
	  if (tb<1./6.) g[2]=CLAMP0255(p+((q-p)*6.*tb));
	  else if (tb<0.5) g[2]=CLAMP0255((int32_t)q);
	  else if (tb<1./6.) g[2]=CLAMP0255(p+((q-p)*6.*(2./3.-tb)));
	  else g[2]=CLAMP0255((int32_t)p);
	}
	else {
	  g[0]=p5[0];
	  g[1]=p5[1];
	  g[2]=p5[2];
	}
      }
    }
  }
};


frei0r::construct<edgeglow> plugin("Edgeglow",
                                "Edgeglow filter",
                                "Salsaman",
                                0,1,
                                F0R_COLOR_MODEL_RGBA8888);