Subversion Repositories filter_foundry

Rev

Rev 152 | Rev 155 | 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
53 toby 3
    Copyright (C) 2003-6 Toby Thain, toby@telegraphics.com.au
2 toby 4
 
5
    This program is free software; you can redistribute it and/or modify
106 dmarschall 6
    it under the terms of the GNU General Public License as published by
2 toby 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
 
106 dmarschall 15
    You should have received a copy of the GNU General Public License
2 toby 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
#include "ff.h"
94 toby 21
 
2 toby 22
#include "symtab.h"
23
#include "node.h"
24
#include "funcs.h"
8 toby 25
#include "y.tab.h"
2 toby 26
 
128 dmarschall 27
// Strict compatibility to Filter Factory
28
//#define YUV_FILTER_FACTORY
29
 
2 toby 30
extern value_type var[];
94 toby 31
extern int nplanes,varused[],cnvused;
2 toby 32
extern struct node *tree[];
66 toby 33
 
34
// points to first row, first column of selection image data
35
// this is used by src() and cnv() functions to access pixels
36
unsigned char *image_ptr;
37
 
2 toby 38
int needinput;
150 dmarschall 39
int state_changing_funcs_used;
2 toby 40
 
41
/* get prepared to evaluate expression trees--
42
   this assumes that tree[] array is already set up
43
   return TRUE if we're ready to go
44
*/
45
 
94 toby 46
// minimum setup required when formulae have not changed,
47
// and a new preview is to be generated. (Called by recalc_preview())
48
void evalinit(){
142 dmarschall 49
        int i;
50
 
94 toby 51
        INITRANDSEED();
142 dmarschall 52
 
53
        for (i=0; i<NUM_CELLS; ++i) {
54
                cell[i] = 0;
55
        }
94 toby 56
}
57
 
58
// full setup for evaluation, called when formulae have changed.
2 toby 59
Boolean setup(FilterRecordPtr pb){
60
        int i;
61
 
117 dmarschall 62
        // Attention: If you introduce new variables, please define them also in lexer.l
53 toby 63
        var['X'] = pb->filterRect.right - pb->filterRect.left;
64
        var['Y'] = pb->filterRect.bottom - pb->filterRect.top;
2 toby 65
        var['Z'] = nplanes;
66
        var['D'] = 1024;
67
        var['M'] = ff_c2m(var['X'],var['Y'])/2;
68
 
69
        /* initialise flags for tracking special variable usage */
71 toby 70
        for(i = 0; i < 0x100; i++)
2 toby 71
                varused[i] = 0;
94 toby 72
        needall = cnvused = 0;
71 toby 73
        for(i = 0; i < nplanes; ++i){
2 toby 74
//char s[100];sprintf(s,"expr[%d]=%#x",i,expr[i]);dbg(s);
71 toby 75
                if( tree[i] || (tree[i] = parseexpr(expr[i])) )
150 dmarschall 76
                        checkvars(tree[i],varused,&cnvused,&needall,&state_changing_funcs_used);
2 toby 77
                else
78
                        break;
79
        }
94 toby 80
        needinput = ( cnvused || needall
71 toby 81
                || varused['r'] || varused['g'] || varused['b'] || varused['a']
82
                || varused['i'] || varused['u'] || varused['v'] || varused['c'] );
106 dmarschall 83
 
94 toby 84
        evalinit();
2 toby 85
        return i==nplanes; /* all required expressions parse OK */
86
}
87
 
88
void evalpixel(unsigned char *outp,unsigned char *inp){
89
        int f,k;
90
 
91
        if(needinput){
92
                var['r'] = inp[0];
93
                var['g'] = nplanes > 1 ? inp[1] : 0;
94
                var['b'] = nplanes > 2 ? inp[2] : 0;
95
                var['a'] = nplanes > 3 ? inp[3] : 0;
106 dmarschall 96
 
128 dmarschall 97
#ifdef YUV_FILTER_FACTORY
98
                if(varused['i']) var['i'] = (( 76L*var['r'])+(150L*var['g'])+( 29L*var['b']))/256; // range: [0..254]
99
                if(varused['u']) var['u'] = ((-19L*var['r'])+(-37L*var['g'])+( 56L*var['b']))/256; // range: [-55..55]
100
                if(varused['v']) var['v'] = (( 78L*var['r'])+(-65L*var['g'])+(-13L*var['b']))/256; // range: [-77..77]
101
#else
102
                // These formulas are more accurate, e.g. pure white has now i=255 instead of 254
103
 
104
                // For Y, the definition is Y := 0.299R + 0.587G + 0.114B
105
                if(varused['i']) var['i'] = ((    299L*var['r'])+(    587L*var['g'])+(    114L*var['b']))/1000;    // range: [0..255]
106
 
107
                // For U, the definition is U := (B-Y) * 0.493; the range would be [-111..111]
108
                // Filter Factory divided it by 2, resulting in a range of [-55..55].
109
                // Due to compatibility reasons, we adopt that behavior.
110
                if(varused['u']) var['u'] = ((-147407L*var['r'])+(-289391L*var['g'])+( 436798L*var['b']))/2000000; // range: [-55..55]
111
 
112
                // For V, the definition is V := (R-Y) * 0.877; the range would be [-156..156]
113
                // Filter Factory divided it by 2, resulting in a range of [-78..78].
114
                // Due to compatibility reasons, we adopt that behavior.
115
                if(varused['v']) var['v'] = (( 614777L*var['r'])+(-514799L*var['g'])+(- 99978L*var['b']))/2000000; // range: [-78..78]
116
#endif
117
 
2 toby 118
        }
66 toby 119
        if(varused['d']) var['d'] = ff_c2d(var['X']/2 - var['x'], var['Y']/2 - var['y']);
120
        if(varused['m']) var['m'] = ff_c2m(var['X']/2 - var['x'], var['Y']/2 - var['y']);
106 dmarschall 121
 
66 toby 122
        for(k = 0; k < nplanes; ++k){
2 toby 123
                if(needinput)
124
                        var['c'] = inp[k];
125
                var['z'] = k;
117 dmarschall 126
                var['p'] = k; // undocumented alias of z
2 toby 127
                f = eval(tree[k]);
150 dmarschall 128
                if (outp)
129
                        outp[k] = f<0 ? 0 : (f>255 ? 255 : f); // clamp channel value to 0-255
2 toby 130
        }
131
}
132
 
53 toby 133
/* Zoom and filter image.
66 toby 134
 * Parameters:  pb          - Photoshop Filter parameter block
135
 *              progress    - whether to use Photoshop's progress bar
53 toby 136
 *                            (not appropriate during preview)
66 toby 137
 *              filterRect  - rectangle (within pb->inRect)
53 toby 138
 *                            of area to be filtered. This may not correspond
139
 *                            to pb->filterRect, it may be just a piece.
66 toby 140
 *              outPiece    - rectangle defining scaled output buffer.
141
 *                            In case of zoomed preview, this is physically
142
 *                            scaled FROM filterRect (or equal to filterRect
143
 *                            for unscaled 1:1 filtering).
53 toby 144
 *              outData     - pointer to output data buffer
145
 *              outRowBytes - row stride of output data buffer
106 dmarschall 146
 *              zoom        - pixel scale factor (both horiz & vert)
53 toby 147
 *                            e.g. 2.0 means 1 output pixel per 2 input pixels.
148
 */
2 toby 149
 
154 dmarschall 150
//#define PROCESS_SCALED_GAP_DEBUG 1
151
 
53 toby 152
OSErr process_scaled(FilterRecordPtr pb, Boolean progress,
66 toby 153
                          Rect *filterPiece, Rect *outPiece,
53 toby 154
                          void *outData, long outRowBytes, double zoom){
2 toby 155
        unsigned char *inrow,*outrow,*outp;
154 dmarschall 156
        int j,i;
2 toby 157
        long t,ticks = TICKCOUNT();
154 dmarschall 158
        double x,y,k;
66 toby 159
 
154 dmarschall 160
        #ifdef PROCESS_SCALED_GAP_DEBUG
161
        char s[0x200];
162
        int last_good_x, last_good_y;
163
        last_good_y = -1;
164
        #endif
165
 
106 dmarschall 166
        // find base pointer to selection image data
167
        image_ptr = (unsigned char*)pb->inData
168
                                + (long)pb->inRowBytes*(pb->filterRect.top - pb->inRect.top)
169
                                + (long)nplanes*(pb->filterRect.left - pb->inRect.left);
66 toby 170
 
150 dmarschall 171
        if (state_changing_funcs_used) {
154 dmarschall 172
                // Fill gap between selection/filter top border and top preview zoomed border
173
                for (y = 0; y < filterPiece->top - pb->filterRect.top; ++y) {
174
                        #ifdef PROCESS_SCALED_GAP_DEBUG
175
                        if (state_changing_funcs_used && last_good_y != (int)floor(y-1)) { sprintf(s, "Non calculated Y gap, type 1: %f, last good %d, zoom %f\n", y, last_good_y, zoom); simplealert(s); } last_good_y = (int)floor(y);
176
                        #endif
177
 
150 dmarschall 178
                        var['y'] = y;
179
                        inrow = image_ptr + (long)(y)*pb->inRowBytes;
154 dmarschall 180
 
181
                        #ifdef PROCESS_SCALED_GAP_DEBUG
182
                        last_good_x = -1;
183
                        #endif
184
 
185
                        for (x = 0; x < pb->filterRect.right - pb->filterRect.left; ++x) {
186
                                #ifdef PROCESS_SCALED_GAP_DEBUG
187
                                if (state_changing_funcs_used && last_good_x != (int)floor(x-1)) { sprintf(s, "Non calculated X gap, type 1a: %f, last good %d, zoom %f\n", x, last_good_x, zoom); simplealert(s); } last_good_x = (int)floor(x);
188
                                #endif
189
 
150 dmarschall 190
                                var['x'] = x;
191
                                evalpixel(NULL,inrow + (long)(x)*nplanes);
192
                        }
154 dmarschall 193
 
194
                        #ifdef PROCESS_SCALED_GAP_DEBUG
195
                        if (var['x'] != var['X']-1) { sprintf(s, "X not at right border #1: x=%d, X=%d\n", var['x'], var['X']); simplealert(s); }
196
                        #endif
150 dmarschall 197
                }
198
        }
199
 
106 dmarschall 200
        // j indexes scaled output rows
201
        for( j = outPiece->top, outrow = (unsigned char*)outData, y = filterPiece->top - pb->filterRect.top ;
202
                 j < outPiece->bottom ; ++j, outrow += outRowBytes, y += zoom )
203
        {
154 dmarschall 204
                #ifdef PROCESS_SCALED_GAP_DEBUG
205
                if (state_changing_funcs_used && last_good_y != (int)floor(y-1)) { sprintf(s, "Non calculated Y gap, type 1: %f, last good %d, zoom %f\n", y, last_good_y, zoom); simplealert(s); } last_good_y = (int)floor(y);
206
                #endif
207
 
106 dmarschall 208
                var['y'] = y;  // index of corresponding *input* row, top of selection == 0
209
                inrow = image_ptr + (long)y*pb->inRowBytes;
66 toby 210
 
154 dmarschall 211
                #ifdef PROCESS_SCALED_GAP_DEBUG
212
                last_good_x = -1;
213
                #endif
214
 
150 dmarschall 215
                if (state_changing_funcs_used) {
154 dmarschall 216
                        // Fill gap between left selection/image border and left border of the preview-area
217
                        for (x = 0; x < filterPiece->left - pb->filterRect.left; ++x) {
218
                                #ifdef PROCESS_SCALED_GAP_DEBUG
219
                                if (state_changing_funcs_used && last_good_x != (int)floor(x-1)) { sprintf(s, "Non calculated X gap, type 2a: %f, last good %d, zoom %f\n", x, last_good_x, zoom); simplealert(s); } last_good_x = (int)floor(x);
220
                                #endif
221
 
150 dmarschall 222
                                var['x'] = x;
223
                                evalpixel(NULL,inrow + (long)(x)*nplanes);
224
                        }
225
                }
226
 
106 dmarschall 227
                // i indexes scaled output columns
228
                for( outp = outrow, i = outPiece->left, x = filterPiece->left - pb->filterRect.left ;
229
                         i < outPiece->right ; ++i, outp += nplanes, x += zoom )
53 toby 230
                {
154 dmarschall 231
                        #ifdef PROCESS_SCALED_GAP_DEBUG
232
                        if (state_changing_funcs_used && last_good_x != (int)floor(x-1)) { sprintf(s, "Non calculated X gap, type 2b: %f, last good %d, zoom %f\n", x, last_good_x, zoom); simplealert(s); } last_good_x = (int)floor(x);
233
                        #endif
234
 
106 dmarschall 235
                        var['x'] = x;  // index of corresponding *input* column, left of selection == 0
154 dmarschall 236
                        evalpixel(outp,inrow + (long)(x)*nplanes); /* var['x'] & var['y'] are implicit parameters */
150 dmarschall 237
 
238
                        if (state_changing_funcs_used) {
154 dmarschall 239
                                // Fill gap between each X-preview-pixel (discarded pixels due to zoom level)
240
                                for (k = x+1; floor(k) < floor(x + zoom); ++k) {
241
                                        #ifdef PROCESS_SCALED_GAP_DEBUG
242
                                        if (state_changing_funcs_used && last_good_x != (int)floor(k-1)) { sprintf(s, "Non calculated X gap, type 2c: %f (x=%f), last good %d, zoom %f\n", k, x, last_good_x, zoom); simplealert(s); } last_good_x = (int)floor(k);
243
                                        #endif
244
 
150 dmarschall 245
                                        var['x'] = k;
154 dmarschall 246
                                        if (var['x'] >= var['X']) break;
150 dmarschall 247
                                        evalpixel(NULL,inrow + (long)(k)*nplanes);
248
                                }
249
                        }
106 dmarschall 250
                }
66 toby 251
 
154 dmarschall 252
                if (state_changing_funcs_used) {
253
                        // Fill gap between right border of preview-area and right border of selection/image border
254
 
255
                        for (x = var['x']+1; x < pb->filterRect.right - pb->filterRect.left; ++x) {
256
                                #ifdef PROCESS_SCALED_GAP_DEBUG
257
                                if (state_changing_funcs_used && last_good_x != (int)floor(x-1)) { sprintf(s, "Non calculated X gap, type 2d: %f, last good %d, zoom %f\n", x, last_good_x, zoom); simplealert(s); } last_good_x = (int)floor(x);
258
                                #endif
259
 
260
                                var['x'] = x;
261
                                evalpixel(NULL,inrow + (long)(x)*nplanes);
150 dmarschall 262
                        }
154 dmarschall 263
 
264
                        #ifdef PROCESS_SCALED_GAP_DEBUG
265
                        if (var['x'] != var['X']-1) { sprintf(s, "X not at right border #2: x=%d, X=%d\n", var['x'], var['X']); simplealert(s);}
266
                        #endif
267
 
268
                        // Fill gap between each Y-preview-pixel (discarded pixels due to zoom level)
269
                        for (k = y+1; floor(k) < floor(y + zoom); ++k) {
270
                                #ifdef PROCESS_SCALED_GAP_DEBUG
271
                                if (state_changing_funcs_used && last_good_y != (int)floor(k-1)) { sprintf(s, "Non calculated Y gap, type 3a: %f (y=%f), last good %d, zoom %f\n", k, y, last_good_y, zoom); simplealert(s); } last_good_y = (int)floor(k);
272
                                #endif
273
 
150 dmarschall 274
                                var['y'] = k;
154 dmarschall 275
                                if (var['y'] >= var['Y']) break;
150 dmarschall 276
                                inrow = image_ptr + (long)(k)*pb->inRowBytes;
154 dmarschall 277
 
278
                                #ifdef PROCESS_SCALED_GAP_DEBUG
279
                                last_good_x = -1;
280
                                #endif
281
 
282
                                for (x = 0; x < pb->filterRect.right - pb->filterRect.left; ++x) {
283
                                        #ifdef PROCESS_SCALED_GAP_DEBUG
284
                                        if (state_changing_funcs_used && last_good_x != (int)floor(x-1)) { sprintf(s, "Non calculated X gap, type 3b: %f, last good %d, zoom %f\n", x, last_good_x, zoom); simplealert(s); } last_good_x = (int)floor(x);
285
                                        #endif
286
 
150 dmarschall 287
                                        var['x'] = x;
288
                                        evalpixel(NULL,inrow + (long)(x)*nplanes);
289
                                }
154 dmarschall 290
 
291
                                #ifdef PROCESS_SCALED_GAP_DEBUG
292
                                if (var['x'] != var['X']-1) {sprintf(s, "X not at right border #3: x=%d, X=%d\n", var['x'], var['X']); simplealert(s);}
293
                                #endif
150 dmarschall 294
                        }
295
                }
296
 
106 dmarschall 297
                if(progress){
298
                        if((t = TICKCOUNT()) > ticks){
299
                                ticks = t + TICKS_SEC/4;
300
                                if(pb->abortProc())
301
                                        return userCanceledErr;
302
                                else
303
                                        pb->progressProc((int)y - pb->filterRect.top,pb->filterRect.bottom - pb->filterRect.top);
2 toby 304
                        }
106 dmarschall 305
                }
2 toby 306
#ifdef MAC_ENV
106 dmarschall 307
                else{
308
                        /* to stop delays during typing of expressions,
309
                           immediately abort preview calculation if a key or mouse has been pressed. */
310
                        EventRecord event;
311
                        if(EventAvail(mDownMask|keyDownMask|autoKeyMask,&event))
312
                                return userCanceledErr;
313
                }
2 toby 314
#endif
106 dmarschall 315
        }
2 toby 316
 
154 dmarschall 317
        // Note for state_changing_funcs_used: We will not evaluate the gap between bottom border
318
        // of preview area and the bottom border of the selection/filter, because there are no
319
        // preview output pixels left that could be affected by these gap evaluations.
320
 
2 toby 321
        return noErr;
322
}