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 |
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 |
||
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 | #include "ff.h" |
||
21 | #include "symtab.h" |
||
22 | #include "node.h" |
||
23 | #include "funcs.h" |
||
8 | toby | 24 | #include "y.tab.h" |
2 | toby | 25 | |
26 | extern value_type var[]; |
||
71 | toby | 27 | extern int nplanes,varused[],cnvused,srcradused; |
2 | toby | 28 | extern struct node *tree[]; |
66 | toby | 29 | |
30 | // points to first row, first column of selection image data |
||
31 | // this is used by src() and cnv() functions to access pixels |
||
32 | unsigned char *image_ptr; |
||
33 | |||
2 | toby | 34 | int needinput; |
35 | |||
36 | /* get prepared to evaluate expression trees-- |
||
37 | this assumes that tree[] array is already set up |
||
38 | return TRUE if we're ready to go |
||
39 | */ |
||
40 | |||
41 | Boolean setup(FilterRecordPtr pb){ |
||
42 | int i; |
||
43 | |||
44 | INITRANDSEED(); |
||
53 | toby | 45 | var['X'] = pb->filterRect.right - pb->filterRect.left; |
46 | var['Y'] = pb->filterRect.bottom - pb->filterRect.top; |
||
2 | toby | 47 | var['Z'] = nplanes; |
48 | var['D'] = 1024; |
||
49 | var['M'] = ff_c2m(var['X'],var['Y'])/2; |
||
50 | |||
51 | /* initialise flags for tracking special variable usage */ |
||
71 | toby | 52 | for(i = 0; i < 0x100; i++) |
2 | toby | 53 | varused[i] = 0; |
71 | toby | 54 | srcradused = cnvused = 0; |
55 | for(i = 0; i < nplanes; ++i){ |
||
2 | toby | 56 | //char s[100];sprintf(s,"expr[%d]=%#x",i,expr[i]);dbg(s); |
71 | toby | 57 | if( tree[i] || (tree[i] = parseexpr(expr[i])) ) |
58 | checkvars(tree[i],varused,&cnvused,&srcradused); |
||
2 | toby | 59 | else |
60 | break; |
||
61 | } |
||
71 | toby | 62 | needinput = ( cnvused || srcradused |
63 | || varused['r'] || varused['g'] || varused['b'] || varused['a'] |
||
64 | || varused['i'] || varused['u'] || varused['v'] || varused['c'] ); |
||
2 | toby | 65 | |
66 | return i==nplanes; /* all required expressions parse OK */ |
||
67 | } |
||
68 | |||
69 | void evalpixel(unsigned char *outp,unsigned char *inp){ |
||
70 | int f,k; |
||
71 | |||
72 | if(needinput){ |
||
73 | var['r'] = inp[0]; |
||
74 | var['g'] = nplanes > 1 ? inp[1] : 0; |
||
75 | var['b'] = nplanes > 2 ? inp[2] : 0; |
||
76 | var['a'] = nplanes > 3 ? inp[3] : 0; |
||
77 | |||
78 | if(varused['i']) var['i'] = (( 76L*var['r'])+(150L*var['g'])+( 29L*var['b']))/256; |
||
79 | if(varused['u']) var['u'] = ((-19L*var['r'])+(-37L*var['g'])+( 56L*var['b']))/256; |
||
80 | if(varused['v']) var['v'] = (( 78L*var['r'])+(-65L*var['g'])+(-13L*var['b']))/256; |
||
81 | } |
||
66 | toby | 82 | if(varused['d']) var['d'] = ff_c2d(var['X']/2 - var['x'], var['Y']/2 - var['y']); |
83 | if(varused['m']) var['m'] = ff_c2m(var['X']/2 - var['x'], var['Y']/2 - var['y']); |
||
2 | toby | 84 | |
66 | toby | 85 | for(k = 0; k < nplanes; ++k){ |
2 | toby | 86 | if(needinput) |
87 | var['c'] = inp[k]; |
||
88 | var['z'] = k; |
||
89 | f = eval(tree[k]); |
||
66 | toby | 90 | outp[k] = f<0 ? 0 : (f>255 ? 255 : f); // clamp channel value to 0-255 |
2 | toby | 91 | } |
92 | } |
||
93 | |||
53 | toby | 94 | /* Zoom and filter image. |
66 | toby | 95 | * Parameters: pb - Photoshop Filter parameter block |
96 | * progress - whether to use Photoshop's progress bar |
||
53 | toby | 97 | * (not appropriate during preview) |
66 | toby | 98 | * filterRect - rectangle (within pb->inRect) |
53 | toby | 99 | * of area to be filtered. This may not correspond |
100 | * to pb->filterRect, it may be just a piece. |
||
66 | toby | 101 | * outPiece - rectangle defining scaled output buffer. |
102 | * In case of zoomed preview, this is physically |
||
103 | * scaled FROM filterRect (or equal to filterRect |
||
104 | * for unscaled 1:1 filtering). |
||
53 | toby | 105 | * outData - pointer to output data buffer |
106 | * outRowBytes - row stride of output data buffer |
||
66 | toby | 107 | * zoom - pixel scale factor (both horiz & vert) |
53 | toby | 108 | * e.g. 2.0 means 1 output pixel per 2 input pixels. |
109 | */ |
||
2 | toby | 110 | |
53 | toby | 111 | OSErr process_scaled(FilterRecordPtr pb, Boolean progress, |
66 | toby | 112 | Rect *filterPiece, Rect *outPiece, |
53 | toby | 113 | void *outData, long outRowBytes, double zoom){ |
2 | toby | 114 | unsigned char *inrow,*outrow,*outp; |
23 | toby | 115 | int j,i; |
2 | toby | 116 | long t,ticks = TICKCOUNT(); |
117 | double x,y; |
||
66 | toby | 118 | |
119 | /* char s[0x100];sprintf(s,"process_scaled: pb->inData=%p outData=%p\n\ |
||
120 | pb->inRect=(%d,%d,%d,%d) filterPiece=(%d,%d,%d,%d) outPiece=(%d,%d,%d,%d)\n\ |
||
121 | pb->fRect=(%d,%d,%d,%d)\n\ |
||
122 | top row offset (from inData) = %d\n\ |
||
123 | left row offset = %d\n", |
||
2 | toby | 124 | pb->inData,outData, |
66 | toby | 125 | pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom, |
126 | filterPiece->left,filterPiece->top,filterPiece->right,filterPiece->bottom, |
||
127 | outPiece->left,outPiece->top,outPiece->right,outPiece->bottom, |
||
128 | pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom, |
||
129 | filterPiece->top - pb->filterRect.top + pb->filterRect.top - pb->inRect.top, |
||
130 | filterPiece->left - pb->filterRect.left + pb->filterRect.left - pb->inRect.left); dbg(s); */ |
||
131 | |||
2 | toby | 132 | if(needinput && !pb->inData){ |
53 | toby | 133 | simplealert("Error (process_scaled: pb->inData == NULL)." |
134 | " This problem is being investigated. Cannot apply the filter;" |
||
135 | " please re-launch Photoshop and try again."); |
||
2 | toby | 136 | return userCanceledErr; |
137 | }else |
||
66 | toby | 138 | // find base pointer to selection image data |
139 | image_ptr = (unsigned char*)pb->inData + (long)pb->inRowBytes*(pb->filterRect.top - pb->inRect.top) |
||
140 | + (long)nplanes*(pb->filterRect.left - pb->inRect.left); |
||
141 | |||
142 | // j indexes scaled output rows |
||
143 | for( j = outPiece->top, outrow = (unsigned char*)outData, y = filterPiece->top - pb->filterRect.top ; |
||
144 | j < outPiece->bottom ; ++j, outrow += outRowBytes, y += zoom ) |
||
53 | toby | 145 | { |
66 | toby | 146 | var['y'] = y; // index of corresponding *input* row, top of selection == 0 |
2 | toby | 147 | inrow = (unsigned char*)pb->inData |
53 | toby | 148 | + ((long)y + pb->filterRect.top - pb->inRect.top)*pb->inRowBytes |
149 | + (long)nplanes*(pb->filterRect.left - pb->inRect.left); |
||
66 | toby | 150 | |
151 | // i indexes scaled output columns |
||
152 | for( outp = outrow, i = outPiece->left, x = filterPiece->left - pb->filterRect.left ; |
||
153 | i < outPiece->right ; ++i, outp += nplanes, x += zoom ) |
||
53 | toby | 154 | { |
66 | toby | 155 | var['x'] = x; // index of corresponding *input* column, left of selection == 0 |
156 | evalpixel(outp,inrow + (long)x*nplanes); /* var['x'] & var['y'] are implicit parameters */ |
||
2 | toby | 157 | } |
158 | |||
66 | toby | 159 | if(progress){ |
160 | if((t = TICKCOUNT()) > ticks){ |
||
161 | ticks = t + TICKS_SEC/4; |
||
162 | if(pb->abortProc()) |
||
163 | return userCanceledErr; |
||
164 | else |
||
165 | pb->progressProc((int)y - pb->filterRect.top,pb->filterRect.bottom - pb->filterRect.top); |
||
166 | } |
||
2 | toby | 167 | } |
168 | #ifdef MAC_ENV |
||
169 | else{ |
||
170 | /* to stop delays during typing of expressions, |
||
171 | immediately abort preview calculation if a key or mouse has been pressed. */ |
||
172 | EventRecord event; |
||
173 | if(EventAvail(mDownMask|keyDownMask|autoKeyMask,&event)) |
||
174 | return userCanceledErr; |
||
175 | } |
||
176 | #endif |
||
177 | } |
||
178 | |||
179 | return noErr; |
||
180 | } |