Subversion Repositories filter_foundry

Rev

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