Subversion Repositories filter_foundry

Rev

Rev 185 | Rev 194 | 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
192 daniel-mar 3
    Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
4
    Copyright (C) 2018-2019 Daniel Marschall, ViaThinkSoft
2 toby 5
 
6
    This program is free software; you can redistribute it and/or modify
106 dmarschall 7
    it under the terms of the GNU General Public License as published by
2 toby 8
    the Free Software Foundation; either version 2 of the License, or
9
    (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
106 dmarschall 16
    You should have received a copy of the GNU General Public License
2 toby 17
    along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
*/
20
 
21
#ifdef MAC_ENV
22
        #include <fp.h>
23
#endif
24
 
25
#include <math.h>
26
#include <stdlib.h>
27
 
28
#ifndef PARSERTEST
29
#include "ff.h"
30
#endif
31
#include "funcs.h"
32
#include "y.tab.h"
33
 
117 dmarschall 34
#include "node.h" // for symbol "var[]"
35
 
2 toby 36
#define RINT //no rounding for now
37
 
38
//#if TARGET_API_MAC_CARBON
39
// this is another incompatibility between Classic stdclib and OS X stdclib
40
// ***FIXME: need to access real OS X includes for Carbon build
41
//#undef RAND_MAX
42
//#define RAND_MAX    0x7fffffff
43
//#endif
44
 
45
extern value_type slider[],cell[],var[],map[][0x100];
66 toby 46
extern unsigned char *image_ptr;
2 toby 47
 
135 dmarschall 48
double costab[COSTABSIZE];
49
double tantab[TANTABSIZE];
50
void init_trigtab(){
51
        int i;
52
        for(i=0;i<COSTABSIZE;++i){
53
                costab[i] = cos(FFANGLE(i));
54
        }
55
        for(i=0;i<TANTABSIZE;++i){
136 dmarschall 56
                if (i>=256) {
57
                        /* the last '-1' in the expression '512-i-1' is for FilterFactory compatibility, and to avoid the undefined pi/2 area */
58
                        tantab[i] = -tantab[512-i-1];
59
                } else {
60
                        tantab[i] = tan(FFANGLE(i));
61
                }
135 dmarschall 62
        }
63
}
64
 
71 toby 65
/* Channel z for the input pixel at coordinates x,y.
76 toby 66
 * Coordinates are relative to the input image data (pb->inData) */
79 toby 67
static value_type rawsrc(value_type x,value_type y,value_type z){
106 dmarschall 68
        if(x < gpb->inRect.left)
71 toby 69
                x = gpb->inRect.left;
106 dmarschall 70
        else if(x >= gpb->inRect.right)
71 toby 71
                x = gpb->inRect.right-1;
106 dmarschall 72
        if(y < gpb->inRect.top)
71 toby 73
                y = gpb->inRect.top;
106 dmarschall 74
        else if(y >= gpb->inRect.bottom)
71 toby 75
                y = gpb->inRect.bottom-1;
76 toby 76
        return ((unsigned char*)gpb->inData)[ (long)gpb->inRowBytes*(y - gpb->inRect.top)
77
                                                                                  + (long)nplanes*(x - gpb->inRect.left) + z ];
71 toby 78
}
79
 
80
/* src(x,y,z) Channel z for the pixel at coordinates x,y.
81
 * Coordinates are relative to filtered area (selection). */
82
value_type ff_src(value_type x,value_type y,value_type z){
76 toby 83
#ifdef PARSERTEST
84
        return 0;
85
#else
106 dmarschall 86
        if(x < 0)
2 toby 87
                x = 0;
106 dmarschall 88
        else if(x >= var['X'])
2 toby 89
                x = var['X']-1;
106 dmarschall 90
        if(y < 0)
2 toby 91
                y = 0;
106 dmarschall 92
        else if(y >= var['Y'])
2 toby 93
                y = var['Y']-1;
71 toby 94
        return z >= 0 && z < var['Z'] ?
95
                image_ptr[(long)gpb->inRowBytes*y + (long)nplanes*x + z] : 0;
76 toby 96
#endif
2 toby 97
}
98
 
71 toby 99
/* rad(d,m,z) Channel z in the source image, which is m units away,
100
        at an angle of d, from the center of the image */
2 toby 101
value_type ff_rad(value_type d,value_type m,value_type z){
71 toby 102
        return ff_src(ff_r2x(d,m) + var['X']/2, ff_r2y(d,m) + var['Y']/2, z);
2 toby 103
}
104
 
105
/* ctl(i) Value of slider i, where i is an integer between 0 and 7, inclusive */
106
value_type ff_ctl(value_type i){
107
        return i>=0 && i<=7 ? slider[i] : 0;
108
}
109
 
110
/* val(i,a,b) Value of slider i, mapped onto the range a to b */
111
value_type ff_val(value_type i,value_type a,value_type b){
112
        return ((long)ff_ctl(i)*(b-a))/255 + a;
113
}
114
 
115
/* map(i,n) Item n from mapping table i, where i is an integer between
116
 
117
        inclusive */
118
value_type ff_map(value_type i,value_type n){
119
/*
120
        if( i>=0 && i<=3 && n>=0 && n<=255 ){
121
                int H = slider[i*2],L = slider[i*2+1];
122
                return n<=L || H==L ? 0 : ( n>=H ? 255 : ((n-L)*255L)/(H-L) );
123
        }else
124
                return 0;
125
*/
126
        // this code is from GIMP User Filter
127
        value_type x = ff_ctl(i*2),
128
                           y = ff_ctl(i*2+1);
129
        return abs(((long)n*(y-x) / 255)+x);
130
}
131
 
132
/* min(a,b) Lesser of a and b */
133
value_type ff_min(value_type a,value_type b){
134
        return a < b ? a : b;
135
}
136
 
137
/* max(a,b) Greater of a and b */
138
value_type ff_max(value_type a,value_type b){
139
        return a > b ? a : b;
140
}
141
 
142
/* abs(a) Absolute value of a */
143
value_type ff_abs(value_type a){
144
        return abs(a);
145
}
146
 
147
/* add(a,b,c) Sum of a and b, or c, whichever is lesser */
148
value_type ff_add(value_type a,value_type b,value_type c){
149
        return ff_min(a+b,c);
150
}
151
 
152
/* sub(a,b,c) Difference of a and b, or c, whichever is greater */
153
value_type ff_sub(value_type a,value_type b,value_type c){
154
        return ff_max(ff_dif(a,b),c);
155
}
156
 
157
/* dif(a,b) Absolute value of the difference of a and b */
158
value_type ff_dif(value_type a,value_type b){
159
        return abs(a-b);
160
}
161
 
162
/* rnd(a,b) Random number between a and b, inclusive */
163
value_type ff_rnd(value_type a,value_type b){
164
        return (int)((abs(a-b)+1)*(rand()/(RAND_MAX+1.))) + ff_min(a,b);
165
//      return ((unsigned)rand() % (ff_dif(a,b)+1)) + ff_min(a,b);
166
}
167
 
168
/* mix(a,b,n,d) Mixture of a and b by fraction n/d, a*n/d+b*(d-n)/d */
169
value_type ff_mix(value_type a,value_type b,value_type n,value_type d){
170
        return d ? ((long)a*n)/d + ((long)b*(d-n))/d : 0;
171
}
172
 
173
/* scl(a,il,ih,ol,oh) Scale a from input range (il to ih)
174
                      to output range (ol to oh) */
175
value_type ff_scl(value_type a,value_type il,value_type ih,
141 dmarschall 176
                  value_type ol,value_type oh){
2 toby 177
        return ih==il ? 0 : ol + ((long)(oh-ol)*(a-il))/(ih-il);
178
}
179
 
170 dmarschall 180
inline uint32_t isqrt(uint32_t x) {
181
        // based on https://gist.github.com/orlp/3481770
2 toby 182
 
170 dmarschall 183
        static uint32_t lkpSquares[65535];
184
        static int lkpInitialized = 0;
185
        const uint32_t *p;
186
        int i;
2 toby 187
 
170 dmarschall 188
        while (lkpInitialized == 1) { /* If other thread is currently creating the lookup table, then wait */ }
189
        if (!lkpInitialized) {
190
                lkpInitialized = 1;
191
                for (i = 0; i < 65535; ++i) {
192
                        lkpSquares[i] = i * i;
193
                }
194
                lkpInitialized = 2;
195
        }
2 toby 196
 
170 dmarschall 197
        p = lkpSquares;
2 toby 198
 
170 dmarschall 199
        if (p[32768] <= x) p += 32768;
200
        if (p[16384] <= x) p += 16384;
201
        if (p[8192] <= x) p += 8192;
202
        if (p[4096] <= x) p += 4096;
203
        if (p[2048] <= x) p += 2048;
204
        if (p[1024] <= x) p += 1024;
205
        if (p[512] <= x) p += 512;
206
        if (p[256] <= x) p += 256;
207
        if (p[128] <= x) p += 128;
208
        if (p[64] <= x) p += 64;
209
        if (p[32] <= x) p += 32;
210
        if (p[16] <= x) p += 16;
211
        if (p[8] <= x) p += 8;
212
        if (p[4] <= x) p += 4;
213
        if (p[2] <= x) p += 2;
214
        if (p[1] <= x) p += 1;
2 toby 215
 
185 dmarschall 216
        return (uint32_t)(p - lkpSquares);
2 toby 217
}
218
 
219
/* sqr(x) Square root of x */
220
value_type ff_sqr(value_type x){
221
        return x < 0 ? 0 : isqrt(x);
222
}
223
 
224
/* sin(x) Sine function of x, where x is an integer between 0 and
141 dmarschall 225
   1024, inclusive, and the value returned is an integer
226
   between -512 and 512, inclusive (Windows) or -1024 and
227
   1024, inclusive (Mac OS) */
2 toby 228
value_type ff_sin(value_type x){
134 dmarschall 229
        //return RINT(TRIGAMP*sin(FFANGLE(x)));
230
        return ff_cos(x-256);
2 toby 231
}
232
 
134 dmarschall 233
/* cos(x) Cosine function of x, where x is an integer between 0 and
234
   1024, inclusive, and the value returned is an integer
235
   between -512 and 512, inclusive (Windows) or -1024 and
236
   1024, inclusive (Mac OS) */
2 toby 237
value_type ff_cos(value_type x){
134 dmarschall 238
        //return RINT(TRIGAMP*cos(FFANGLE(x)));
185 dmarschall 239
        return (value_type)RINT(TRIGAMP*costab[abs(x) % COSTABSIZE]);
2 toby 240
}
241
 
134 dmarschall 242
/* tan(x) Bounded tangent function of x, where x is an integer
243
   between -256 and 256, inclusive, and the value returned is
244
   an integer between -512 and 512, inclusive (Windows) or
245
   -1024 and 1024, inclusive (Mac OS) */
2 toby 246
value_type ff_tan(value_type x){
134 dmarschall 247
        // TODO: Shouldn't the output be bounded to -1024..1024, or do I understand the definition wrong?
136 dmarschall 248
        if (x < 0) x--; /* required for FilterFactory compatibility */
249
        while (x < 0) x += TANTABSIZE;
185 dmarschall 250
        return (value_type)RINT(2*TRIGAMP*tantab[x % TANTABSIZE]); /* FIXME: why do we need factor 2? */
2 toby 251
}
252
 
253
/* r2x(d,m) x displacement of the pixel m units away, at an angle of d,
254
   from an arbitrary center */
255
value_type ff_r2x(value_type d,value_type m){
185 dmarschall 256
        return (value_type)RINT(m*costab[abs(d) % COSTABSIZE]);
2 toby 257
}
258
 
259
/* r2y(d,m) y displacement of the pixel m units away, at an angle of d,
260
   from an arbitrary center */
261
value_type ff_r2y(value_type d,value_type m){
185 dmarschall 262
        return (value_type)RINT(m*costab[abs(d-256) % COSTABSIZE]);
2 toby 263
}
264
 
265
/* c2d(x,y) Angle displacement of the pixel at coordinates x,y */
266
/* note, sign of y difference is negated, as we are dealing with top-down coordinates
267
   angle is "observed" */
268
value_type ff_c2d(value_type x,value_type y){
139 dmarschall 269
        // Behavior of FilterFoundry <1.7:
154 dmarschall 270
        //return RINT(TO_FFANGLE(atan2(-y,-x)));
139 dmarschall 271
 
272
        // Behavior in FilterFoundry 1.7+: Matches FilterFactory
185 dmarschall 273
        return (value_type)RINT(TO_FFANGLE(atan2(y,x)));
2 toby 274
}
275
 
276
/* c2m(x,y) Magnitude displacement of the pixel at coordinates x,y */
277
value_type ff_c2m(value_type x,value_type y){
278
        return isqrt((long)x*x + (long)y*y);
279
}
280
 
281
/* get(i) Returns the current cell value at i */
282
value_type ff_get(value_type i){
141 dmarschall 283
        // Filter Factory:
142 dmarschall 284
        //return i>=0 && i<NUM_CELLS ? cell[i] : i;
141 dmarschall 285
 
286
        // Filter Foundry:
142 dmarschall 287
        return i>=0 && i<NUM_CELLS ? cell[i] : 0;
2 toby 288
}
289
 
290
/* put(v,i) Puts the new value v into cell i */
291
value_type ff_put(value_type v,value_type i){
142 dmarschall 292
        if(i>=0 && i<NUM_CELLS)
2 toby 293
                cell[i] = v;
294
        return v;
295
}
296
 
297
value_type ff_cnv(value_type m11,value_type m12,value_type m13,
298
                                  value_type m21,value_type m22,value_type m23,
299
                                  value_type m31,value_type m32,value_type m33,
71 toby 300
                                  value_type d)
301
{
76 toby 302
#ifdef PARSERTEST
303
        return 0;
304
#else
71 toby 305
        long total;
306
        // shift x,y from selection-relative to image relative
307
        int x = var['x'] + gpb->filterRect.left,
308
                y = var['y'] + gpb->filterRect.top,
309
                z = var['z'];
2 toby 310
 
71 toby 311
        if(z >= 0 && z < var['Z'])
312
                total = m11*rawsrc(x-1,y-1,z) + m12*rawsrc(x,y-1,z) + m13*rawsrc(x+1,y-1,z)
313
                          + m21*rawsrc(x-1,y,  z) + m22*rawsrc(x,y,  z) + m23*rawsrc(x+1,y,  z)
314
                          + m31*rawsrc(x-1,y+1,z) + m32*rawsrc(x,y+1,z) + m33*rawsrc(x+1,y+1,z);
315
        else
316
                total = 0;
2 toby 317
 
318
        return d ? total/d : 0;
76 toby 319
#endif
2 toby 320
}
321
 
106 dmarschall 322
/* rst(i) sets a random seed and returns 0. (undocumented Filter Factory function).
323
   Added by DM, 18 Dec 2018 */
324
value_type ff_rst(value_type seed){
325
        srand(seed);
326
        return 0;
327
}
328
 
2 toby 329
value_type zero_val = 0;
118 dmarschall 330
value_type one_val = 1;
117 dmarschall 331
value_type max_channel_val = 255;
2 toby 332
 
333
/* predefined symbols */
334
struct sym_rec predefs[]={
335
        /* functions */
117 dmarschall 336
 
2 toby 337
        {0,TOK_FN3,"src", (pfunc_type)ff_src, 0},
338
        {0,TOK_FN3,"rad", (pfunc_type)ff_rad, 0},
339
        {0,TOK_FN1,"ctl", (pfunc_type)ff_ctl, 0},
340
        {0,TOK_FN3,"val", (pfunc_type)ff_val, 0},
341
        {0,TOK_FN2,"map", (pfunc_type)ff_map, 0},
342
        {0,TOK_FN2,"min", (pfunc_type)ff_min, 0},
343
        {0,TOK_FN2,"max", (pfunc_type)ff_max, 0},
344
        {0,TOK_FN1,"abs", (pfunc_type)ff_abs, 0},
345
        {0,TOK_FN3,"add", (pfunc_type)ff_add, 0},
346
        {0,TOK_FN3,"sub", (pfunc_type)ff_sub, 0},
347
        {0,TOK_FN2,"dif", (pfunc_type)ff_dif, 0},
348
        {0,TOK_FN2,"rnd", (pfunc_type)ff_rnd, 0},
349
        {0,TOK_FN4,"mix", (pfunc_type)ff_mix, 0},
350
        {0,TOK_FN5,"scl", (pfunc_type)ff_scl, 0},
351
        {0,TOK_FN1,"sqr", (pfunc_type)ff_sqr, 0},
146 dmarschall 352
        {0,TOK_FN1,"sqrt", (pfunc_type)ff_sqr, 0}, // sqrt() is synonym to sqr() in Premiere
2 toby 353
        {0,TOK_FN1,"sin", (pfunc_type)ff_sin, 0},
354
        {0,TOK_FN1,"cos", (pfunc_type)ff_cos, 0},
355
        {0,TOK_FN1,"tan", (pfunc_type)ff_tan, 0},
356
        {0,TOK_FN2,"r2x", (pfunc_type)ff_r2x, 0},
357
        {0,TOK_FN2,"r2y", (pfunc_type)ff_r2y, 0},
358
        {0,TOK_FN2,"c2d", (pfunc_type)ff_c2d, 0},
359
        {0,TOK_FN2,"c2m", (pfunc_type)ff_c2m, 0},
360
        {0,TOK_FN1,"get", (pfunc_type)ff_get, 0},
361
        {0,TOK_FN2,"put", (pfunc_type)ff_put, 0},
362
        {0,TOK_FN10,"cnv",(pfunc_type)ff_cnv, 0},
117 dmarschall 363
        {0,TOK_FN1,"rst", (pfunc_type)ff_rst, 0}, // undocumented FilterFactory function
364
 
365
        /* predefined variables (names with more than 1 character); most of them are undocumented in FilterFactory */
366
        /* the predefined variables with 1 character are defined in lexer.l and process.c */
367
        /* in this table, you must not add TOK_VAR with only 1 character (since this case is not defined in parser.y) */
368
 
369
        {0,TOK_VAR,"rmax",0, &max_channel_val}, // alias of 'R' (defined in lexer.l, line 129)
370
        {0,TOK_VAR,"gmax",0, &max_channel_val}, // alias of 'G' (defined in lexer.l, line 129)
371
        {0,TOK_VAR,"bmax",0, &max_channel_val}, // alias of 'B' (defined in lexer.l, line 129)
372
        {0,TOK_VAR,"amax",0, &max_channel_val}, // alias of 'A' (defined in lexer.l, line 129)
373
        {0,TOK_VAR,"cmax",0, &max_channel_val}, // alias of 'C' (defined in lexer.l, line 129)
374
        {0,TOK_VAR,"imax",0, &max_channel_val}, // alias of 'I' (defined in lexer.l, line 129)
375
        {0,TOK_VAR,"umax",0, &max_channel_val}, // alias of 'U' (defined in lexer.l, line 129)
376
        {0,TOK_VAR,"vmax",0, &max_channel_val}, // alias of 'V' (defined in lexer.l, line 129)
377
        {0,TOK_VAR,"dmax",0, &var['D']},
378
        {0,TOK_VAR,"mmax",0, &var['M']},
379
        {0,TOK_VAR,"pmax",0, &var['Z']},
380
        {0,TOK_VAR,"xmax",0, &var['X']},
381
        {0,TOK_VAR,"ymax",0, &var['Y']},
382
        {0,TOK_VAR,"zmax",0, &var['Z']},
383
 
384
        {0,TOK_VAR,"rmin",0, &zero_val},
385
        {0,TOK_VAR,"gmin",0, &zero_val},
386
        {0,TOK_VAR,"bmin",0, &zero_val},
387
        {0,TOK_VAR,"amin",0, &zero_val},
388
        {0,TOK_VAR,"cmin",0, &zero_val},
389
        {0,TOK_VAR,"imin",0, &zero_val},
390
        {0,TOK_VAR,"umin",0, &zero_val},
391
        {0,TOK_VAR,"vmin",0, &zero_val},
2 toby 392
        {0,TOK_VAR,"dmin",0, &zero_val},
393
        {0,TOK_VAR,"mmin",0, &zero_val},
117 dmarschall 394
        {0,TOK_VAR,"pmin",0, &zero_val},
395
        {0,TOK_VAR,"xmin",0, &zero_val},
396
        {0,TOK_VAR,"ymin",0, &zero_val},
397
        {0,TOK_VAR,"zmin",0, &zero_val},
398
 
399
        /* Undocumented synonyms of FilterFactory for compatibility with Premiere */
400
        {0,TOK_FN10,"cnv0",(pfunc_type)ff_cnv, 0},
401
        {0,TOK_FN3,"src0", (pfunc_type)ff_src, 0},
402
        {0,TOK_FN3,"rad0", (pfunc_type)ff_rad, 0},
403
        {0,TOK_FN10,"cnv1",(pfunc_type)ff_cnv, 0},
404
        {0,TOK_FN3,"src1", (pfunc_type)ff_src, 0},
405
        {0,TOK_FN3,"rad1", (pfunc_type)ff_rad, 0},
406
        {0,TOK_VAR,"r0",0, &var['r']},
407
        {0,TOK_VAR,"g0",0, &var['g']},
408
        {0,TOK_VAR,"b0",0, &var['b']},
409
        {0,TOK_VAR,"a0",0, &var['a']},
410
        {0,TOK_VAR,"c0",0, &var['c']},
411
        {0,TOK_VAR,"i0",0, &var['i']},
412
        {0,TOK_VAR,"u0",0, &var['u']},
413
        {0,TOK_VAR,"v0",0, &var['v']},
414
        {0,TOK_VAR,"d0",0, &var['d']},
415
        {0,TOK_VAR,"m0",0, &var['m']},
416
        {0,TOK_VAR,"r1",0, &var['r']},
417
        {0,TOK_VAR,"g1",0, &var['g']},
418
        {0,TOK_VAR,"b1",0, &var['b']},
419
        {0,TOK_VAR,"a1",0, &var['a']},
420
        {0,TOK_VAR,"c1",0, &var['c']},
421
        {0,TOK_VAR,"i1",0, &var['i']},
422
        {0,TOK_VAR,"u1",0, &var['u']},
423
        {0,TOK_VAR,"v1",0, &var['v']},
424
        {0,TOK_VAR,"d1",0, &var['d']},
425
        {0,TOK_VAR,"m1",0, &var['m']},
426
        {0,TOK_VAR,"tmin",0, &zero_val},
427
        {0,TOK_VAR,"tmax",0, &one_val},
428
        {0,TOK_VAR,"total",0, &one_val},
429
 
2 toby 430
        {0,0,0,0,0}
117 dmarschall 431
};