Debian Codegraph @

src/filter/edgeglow/edgeglow.cpp

raw symbols SCIP
1 /* edgeglow.cpp
2 * Copyright (C) 2008 Salsaman ([email protected])
3 * This file is a Frei0r plugin.
4 *
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.
9 *
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.
14 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "frei0r.hpp"
21 #include "frei0r/math.h"
22 #include <stdlib.h>
23
24 class edgeglow : public frei0r::filter
25 {
26 public:
27
28 double lthresh;
29 double lupscale;
30 double lredscale;
31
32
33 edgeglow(unsigned int width, unsigned int height)
34 {
35 lthresh = 0.0;
36 lupscale = 0.0;
37 lredscale = 0.0;
38 register_param(lthresh, "lthresh", "threshold for edge lightening");
39 register_param(lupscale, "lupscale", "multiplier for upscaling edge brightness");
40 register_param(lredscale, "lredscale", "multiplier for downscaling non-edge brightness");
41 }
42
43 virtual void update(double time,
44 uint32_t* out,
45 const uint32_t* in)
46 {
47 std::copy(in, in + width*height, out);
48 for (unsigned int y=1; y<height-1; ++y)
49 {
50 for (unsigned int x=1; x<width-1; ++x)
51 {
52 // XXX faut eviter le * width en y allant par rangee
53 unsigned char *p1 = (unsigned char *)&in[(y-1)*width+(x-1)];
54 unsigned char *p2 = (unsigned char *)&in[(y-1)*width+x];
55 unsigned char *p3 = (unsigned char *)&in[(y-1)*width+(x+1)];
56 unsigned char *p4 = (unsigned char *)&in[y*width+(x-1)];
57 unsigned char *p6 = (unsigned char *)&in[y*width+(x+1)];
58 unsigned char *p7 = (unsigned char *)&in[(y+1)*width+(x-1)];
59 unsigned char *p8 = (unsigned char *)&in[(y+1)*width+x];
60 unsigned char *p9 = (unsigned char *)&in[(y+1)*width+(x+1)];
61
62 unsigned char *g = (unsigned char *)&out[y*width+x];
63
64 for (int i=0; i<3; ++i)
65 g[i] = CLAMP0255(
66 abs(p1[i] + p2[i]*2 + p3[i] - p7[i] - p8[i]*2 - p9[i]) +
67 abs(p3[i] + p6[i]*2 + p9[i] - p1[i] - p4[i]*2 - p7[i]) );
68
69
70 unsigned char *p5 = (unsigned char *)&in[y*width+x];
71
72 g[3] = p5[3]; // copy alpha
73
74 float lt;
75
76 // calculate "lightness"
77 unsigned char R=g[0];
78 unsigned char G=g[1];
79 unsigned char B=g[2];
80
81 unsigned char max=R;
82 if (G>max) max=G;
83 if (B>max) max=B;
84
85 unsigned char min=R;
86 if (G<min) min=G;
87 if (B<min) min=B;
88
89 unsigned char l=(unsigned char) (((float)max+(float)min)/2.);
90
91
92
93 // convert in and out to HSL, add L value of out
94
95 unsigned int h;
96 float s;
97
98 R=p5[0];
99 G=p5[1];
100 B=p5[2];
101
102 max=R;
103 if (G>max) max=G;
104 if (B>max) max=B;
105
106 min=R;
107 if (G<min) min=G;
108 if (B<min) min=B;
109
110
111 if (l>(lt=lthresh*255.)) {
112 // if lightness > threshold, we add it to the lightness of the original
113 l=CLAMP0255((int32_t)((float)l*lupscale+((float)max+(float)min)/2.));
114 }
115 // otherwise reduce
116 else if (lredscale>0.) {
117 l=((float)max+(float)min)/2.*(1.-lredscale);
118 }
119
120 if (lredscale>0.||l>lt) {
121 if (max==min) {
122 h=0;
123 s=0.;
124 }
125 else {
126 if (max==R) {
127 h=(unsigned int)(60.*((float)G-(float)B)/((float)max-(float)min));
128 if (G<B) h+=360;
129 }
130 else {
131 if (max==G) h=(unsigned int)(60.*((float)B-(float)R)/((float)max-(float)min)+120.);
132 else h=(unsigned int)(60.*(float)(R-G)/float(max-min)+240.);
133 }
134
135 if (l<=0.5) s=((float)max-(float)min)/((float)max+(float)min);
136 else s=((float)max-(float)min)/(2.-((float)max+(float)min));
137 }
138
139
140 // convert back to RGB
141
142 float q;
143
144 if (l<0.5) q=l*(1.+s);
145 else q=l+s-(l*s);
146
147 float p=2.*l-q;
148
149 float hk=(float)h/360.;
150
151 float tr=hk+1./3.;
152 float tg=hk;
153 float tb=hk-1./3.;
154
155 if (tr>1.) tr-=1.;
156 if (tb<0.) tb+=1.;
157
158 if (tr<1./6.) g[0]=CLAMP0255(p+((q-p)*6.*tr));
159 else if (tr<0.5) g[0]=CLAMP0255((int32_t)q);
160 else if (tr<2./3.) g[0]=CLAMP0255(p+((q-p)*6.*(2./3.-tr)));
161 else g[0]=CLAMP0255((int32_t)p);
162
163 if (tg<1./6.) g[1]=CLAMP0255(p+((q-p)*6.*tg));
164 else if (tg<0.5) g[1]=CLAMP0255((int32_t)q);
165 else if (tg<2./3.) g[1]=CLAMP0255(p+((q-p)*6.*(2./3.-tg)));
166 else g[1]=CLAMP0255((int32_t)p);
167
168 if (tb<1./6.) g[2]=CLAMP0255(p+((q-p)*6.*tb));
169 else if (tb<0.5) g[2]=CLAMP0255((int32_t)q);
170 else if (tb<2./3.) g[2]=CLAMP0255(p+((q-p)*6.*(2./3.-tb)));
171 else g[2]=CLAMP0255((int32_t)p);
172 }
173 else {
174 g[0]=p5[0];
175 g[1]=p5[1];
176 g[2]=p5[2];
177 }
178 }
179 }
180 }
181 };
182
183
184 frei0r::construct<edgeglow> plugin("Edgeglow",
185 "Edgeglow filter",
186 "Salsaman",
187 0,3,
188 F0R_COLOR_MODEL_RGBA8888);
189