Rev 2 | Rev 23 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | toby | 1 | /* |
18 | toby | 2 | This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop |
2 | toby | 3 | Copyright (C) 2003-5 Toby Thain, toby@telegraphics.com.au |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
18 | */ |
||
19 | |||
20 | #ifdef MAC_ENV |
||
21 | #include <fp.h> |
||
22 | #endif |
||
23 | |||
24 | #include <math.h> |
||
25 | #include <stdlib.h> |
||
26 | |||
27 | #ifndef PARSERTEST |
||
28 | #include "ff.h" |
||
29 | #endif |
||
30 | #include "funcs.h" |
||
31 | #include "y.tab.h" |
||
32 | |||
33 | #define RINT //no rounding for now |
||
34 | |||
35 | //#if TARGET_API_MAC_CARBON |
||
36 | // this is another incompatibility between Classic stdclib and OS X stdclib |
||
37 | // ***FIXME: need to access real OS X includes for Carbon build |
||
38 | //#undef RAND_MAX |
||
39 | //#define RAND_MAX 0x7fffffff |
||
40 | //#endif |
||
41 | |||
42 | extern value_type slider[],cell[],var[],map[][0x100]; |
||
43 | |||
44 | /* src(x,y,z) Channel z for the pixel at coordinates x,y */ |
||
45 | value_type ff_src(value_type x,value_type y,value_type z){ |
||
46 | #ifdef PARSERTEST |
||
47 | return 0; |
||
48 | #else |
||
49 | if(x<0) |
||
50 | x = 0; |
||
51 | else if(x>=var['X']) |
||
52 | x = var['X']-1; |
||
53 | if(y<0) |
||
54 | y = 0; |
||
55 | else if(y>=var['Y']) |
||
56 | y = var['Y']-1; |
||
57 | return z>=0 && z<var['Z'] ? ((unsigned char*)gpb->inData)[ (long)gpb->inRowBytes*y + (long)nplanes*x + z ] : 0; |
||
58 | #endif |
||
59 | } |
||
60 | |||
61 | /* rad(d,m,z) Channel z in the source image, which is m units away, at an |
||
62 | angle of d, from the center of the image */ |
||
63 | value_type ff_rad(value_type d,value_type m,value_type z){ |
||
64 | return ff_src(ff_r2x(d,m)+var['X']/2,ff_r2y(d,m)+var['Y']/2,z); |
||
65 | } |
||
66 | |||
67 | /* ctl(i) Value of slider i, where i is an integer between 0 and 7, inclusive */ |
||
68 | value_type ff_ctl(value_type i){ |
||
69 | return i>=0 && i<=7 ? slider[i] : 0; |
||
70 | } |
||
71 | |||
72 | /* val(i,a,b) Value of slider i, mapped onto the range a to b */ |
||
73 | value_type ff_val(value_type i,value_type a,value_type b){ |
||
74 | return ((long)ff_ctl(i)*(b-a))/255 + a; |
||
75 | } |
||
76 | |||
77 | /* map(i,n) Item n from mapping table i, where i is an integer between |
||
78 | |||
79 | inclusive */ |
||
80 | value_type ff_map(value_type i,value_type n){ |
||
81 | /* |
||
82 | if( i>=0 && i<=3 && n>=0 && n<=255 ){ |
||
83 | int H = slider[i*2],L = slider[i*2+1]; |
||
84 | return n<=L || H==L ? 0 : ( n>=H ? 255 : ((n-L)*255L)/(H-L) ); |
||
85 | }else |
||
86 | return 0; |
||
87 | */ |
||
88 | // this code is from GIMP User Filter |
||
89 | value_type x = ff_ctl(i*2), |
||
90 | y = ff_ctl(i*2+1); |
||
91 | return abs(((long)n*(y-x) / 255)+x); |
||
92 | } |
||
93 | |||
94 | /* min(a,b) Lesser of a and b */ |
||
95 | value_type ff_min(value_type a,value_type b){ |
||
96 | return a < b ? a : b; |
||
97 | } |
||
98 | |||
99 | /* max(a,b) Greater of a and b */ |
||
100 | value_type ff_max(value_type a,value_type b){ |
||
101 | return a > b ? a : b; |
||
102 | } |
||
103 | |||
104 | /* abs(a) Absolute value of a */ |
||
105 | value_type ff_abs(value_type a){ |
||
106 | return abs(a); |
||
107 | } |
||
108 | |||
109 | /* add(a,b,c) Sum of a and b, or c, whichever is lesser */ |
||
110 | value_type ff_add(value_type a,value_type b,value_type c){ |
||
111 | return ff_min(a+b,c); |
||
112 | } |
||
113 | |||
114 | /* sub(a,b,c) Difference of a and b, or c, whichever is greater */ |
||
115 | value_type ff_sub(value_type a,value_type b,value_type c){ |
||
116 | return ff_max(ff_dif(a,b),c); |
||
117 | } |
||
118 | |||
119 | /* dif(a,b) Absolute value of the difference of a and b */ |
||
120 | value_type ff_dif(value_type a,value_type b){ |
||
121 | return abs(a-b); |
||
122 | } |
||
123 | |||
124 | /* rnd(a,b) Random number between a and b, inclusive */ |
||
125 | value_type ff_rnd(value_type a,value_type b){ |
||
126 | return (int)((abs(a-b)+1)*(rand()/(RAND_MAX+1.))) + ff_min(a,b); |
||
127 | // return ((unsigned)rand() % (ff_dif(a,b)+1)) + ff_min(a,b); |
||
128 | } |
||
129 | |||
130 | /* mix(a,b,n,d) Mixture of a and b by fraction n/d, a*n/d+b*(d-n)/d */ |
||
131 | value_type ff_mix(value_type a,value_type b,value_type n,value_type d){ |
||
132 | return d ? ((long)a*n)/d + ((long)b*(d-n))/d : 0; |
||
133 | } |
||
134 | |||
135 | /* scl(a,il,ih,ol,oh) Scale a from input range (il to ih) |
||
136 | to output range (ol to oh) */ |
||
137 | value_type ff_scl(value_type a,value_type il,value_type ih, |
||
138 | value_type ol,value_type oh){ |
||
139 | return ih==il ? 0 : ol + ((long)(oh-ol)*(a-il))/(ih-il); |
||
140 | } |
||
141 | |||
142 | /* adapted from http://remus.rutgers.edu/~rhoads/Code/isqrt.c */ |
||
143 | /* also see http://www.freaknet.org/martin/tape/gos/misc/personal/msc/sqrt/sqrt.c */ |
||
144 | #define NBITS (sizeof(long)*8) |
||
145 | #define TOP2BITS(x) (x>>(NBITS-2)) |
||
146 | |||
147 | unsigned long isqrt (unsigned long x) |
||
148 | { |
||
149 | int i; |
||
150 | unsigned long a = 0, e = 0, r = 0; |
||
151 | |||
152 | |||
153 | for (i=0; i < (NBITS >> 1); i++) |
||
154 | { |
||
155 | r <<= 2; |
||
156 | r += TOP2BITS(x); |
||
157 | x <<= 2; |
||
158 | |||
159 | a <<= 1; |
||
160 | e = (a<<1) | 1; |
||
161 | |||
162 | if (r >= e) |
||
163 | { |
||
164 | r -= e; |
||
165 | a++; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | return a; |
||
170 | } |
||
171 | |||
172 | /* sqr(x) Square root of x */ |
||
173 | value_type ff_sqr(value_type x){ |
||
174 | return x < 0 ? 0 : isqrt(x); |
||
175 | } |
||
176 | |||
177 | /* sin(x) Sine function of x, where x is an integer between 0 and |
||
178 | 1024, inclusive, and the value returned is an integer |
||
179 | between -512 and 512, inclusive (Windows) or -1024 and |
||
180 | 1024, inclusive (Mac OS) */ |
||
181 | value_type ff_sin(value_type x){ |
||
182 | return ff_cos(x-256); //RINT(TRIGAMP*sin(FFANGLE(x))); |
||
183 | } |
||
184 | |||
185 | /* cos(x) Cosine function of x */ |
||
186 | value_type ff_cos(value_type x){ |
||
187 | return costab[abs(x) % COSTABSIZE]; //RINT(TRIGAMP*cos(FFANGLE(x))); |
||
188 | } |
||
189 | |||
190 | /* tan(x) |
||
191 | Bounded tangent function of x, where x is an integer |
||
192 | between -256 and 256, inclusive, and the value returned is |
||
193 | */ |
||
194 | value_type ff_tan(value_type x){ |
||
195 | return tantab[(x+256) % TANTABSIZE]; //RINT(TRIGAMP*tan(FFANGLE(x))); |
||
196 | } |
||
197 | |||
198 | /* r2x(d,m) x displacement of the pixel m units away, at an angle of d, |
||
199 | from an arbitrary center */ |
||
200 | value_type ff_r2x(value_type d,value_type m){ |
||
201 | return RINT(m*cos(FFANGLE(d))); |
||
202 | } |
||
203 | |||
204 | /* r2y(d,m) y displacement of the pixel m units away, at an angle of d, |
||
205 | from an arbitrary center */ |
||
206 | value_type ff_r2y(value_type d,value_type m){ |
||
207 | return RINT(m*sin(FFANGLE(d))); |
||
208 | } |
||
209 | |||
210 | /* c2d(x,y) Angle displacement of the pixel at coordinates x,y */ |
||
211 | /* note, sign of y difference is negated, as we are dealing with top-down coordinates |
||
212 | angle is "observed" */ |
||
213 | value_type ff_c2d(value_type x,value_type y){ |
||
214 | return RINT(TO_FFANGLE(atan2(-y,-x))); /* FIXME: why must we negate x here? */ |
||
215 | } |
||
216 | |||
217 | /* c2m(x,y) Magnitude displacement of the pixel at coordinates x,y */ |
||
218 | value_type ff_c2m(value_type x,value_type y){ |
||
219 | return isqrt((long)x*x + (long)y*y); |
||
220 | } |
||
221 | |||
222 | /* get(i) Returns the current cell value at i */ |
||
223 | value_type ff_get(value_type i){ |
||
224 | return i>=0 && i<=0xff ? cell[i] : 0; |
||
225 | } |
||
226 | |||
227 | /* put(v,i) Puts the new value v into cell i */ |
||
228 | value_type ff_put(value_type v,value_type i){ |
||
229 | if(i>=0 && i<=0xff) |
||
230 | cell[i] = v; |
||
231 | return v; |
||
232 | } |
||
233 | |||
234 | value_type ff_cnv(value_type m11,value_type m12,value_type m13, |
||
235 | value_type m21,value_type m22,value_type m23, |
||
236 | value_type m31,value_type m32,value_type m33, |
||
237 | value_type d ){ |
||
238 | long total = 0; |
||
239 | int x=var['x'],y=var['y']; |
||
240 | unsigned char *p = (unsigned char*)gpb->inData + y*(long)gpb->inRowBytes + (x-1)*(long)nplanes + var['z']; |
||
241 | |||
242 | /* left column */ |
||
243 | if(x > 0){ |
||
244 | if(y > 0) total += m11*p[ -gpb->inRowBytes ]; |
||
245 | total += m21*p[ 0 ]; |
||
246 | if(y < var['Y']-1) total += m31*p[ gpb->inRowBytes ]; |
||
247 | } |
||
248 | |||
249 | /* centre column */ |
||
250 | p += nplanes; |
||
251 | if(y > 0) total += m12*p[ -gpb->inRowBytes ]; |
||
252 | total += m22*p[ 0 ]; |
||
253 | if(y < var['Y']-1) total += m32*p[ gpb->inRowBytes ]; |
||
254 | |||
255 | /* right column */ |
||
256 | if(x < var['X']-1){ |
||
257 | p += nplanes; |
||
258 | if(y > 0) total += m13*p[ -gpb->inRowBytes ]; |
||
259 | total += m23*p[ 0 ]; |
||
260 | if(y < var['Y']-1) total += m33*p[ gpb->inRowBytes ]; |
||
261 | } |
||
262 | |||
263 | return d ? total/d : 0; |
||
264 | } |
||
265 | |||
266 | value_type zero_val = 0; |
||
267 | |||
268 | /* predefined symbols */ |
||
269 | struct sym_rec predefs[]={ |
||
270 | /* functions */ |
||
271 | {0,TOK_FN3,"src", (pfunc_type)ff_src, 0}, |
||
272 | {0,TOK_FN3,"rad", (pfunc_type)ff_rad, 0}, |
||
273 | {0,TOK_FN1,"ctl", (pfunc_type)ff_ctl, 0}, |
||
274 | {0,TOK_FN3,"val", (pfunc_type)ff_val, 0}, |
||
275 | {0,TOK_FN2,"map", (pfunc_type)ff_map, 0}, |
||
276 | {0,TOK_FN2,"min", (pfunc_type)ff_min, 0}, |
||
277 | {0,TOK_FN2,"max", (pfunc_type)ff_max, 0}, |
||
278 | {0,TOK_FN1,"abs", (pfunc_type)ff_abs, 0}, |
||
279 | {0,TOK_FN3,"add", (pfunc_type)ff_add, 0}, |
||
280 | {0,TOK_FN3,"sub", (pfunc_type)ff_sub, 0}, |
||
281 | {0,TOK_FN2,"dif", (pfunc_type)ff_dif, 0}, |
||
282 | {0,TOK_FN2,"rnd", (pfunc_type)ff_rnd, 0}, |
||
283 | {0,TOK_FN4,"mix", (pfunc_type)ff_mix, 0}, |
||
284 | {0,TOK_FN5,"scl", (pfunc_type)ff_scl, 0}, |
||
285 | {0,TOK_FN1,"sqr", (pfunc_type)ff_sqr, 0}, |
||
286 | {0,TOK_FN1,"sin", (pfunc_type)ff_sin, 0}, |
||
287 | {0,TOK_FN1,"cos", (pfunc_type)ff_cos, 0}, |
||
288 | {0,TOK_FN1,"tan", (pfunc_type)ff_tan, 0}, |
||
289 | {0,TOK_FN2,"r2x", (pfunc_type)ff_r2x, 0}, |
||
290 | {0,TOK_FN2,"r2y", (pfunc_type)ff_r2y, 0}, |
||
291 | {0,TOK_FN2,"c2d", (pfunc_type)ff_c2d, 0}, |
||
292 | {0,TOK_FN2,"c2m", (pfunc_type)ff_c2m, 0}, |
||
293 | {0,TOK_FN1,"get", (pfunc_type)ff_get, 0}, |
||
294 | {0,TOK_FN2,"put", (pfunc_type)ff_put, 0}, |
||
295 | {0,TOK_FN10,"cnv",(pfunc_type)ff_cnv, 0}, |
||
296 | /* predefined variables (names >1 characters) */ |
||
297 | {0,TOK_VAR,"dmin",0, &zero_val}, |
||
298 | {0,TOK_VAR,"mmin",0, &zero_val}, |
||
299 | {0,0,0,0,0} |
||
300 | }; |
||
301 |