| 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"cxx . . $ `<file>/include/frei0r.hpp`/ |
| 21 | #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/ |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | class edgeglowcxx . . $ edgeglow# : public frei0rcxx . . $ frei0r/::filtercxx . . $ frei0r/filter# |
| 25 | { |
| 26 | public: |
| 27 | |
| 28 | double lthreshcxx . . $ edgeglow#lthresh.; |
| 29 | double lupscalecxx . . $ edgeglow#lupscale.; |
| 30 | double lredscalecxx . . $ edgeglow#lredscale.; |
| 31 | |
| 32 | |
| 33 | edgeglowcxx . . $ edgeglow#edgeglow(b4d55b1286ff73e8).(unsigned int width, unsigned int height) |
| 34 | { |
| 35 | lthreshcxx . . $ edgeglow#lthresh. = 0.0; |
| 36 | lupscalecxx . . $ edgeglow#lupscale. = 0.0; |
| 37 | lredscalecxx . . $ edgeglow#lredscale. = 0.0; |
| 38 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(lthreshcxx . . $ edgeglow#lthresh., "lthresh"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "threshold for edge lightening"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 39 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(lupscalecxx . . $ edgeglow#lupscale., "lupscale"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "multiplier for upscaling edge brightness"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 40 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(lredscalecxx . . $ edgeglow#lredscale., "lredscale"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "multiplier for downscaling non-edge brightness"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 41 | } |
| 42 | |
| 43 | virtual void updatecxx . . $ edgeglow#update(da7b4a17ceb4e697).(double time, |
| 44 | uint32_t* out, |
| 45 | const uint32_t* in) |
| 46 | { |
| 47 | stdcxx . . $ std/::copycxx . . $ std/copy(88304b602929f6f4).(in, in + widthcxx . . $ frei0r/fx#width.*heightcxx . . $ frei0r/fx#height., out); |
| 48 | for (unsigned int y=1; y<heightcxx . . $ frei0r/fx#height.-1; ++y) |
| 49 | { |
| 50 | for (unsigned int x=1; x<widthcxx . . $ frei0r/fx#width.-1; ++x) |
| 51 | { |
| 52 | // XXX faut eviter le * width en y allant par rangee |
| 53 | unsigned char *p1 = (unsigned char *)&in[(y-1)*widthcxx . . $ frei0r/fx#width.+(x-1)]; |
| 54 | unsigned char *p2 = (unsigned char *)&in[(y-1)*widthcxx . . $ frei0r/fx#width.+x]; |
| 55 | unsigned char *p3 = (unsigned char *)&in[(y-1)*widthcxx . . $ frei0r/fx#width.+(x+1)]; |
| 56 | unsigned char *p4 = (unsigned char *)&in[y*widthcxx . . $ frei0r/fx#width.+(x-1)]; |
| 57 | unsigned char *p6 = (unsigned char *)&in[y*widthcxx . . $ frei0r/fx#width.+(x+1)]; |
| 58 | unsigned char *p7 = (unsigned char *)&in[(y+1)*widthcxx . . $ frei0r/fx#width.+(x-1)]; |
| 59 | unsigned char *p8 = (unsigned char *)&in[(y+1)*widthcxx . . $ frei0r/fx#width.+x]; |
| 60 | unsigned char *p9 = (unsigned char *)&in[(y+1)*widthcxx . . $ frei0r/fx#width.+(x+1)]; |
| 61 | |
| 62 | unsigned char *g = (unsigned char *)&out[y*widthcxx . . $ frei0r/fx#width.+x]; |
| 63 | |
| 64 | for (int i=0; i<3; ++i) |
| 65 | g[i] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).( |
| 66 | abscxx . . $ abs(7864480464b09eea).(p1[i] + p2[i]*2 + p3[i] - p7[i] - p8[i]*2 - p9[i]) + |
| 67 | abscxx . . $ abs(7864480464b09eea).(p3[i] + p6[i]*2 + p9[i] - p1[i] - p4[i]*2 - p7[i]) ); |
| 68 | |
| 69 | |
| 70 | unsigned char *p5 = (unsigned char *)&in[y*widthcxx . . $ frei0r/fx#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=lthreshcxx . . $ edgeglow#lthresh.*255.)) { |
| 112 | // if lightness > threshold, we add it to the lightness of the original |
| 113 | l=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int32_t)((float)l*lupscalecxx . . $ edgeglow#lupscale.+((float)max+(float)min)/2.)); |
| 114 | } |
| 115 | // otherwise reduce |
| 116 | else if (lredscalecxx . . $ edgeglow#lredscale.>0.) { |
| 117 | l=((float)max+(float)min)/2.*(1.-lredscalecxx . . $ edgeglow#lredscale.); |
| 118 | } |
| 119 | |
| 120 | if (lredscalecxx . . $ edgeglow#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]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(p+((q-p)*6.*tr)); |
| 159 | else if (tr<0.5) g[0]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int32_t)q); |
| 160 | else if (tr<2./3.) g[0]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(p+((q-p)*6.*(2./3.-tr))); |
| 161 | else g[0]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int32_t)p); |
| 162 | |
| 163 | if (tg<1./6.) g[1]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(p+((q-p)*6.*tg)); |
| 164 | else if (tg<0.5) g[1]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int32_t)q); |
| 165 | else if (tg<2./3.) g[1]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(p+((q-p)*6.*(2./3.-tg))); |
| 166 | else g[1]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int32_t)p); |
| 167 | |
| 168 | if (tb<1./6.) g[2]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(p+((q-p)*6.*tb)); |
| 169 | else if (tb<0.5) g[2]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int32_t)q); |
| 170 | else if (tb<2./3.) g[2]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(p+((q-p)*6.*(2./3.-tb))); |
| 171 | else g[2]=CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((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 | frei0rcxx . . $ frei0r/::constructcxx . . $ frei0r/construct#<edgeglowcxx . . $ edgeglow#> plugincxx . . $ plugin.("Edgeglow"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 185 | "Edgeglow filter"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 186 | "Salsaman"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 187 | 0,3, |
| 188 | F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!); |
| 189 |