Rev 105 | Rev 135 | 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 |
102 | toby | 3 | Copyright (C) 2003-9 Toby Thain, toby@telegraphics.com.au |
2 | toby | 4 | |
5 | This program is free software; you can redistribute it and/or modify |
||
103 | toby | 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 | |||
103 | toby | 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 <stdio.h> |
||
21 | //#include <sound.h> |
||
22 | |||
23 | #include "ff.h" |
||
8 | toby | 24 | |
2 | toby | 25 | #include "node.h" |
8 | toby | 26 | #include "y.tab.h" |
2 | toby | 27 | #include "scripting.h" |
28 | |||
29 | struct node *tree[4]; |
||
30 | char *err[4]; |
||
94 | toby | 31 | int errpos[4],errstart[4],nplanes,cnvused,chunksize,toprow; |
2 | toby | 32 | value_type slider[8],cell[0x100],map[4][0x100]; |
89 | toby | 33 | char *expr[4]; |
106 | dmarschall | 34 | // long maxSpace; |
71 | toby | 35 | globals_t *gdata; |
36 | FilterRecordPtr gpb; |
||
2 | toby | 37 | |
38 | #ifdef MAC_ENV |
||
62 | toby | 39 | #define hDllInstance NULL /* fake this Windows-only global */ |
2 | toby | 40 | #endif |
41 | |||
42 | extern struct sym_rec predefs[]; |
||
71 | toby | 43 | extern int nplanes,varused[]; |
2 | toby | 44 | |
15 | toby | 45 | int checkandinitparams(Handle params); |
2 | toby | 46 | |
102 | toby | 47 | // MPW MrC requires prototype |
103 | toby | 48 | DLLEXPORT MACPASCAL |
102 | toby | 49 | void ENTRYPOINT(short selector,FilterRecordPtr pb,intptr_t *data,short *result); |
50 | |||
103 | toby | 51 | DLLEXPORT MACPASCAL |
102 | toby | 52 | void ENTRYPOINT(short selector, FilterRecordPtr pb, intptr_t *data, short *result){ |
87 | toby | 53 | static Boolean wantdialog = false; |
2 | toby | 54 | OSErr e = noErr; |
87 | toby | 55 | char *reason; |
102 | toby | 56 | |
105 | toby | 57 | if(selector != filterSelectorAbout && !*data){ |
102 | toby | 58 | BufferID tempId; |
59 | if( (*result = PS_BUFFER_ALLOC(sizeof(globals_t), &tempId)) ) |
||
60 | return; |
||
103 | toby | 61 | *data = (intptr_t)PS_BUFFER_LOCK(tempId, true); |
62 | gdata = (globals_t*)*data; |
||
2 | toby | 63 | gdata->standalone = gdata->parmloaded = false; |
64 | }else |
||
65 | gdata = (globals_t*)*data; |
||
102 | toby | 66 | |
67 | EnterCodeResource(); |
||
68 | |||
69 | gpb = pb; |
||
70 | |||
2 | toby | 71 | nplanes = MIN(pb->planes,4); |
72 | |||
73 | switch (selector){ |
||
87 | toby | 74 | case filterSelectorAbout: |
102 | toby | 75 | if(gdata && !gdata->parmloaded) |
87 | toby | 76 | gdata->standalone = gdata->parmloaded = readPARMresource(hDllInstance,&reason,1); |
103 | toby | 77 | DoAbout((AboutRecordPtr)pb); |
2 | toby | 78 | break; |
79 | case filterSelectorParameters: |
||
15 | toby | 80 | wantdialog = true; |
2 | toby | 81 | break; |
82 | case filterSelectorPrepare: |
||
83 | DoPrepare(pb); |
||
84 | init_symtab(predefs); // ready for parser calls |
||
85 | break; |
||
89 | toby | 86 | case filterSelectorStart: |
87 | /* initialise the parameter handle that Photoshop keeps for us */ |
||
88 | if(!pb->parameters) |
||
89 | pb->parameters = PINEWHANDLE(0); |
||
2 | toby | 90 | |
15 | toby | 91 | wantdialog |= checkandinitparams(pb->parameters); |
92 | |||
93 | /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */ |
||
2 | toby | 94 | if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){ |
95 | if( maindialog(pb) ){ |
||
15 | toby | 96 | /* update stored parameters from new user settings */ |
2 | toby | 97 | saveparams(pb->parameters); |
98 | }else |
||
99 | e = userCanceledErr; |
||
100 | } |
||
18 | toby | 101 | wantdialog = false; |
2 | toby | 102 | |
103 | if(!e){ |
||
104 | if(setup(pb)){ |
||
105 | DoStart(pb); |
||
106 | }else{ |
||
107 | SYSBEEP(1); |
||
108 | e = filterBadParameters; |
||
109 | } |
||
110 | } |
||
111 | break; |
||
103 | toby | 112 | case filterSelectorContinue: |
113 | e = DoContinue(pb); |
||
2 | toby | 114 | break; |
115 | case filterSelectorFinish: |
||
116 | DoFinish(pb); |
||
117 | break; |
||
118 | default: |
||
119 | e = filterBadParameters; |
||
120 | } |
||
103 | toby | 121 | |
2 | toby | 122 | *result = e; |
123 | |||
124 | ExitCodeResource(); |
||
125 | } |
||
126 | |||
15 | toby | 127 | int checkandinitparams(Handle params){ |
128 | char *reasonstr,*reason; |
||
21 | toby | 129 | int i,f,showdialog; |
103 | toby | 130 | |
23 | toby | 131 | if( (f = !(params && readparams(params,false,&reasonstr))) ){ |
2 | toby | 132 | /* either the parameter handle was uninitialised, |
133 | or the parameter data couldn't be read; set default values */ |
||
134 | |||
89 | toby | 135 | // see if saved parameters exist |
87 | toby | 136 | gdata->standalone = gdata->parmloaded = readPARMresource(hDllInstance,&reason,1); |
89 | toby | 137 | |
87 | toby | 138 | if(!gdata->standalone){ |
21 | toby | 139 | // no saved settings (not standalone) |
85 | toby | 140 | for(i = 0; i < 8; ++i) |
2 | toby | 141 | slider[i] = i*10+100; |
89 | toby | 142 | if(gpb->imageMode == plugInModeRGBColor){ |
143 | expr[0] = my_strdup("r"); |
||
144 | expr[1] = my_strdup("g"); |
||
145 | expr[2] = my_strdup("b"); |
||
146 | expr[3] = my_strdup("a"); |
||
147 | }else{ |
||
148 | expr[0] = my_strdup("c"); |
||
149 | expr[1] = my_strdup("c"); |
||
150 | expr[2] = my_strdup("c"); |
||
151 | expr[3] = my_strdup("c"); |
||
152 | } |
||
2 | toby | 153 | } |
154 | } |
||
103 | toby | 155 | |
89 | toby | 156 | // let scripting system change parameters, if we're scripted; |
103 | toby | 157 | // user may want to force display of dialog during scripting playback |
21 | toby | 158 | showdialog = ReadScriptParamsOnRead(); |
2 | toby | 159 | |
89 | toby | 160 | saveparams(params); |
21 | toby | 161 | return f || showdialog; |
2 | toby | 162 | } |
163 | |||
164 | void DoPrepare(FilterRecordPtr pb){ |
||
165 | int i; |
||
166 | |||
85 | toby | 167 | for(i = 4; i--;){ |
8 | toby | 168 | if(expr[i]||tree[i]) DBG("expr[] or tree[] non-NULL in Prepare!"); |
2 | toby | 169 | expr[i] = NULL; |
170 | tree[i] = NULL; |
||
171 | } |
||
106 | dmarschall | 172 | |
173 | // Commented out by DM, 18 Dec 2018: |
||
174 | // This code did not work on systems with 8 GB RAM: |
||
175 | /* |
||
176 | long space = (pb->maxSpace*9)/10; // don't ask for more than 90% of available memory |
||
177 | |||
103 | toby | 178 | maxSpace = 512L<<10; // this is a wild guess, actually |
179 | if(maxSpace > space) |
||
180 | maxSpace = space; |
||
181 | pb->maxSpace = maxSpace; |
||
106 | dmarschall | 182 | */ |
183 | |||
184 | // New variant: |
||
185 | // TODO: Programmatically test if host supports pb->maxSpace64, and if it does so, use this value instead. |
||
186 | pb->maxSpace = ((double)pb->maxSpace/10)*9; // don't ask for more than 90% of available memory |
||
2 | toby | 187 | } |
188 | |||
189 | void RequestNext(FilterRecordPtr pb,long toprow){ |
||
190 | /* Request next block of the image */ |
||
191 | |||
192 | pb->inLoPlane = pb->outLoPlane = 0; |
||
193 | pb->inHiPlane = pb->outHiPlane = nplanes-1; |
||
194 | |||
66 | toby | 195 | // if any of the formulae involve random access to image pixels, |
89 | toby | 196 | // ask for the entire image |
94 | toby | 197 | if(needall){ |
2 | toby | 198 | SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v); |
199 | }else{ |
||
66 | toby | 200 | // otherwise, process the filtered area, by chunksize parts |
2 | toby | 201 | pb->inRect.left = pb->filterRect.left; |
202 | pb->inRect.right = pb->filterRect.right; |
||
203 | pb->inRect.top = toprow; |
||
204 | pb->inRect.bottom = MIN(toprow + chunksize,pb->filterRect.bottom); |
||
103 | toby | 205 | |
71 | toby | 206 | if(cnvused){ |
207 | // cnv() needs one extra pixel in each direction |
||
208 | if(pb->inRect.left > 0) |
||
209 | --pb->inRect.left; |
||
210 | if(pb->inRect.right < pb->imageSize.h) |
||
211 | ++pb->inRect.right; |
||
212 | if(pb->inRect.top > 0) |
||
213 | --pb->inRect.top; |
||
214 | if(pb->inRect.bottom < pb->imageSize.v) |
||
215 | ++pb->inRect.bottom; |
||
216 | } |
||
2 | toby | 217 | } |
66 | toby | 218 | pb->outRect = pb->filterRect; |
2 | toby | 219 | /* |
94 | toby | 220 | {char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)", |
103 | toby | 221 | needall, |
2 | toby | 222 | pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom, |
223 | pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom);dbg(s);} |
||
224 | */ |
||
225 | } |
||
226 | |||
227 | void DoStart(FilterRecordPtr pb){ |
||
228 | //dbg("DoStart"); |
||
229 | /* if src() or rad() functions are used, random access to the image data is required, |
||
230 | so we must request the entire image in a single chunk. */ |
||
94 | toby | 231 | chunksize = needall ? (pb->filterRect.bottom - pb->filterRect.top) : CHUNK_ROWS; |
2 | toby | 232 | toprow = pb->filterRect.top; |
233 | RequestNext(pb,toprow); |
||
234 | } |
||
235 | |||
236 | OSErr DoContinue(FilterRecordPtr pb){ |
||
237 | OSErr e = noErr; |
||
71 | toby | 238 | Rect fr; |
239 | long outoffset; |
||
2 | toby | 240 | |
94 | toby | 241 | if(needall) |
71 | toby | 242 | fr = pb->filterRect; // filter whole selection at once |
243 | else if(cnvused){ |
||
244 | // we've requested one pixel extra all around |
||
245 | // (see RequestNext()), just for access purposes. But filter |
||
246 | // original selection only. |
||
247 | fr.left = pb->filterRect.left; |
||
248 | fr.right = pb->filterRect.right; |
||
249 | fr.top = toprow; |
||
250 | fr.bottom = MIN(toprow + chunksize,pb->filterRect.bottom); |
||
251 | }else // filter whatever portion we've been given |
||
252 | fr = pb->inRect; |
||
253 | |||
103 | toby | 254 | outoffset = (long)pb->outRowBytes*(fr.top - pb->outRect.top) |
71 | toby | 255 | + (long)nplanes*(fr.left - pb->outRect.left); |
256 | |||
257 | if(!(e = process_scaled(pb, true, &fr, &fr, |
||
62 | toby | 258 | (Ptr)pb->outData+outoffset, pb->outRowBytes, 1.))) |
259 | { |
||
2 | toby | 260 | toprow += chunksize; |
261 | if(toprow < pb->filterRect.bottom) |
||
262 | RequestNext(pb,toprow); |
||
263 | else{ |
||
264 | SETRECT(pb->inRect,0,0,0,0); |
||
265 | pb->outRect = pb->maskRect = pb->inRect; |
||
266 | } |
||
267 | } |
||
268 | return e; |
||
269 | } |
||
270 | |||
271 | void DoFinish(FilterRecordPtr pb){ |
||
272 | int i; |
||
89 | toby | 273 | |
2 | toby | 274 | WriteScriptParamsOnRead(); |
275 | |||
85 | toby | 276 | for(i = 4; i--;){ |
2 | toby | 277 | freetree(tree[i]); |
278 | if(expr[i]) free(expr[i]); |
||
279 | } |
||
280 | } |