Rev 204 | Rev 210 | 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 |
206 | daniel-mar | 4 | Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft |
2 | toby | 5 | |
6 | This program is free software; you can redistribute it and/or modify |
||
103 | toby | 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 | |||
103 | toby | 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 | //#include <stdio.h> |
||
22 | //#include <sound.h> |
||
23 | |||
24 | #include "ff.h" |
||
8 | toby | 25 | |
2 | toby | 26 | #include "node.h" |
172 | dmarschall | 27 | #include "funcs.h" |
8 | toby | 28 | #include "y.tab.h" |
2 | toby | 29 | #include "scripting.h" |
185 | dmarschall | 30 | #include <math.h> |
2 | toby | 31 | |
32 | struct node *tree[4]; |
||
33 | char *err[4]; |
||
94 | toby | 34 | int errpos[4],errstart[4],nplanes,cnvused,chunksize,toprow; |
158 | dmarschall | 35 | value_type slider[8],cell[NUM_CELLS],map[4][0x100]; |
89 | toby | 36 | char *expr[4]; |
106 | dmarschall | 37 | // long maxSpace; |
71 | toby | 38 | globals_t *gdata; |
39 | FilterRecordPtr gpb; |
||
2 | toby | 40 | |
41 | #ifdef MAC_ENV |
||
198 | daniel-mar | 42 | #define HINSTANCE HANDLE |
62 | toby | 43 | #define hDllInstance NULL /* fake this Windows-only global */ |
2 | toby | 44 | #endif |
45 | |||
159 | dmarschall | 46 | #ifdef WIN_ENV |
47 | #include "manifest.h" |
||
48 | #endif |
||
49 | |||
2 | toby | 50 | extern struct sym_rec predefs[]; |
71 | toby | 51 | extern int nplanes,varused[]; |
2 | toby | 52 | |
15 | toby | 53 | int checkandinitparams(Handle params); |
2 | toby | 54 | |
102 | toby | 55 | // MPW MrC requires prototype |
103 | toby | 56 | DLLEXPORT MACPASCAL |
102 | toby | 57 | void ENTRYPOINT(short selector,FilterRecordPtr pb,intptr_t *data,short *result); |
58 | |||
103 | toby | 59 | DLLEXPORT MACPASCAL |
102 | toby | 60 | void ENTRYPOINT(short selector, FilterRecordPtr pb, intptr_t *data, short *result){ |
87 | toby | 61 | static Boolean wantdialog = false; |
2 | toby | 62 | OSErr e = noErr; |
87 | toby | 63 | char *reason; |
102 | toby | 64 | |
158 | dmarschall | 65 | #ifdef WIN_ENV |
66 | // For Windows, we use an activation context to enforce that our Manifest resource will |
||
67 | // be used. This allows us to use Visual Styles, even if the host application does not |
||
68 | // support it. |
||
159 | dmarschall | 69 | ManifestActivationCtx manifestVars; |
70 | BOOL activationContextUsed; |
||
158 | dmarschall | 71 | |
194 | daniel-mar | 72 | activationContextUsed = ActivateManifest((HMODULE)hDllInstance, 1, &manifestVars); |
158 | dmarschall | 73 | #endif |
74 | |||
105 | toby | 75 | if(selector != filterSelectorAbout && !*data){ |
102 | toby | 76 | BufferID tempId; |
194 | daniel-mar | 77 | if( (*result = PS_BUFFER_ALLOC(sizeof(globals_t), &tempId)) ) { |
78 | #ifdef WIN_ENV |
||
79 | if (activationContextUsed) DeactivateManifest(&manifestVars); |
||
80 | #endif |
||
102 | toby | 81 | return; |
194 | daniel-mar | 82 | } |
103 | toby | 83 | *data = (intptr_t)PS_BUFFER_LOCK(tempId, true); |
84 | gdata = (globals_t*)*data; |
||
2 | toby | 85 | gdata->standalone = gdata->parmloaded = false; |
86 | }else |
||
87 | gdata = (globals_t*)*data; |
||
102 | toby | 88 | |
89 | EnterCodeResource(); |
||
90 | |||
91 | gpb = pb; |
||
92 | |||
2 | toby | 93 | nplanes = MIN(pb->planes,4); |
94 | |||
95 | switch (selector){ |
||
87 | toby | 96 | case filterSelectorAbout: |
102 | toby | 97 | if(gdata && !gdata->parmloaded) |
194 | daniel-mar | 98 | gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason,1); |
103 | toby | 99 | DoAbout((AboutRecordPtr)pb); |
2 | toby | 100 | break; |
101 | case filterSelectorParameters: |
||
15 | toby | 102 | wantdialog = true; |
2 | toby | 103 | break; |
104 | case filterSelectorPrepare: |
||
105 | DoPrepare(pb); |
||
106 | init_symtab(predefs); // ready for parser calls |
||
135 | dmarschall | 107 | init_trigtab(); |
2 | toby | 108 | break; |
89 | toby | 109 | case filterSelectorStart: |
110 | /* initialise the parameter handle that Photoshop keeps for us */ |
||
111 | if(!pb->parameters) |
||
167 | dmarschall | 112 | pb->parameters = PINEWHANDLE(1); // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that. |
2 | toby | 113 | |
15 | toby | 114 | wantdialog |= checkandinitparams(pb->parameters); |
115 | |||
116 | /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */ |
||
2 | toby | 117 | if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){ |
118 | if( maindialog(pb) ){ |
||
183 | dmarschall | 119 | if (!host_preserves_parameters()) { |
120 | /* Workaround for GIMP/PSPI, to avoid that formulas vanish when you re-open the main window. |
||
121 | The reason is a bug in PSPI: The host should preserve the value of pb->parameters, which PSPI does not do. |
||
122 | Also, all global variables are unloaded, so the plugin cannot preserve any data. |
||
194 | daniel-mar | 123 | Workaround in FF 1.7: If the host GIMP is detected, then a special mode will be activated. |
183 | dmarschall | 124 | This mode saves the filter data into a temporary file "FilterFoundry.afs" and loads it |
125 | when the window is opened again. */ |
||
126 | // Workaround: Save settings in "FilterFoundry.afs" if the host does not preserve pb->parameters |
||
182 | dmarschall | 127 | char outfilename[255]; |
128 | char* tempdir; |
||
167 | dmarschall | 129 | StandardFileReply sfr; |
130 | sfr.sfGood = true; |
||
131 | sfr.sfReplacing = true; |
||
132 | sfr.sfType = PS_FILTER_FILETYPE; |
||
182 | dmarschall | 133 | |
134 | tempdir = getenv("TMP"); |
||
135 | #ifdef WIN_ENV |
||
136 | if (strlen(tempdir) > 0) strcat(tempdir, "\\"); |
||
137 | #else |
||
138 | if (strlen(tempdir) > 0) strcat(tempdir, "/"); |
||
139 | #endif |
||
183 | dmarschall | 140 | sprintf(outfilename, "%sFilterFoundry.afs", tempdir); |
182 | dmarschall | 141 | |
142 | myc2pstrcpy(sfr.sfFile.name, outfilename); |
||
188 | dmarschall | 143 | #ifdef WIN_ENV |
185 | dmarschall | 144 | sfr.nFileExtension = (WORD)(strlen(outfilename) - strlen(".afs")); |
188 | dmarschall | 145 | #endif |
167 | dmarschall | 146 | sfr.sfScript = 0; // FIXME: is that ok? |
147 | savefile(&sfr); |
||
148 | } |
||
149 | |||
15 | toby | 150 | /* update stored parameters from new user settings */ |
2 | toby | 151 | saveparams(pb->parameters); |
152 | }else |
||
153 | e = userCanceledErr; |
||
154 | } |
||
18 | toby | 155 | wantdialog = false; |
2 | toby | 156 | |
157 | if(!e){ |
||
158 | if(setup(pb)){ |
||
159 | DoStart(pb); |
||
160 | }else{ |
||
161 | SYSBEEP(1); |
||
162 | e = filterBadParameters; |
||
163 | } |
||
164 | } |
||
165 | break; |
||
103 | toby | 166 | case filterSelectorContinue: |
167 | e = DoContinue(pb); |
||
2 | toby | 168 | break; |
169 | case filterSelectorFinish: |
||
170 | DoFinish(pb); |
||
171 | break; |
||
172 | default: |
||
173 | e = filterBadParameters; |
||
174 | } |
||
103 | toby | 175 | |
2 | toby | 176 | *result = e; |
177 | |||
178 | ExitCodeResource(); |
||
158 | dmarschall | 179 | |
180 | #ifdef WIN_ENV |
||
194 | daniel-mar | 181 | if (activationContextUsed) DeactivateManifest(&manifestVars); |
158 | dmarschall | 182 | #endif |
2 | toby | 183 | } |
184 | |||
15 | toby | 185 | int checkandinitparams(Handle params){ |
186 | char *reasonstr,*reason; |
||
204 | daniel-mar | 187 | int i,bUninitializedParams; |
188 | Boolean showdialog; |
||
103 | toby | 189 | |
183 | dmarschall | 190 | if (!host_preserves_parameters()) { |
191 | // Workaround: Load settings in "FilterFoundry.afs" if host does not preserve pb->parameters |
||
182 | dmarschall | 192 | char outfilename[255]; |
193 | char* tempdir; |
||
167 | dmarschall | 194 | StandardFileReply sfr; |
195 | sfr.sfGood = true; |
||
196 | sfr.sfReplacing = true; |
||
197 | sfr.sfType = PS_FILTER_FILETYPE; |
||
182 | dmarschall | 198 | |
199 | tempdir = getenv("TMP"); |
||
200 | #ifdef WIN_ENV |
||
201 | if (strlen(tempdir) > 0) strcat(tempdir, "\\"); |
||
202 | #else |
||
203 | if (strlen(tempdir) > 0) strcat(tempdir, "/"); |
||
204 | #endif |
||
183 | dmarschall | 205 | sprintf(outfilename, "%sFilterFoundry.afs", tempdir); |
182 | dmarschall | 206 | |
207 | myc2pstrcpy(sfr.sfFile.name, outfilename); |
||
188 | dmarschall | 208 | #ifdef WIN_ENV |
185 | dmarschall | 209 | sfr.nFileExtension = (WORD)(strlen(outfilename) - strlen(".afs")); |
188 | dmarschall | 210 | #endif |
167 | dmarschall | 211 | sfr.sfScript = 0; // FIXME: is that ok? |
212 | if (loadfile(&sfr, &reason)) return true; |
||
213 | } |
||
214 | |||
203 | daniel-mar | 215 | if( (bUninitializedParams = !(params && readparams(params,false,&reasonstr))) ){ |
2 | toby | 216 | /* either the parameter handle was uninitialised, |
217 | or the parameter data couldn't be read; set default values */ |
||
218 | |||
89 | toby | 219 | // see if saved parameters exist |
194 | daniel-mar | 220 | gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason,1); |
89 | toby | 221 | |
87 | toby | 222 | if(!gdata->standalone){ |
21 | toby | 223 | // no saved settings (not standalone) |
85 | toby | 224 | for(i = 0; i < 8; ++i) |
2 | toby | 225 | slider[i] = i*10+100; |
146 | dmarschall | 226 | for(i = 0; i < 4; ++i) |
227 | if(expr[i]) |
||
228 | free(expr[i]); |
||
89 | toby | 229 | if(gpb->imageMode == plugInModeRGBColor){ |
198 | daniel-mar | 230 | expr[0] = _strdup("r"); |
231 | expr[1] = _strdup("g"); |
||
232 | expr[2] = _strdup("b"); |
||
233 | expr[3] = _strdup("a"); |
||
89 | toby | 234 | }else{ |
198 | daniel-mar | 235 | expr[0] = _strdup("c"); |
236 | expr[1] = _strdup("c"); |
||
237 | expr[2] = _strdup("c"); |
||
238 | expr[3] = _strdup("c"); |
||
89 | toby | 239 | } |
2 | toby | 240 | } |
241 | } |
||
103 | toby | 242 | |
89 | toby | 243 | // let scripting system change parameters, if we're scripted; |
103 | toby | 244 | // user may want to force display of dialog during scripting playback |
203 | daniel-mar | 245 | switch (ReadScriptParamsOnRead()) { |
246 | case SCR_SHOW_DIALOG: |
||
204 | daniel-mar | 247 | showdialog = true; |
248 | break; |
||
203 | daniel-mar | 249 | case SCR_HIDE_DIALOG: |
204 | daniel-mar | 250 | showdialog = false; |
251 | break; |
||
252 | default: |
||
203 | daniel-mar | 253 | case SCR_NO_SCRIPT: |
204 | daniel-mar | 254 | showdialog = bUninitializedParams; |
255 | break; |
||
203 | daniel-mar | 256 | } |
204 | daniel-mar | 257 | |
258 | saveparams(params); |
||
259 | |||
260 | return showdialog; |
||
2 | toby | 261 | } |
262 | |||
185 | dmarschall | 263 | Boolean host_preserves_parameters() { |
183 | dmarschall | 264 | if (gpb->hostSig == HOSTSIG_GIMP) return false; |
265 | if (gpb->hostSig == HOSTSIG_IRFANVIEW) return false; |
||
266 | |||
267 | /* |
||
268 | char x[100]; |
||
269 | sprintf(x, "Host Signature: %u", gpb->hostSig); |
||
270 | simplealert(x); |
||
271 | */ |
||
272 | |||
273 | // We just assume the other hosts preserve the parameters |
||
274 | return true; |
||
275 | } |
||
276 | |||
182 | dmarschall | 277 | int64_t maxspace(){ |
181 | dmarschall | 278 | if (gpb->maxSpace64 != 0) { |
279 | // If this is non-zero, the host either support 64-bit OR the host ignored the rule "set reserved fields to 0". |
||
280 | uint64_t maxSpace64 = gpb->maxSpace64; |
||
183 | dmarschall | 281 | |
282 | /* |
||
283 | char x[100]; |
||
284 | sprintf(x, "maxSpace64: %lld", maxSpace64); |
||
285 | simplealert(x); |
||
286 | */ |
||
287 | |||
288 | // Note: IrfanView currently does not support maxSpace64, so we don't handle HOSTSIG_IRFANVIEW |
||
181 | dmarschall | 289 | return maxSpace64; |
290 | } else { |
||
291 | // Note: If maxSpace gets converted from Int32 to unsigned int, we can reach up to 4 GB RAM. However, after this, there will be a wrap to 0 GB again. |
||
292 | unsigned int maxSpace32 = (unsigned int) gpb->maxSpace; |
||
293 | uint64_t maxSpace64 = maxSpace32; |
||
183 | dmarschall | 294 | |
295 | /* |
||
296 | char x[100]; |
||
297 | sprintf(x, "maxSpace32: %lld", maxSpace64); |
||
298 | simplealert(x); |
||
299 | */ |
||
300 | |||
301 | if (gpb->hostSig == HOSTSIG_IRFANVIEW) maxSpace64 *= 1024; // IrfanView is giving Kilobytes instead of Bytes |
||
190 | dmarschall | 302 | // TODO: Serif PhotoPlus also gives Kilobytes instead of bytes. But since it uses not a unique host signature, there is nothing we can do??? |
181 | dmarschall | 303 | return maxSpace64; |
304 | } |
||
305 | } |
||
306 | |||
185 | dmarschall | 307 | Boolean maxspace_available() { |
182 | dmarschall | 308 | // GIMP sets MaxSpace to hardcoded 100 MB |
183 | dmarschall | 309 | if (gpb->hostSig == HOSTSIG_GIMP) return false; |
182 | dmarschall | 310 | |
311 | return true; |
||
312 | } |
||
313 | |||
2 | toby | 314 | void DoPrepare(FilterRecordPtr pb){ |
315 | int i; |
||
316 | |||
85 | toby | 317 | for(i = 4; i--;){ |
8 | toby | 318 | if(expr[i]||tree[i]) DBG("expr[] or tree[] non-NULL in Prepare!"); |
2 | toby | 319 | expr[i] = NULL; |
320 | tree[i] = NULL; |
||
321 | } |
||
106 | dmarschall | 322 | |
323 | // Commented out by DM, 18 Dec 2018: |
||
324 | // This code did not work on systems with 8 GB RAM: |
||
325 | /* |
||
326 | long space = (pb->maxSpace*9)/10; // don't ask for more than 90% of available memory |
||
327 | |||
103 | toby | 328 | maxSpace = 512L<<10; // this is a wild guess, actually |
329 | if(maxSpace > space) |
||
330 | maxSpace = space; |
||
331 | pb->maxSpace = maxSpace; |
||
106 | dmarschall | 332 | */ |
333 | |||
334 | // New variant: |
||
182 | dmarschall | 335 | if (maxspace_available()) { |
185 | dmarschall | 336 | pb->maxSpace = (int32)ceil((maxspace()/10.)*9); // don't ask for more than 90% of available memory |
337 | // FIXME: Also maxSpace64 |
||
182 | dmarschall | 338 | } |
2 | toby | 339 | } |
340 | |||
341 | void RequestNext(FilterRecordPtr pb,long toprow){ |
||
342 | /* Request next block of the image */ |
||
343 | |||
344 | pb->inLoPlane = pb->outLoPlane = 0; |
||
345 | pb->inHiPlane = pb->outHiPlane = nplanes-1; |
||
346 | |||
66 | toby | 347 | // if any of the formulae involve random access to image pixels, |
89 | toby | 348 | // ask for the entire image |
94 | toby | 349 | if(needall){ |
2 | toby | 350 | SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v); |
351 | }else{ |
||
164 | dmarschall | 352 | // TODO: This does not work with GIMP. So, if we are using GIMP, we should |
353 | // somehow always use "needall=true", and/or find out why this doesn't work |
||
354 | // with GIMP. |
||
355 | |||
66 | toby | 356 | // otherwise, process the filtered area, by chunksize parts |
2 | toby | 357 | pb->inRect.left = pb->filterRect.left; |
358 | pb->inRect.right = pb->filterRect.right; |
||
185 | dmarschall | 359 | pb->inRect.top = (int16)toprow; |
360 | pb->inRect.bottom = (int16)MIN(toprow + chunksize,pb->filterRect.bottom); |
||
103 | toby | 361 | |
71 | toby | 362 | if(cnvused){ |
363 | // cnv() needs one extra pixel in each direction |
||
364 | if(pb->inRect.left > 0) |
||
365 | --pb->inRect.left; |
||
366 | if(pb->inRect.right < pb->imageSize.h) |
||
367 | ++pb->inRect.right; |
||
368 | if(pb->inRect.top > 0) |
||
369 | --pb->inRect.top; |
||
370 | if(pb->inRect.bottom < pb->imageSize.v) |
||
371 | ++pb->inRect.bottom; |
||
372 | } |
||
2 | toby | 373 | } |
66 | toby | 374 | pb->outRect = pb->filterRect; |
2 | toby | 375 | /* |
94 | toby | 376 | {char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)", |
103 | toby | 377 | needall, |
2 | toby | 378 | pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom, |
379 | pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom);dbg(s);} |
||
380 | */ |
||
381 | } |
||
382 | |||
383 | void DoStart(FilterRecordPtr pb){ |
||
384 | //dbg("DoStart"); |
||
385 | /* if src() or rad() functions are used, random access to the image data is required, |
||
386 | so we must request the entire image in a single chunk. */ |
||
94 | toby | 387 | chunksize = needall ? (pb->filterRect.bottom - pb->filterRect.top) : CHUNK_ROWS; |
2 | toby | 388 | toprow = pb->filterRect.top; |
389 | RequestNext(pb,toprow); |
||
390 | } |
||
391 | |||
392 | OSErr DoContinue(FilterRecordPtr pb){ |
||
393 | OSErr e = noErr; |
||
71 | toby | 394 | Rect fr; |
395 | long outoffset; |
||
2 | toby | 396 | |
94 | toby | 397 | if(needall) |
71 | toby | 398 | fr = pb->filterRect; // filter whole selection at once |
399 | else if(cnvused){ |
||
400 | // we've requested one pixel extra all around |
||
401 | // (see RequestNext()), just for access purposes. But filter |
||
402 | // original selection only. |
||
403 | fr.left = pb->filterRect.left; |
||
404 | fr.right = pb->filterRect.right; |
||
405 | fr.top = toprow; |
||
406 | fr.bottom = MIN(toprow + chunksize,pb->filterRect.bottom); |
||
407 | }else // filter whatever portion we've been given |
||
408 | fr = pb->inRect; |
||
409 | |||
103 | toby | 410 | outoffset = (long)pb->outRowBytes*(fr.top - pb->outRect.top) |
71 | toby | 411 | + (long)nplanes*(fr.left - pb->outRect.left); |
412 | |||
413 | if(!(e = process_scaled(pb, true, &fr, &fr, |
||
62 | toby | 414 | (Ptr)pb->outData+outoffset, pb->outRowBytes, 1.))) |
415 | { |
||
2 | toby | 416 | toprow += chunksize; |
417 | if(toprow < pb->filterRect.bottom) |
||
418 | RequestNext(pb,toprow); |
||
419 | else{ |
||
420 | SETRECT(pb->inRect,0,0,0,0); |
||
421 | pb->outRect = pb->maskRect = pb->inRect; |
||
422 | } |
||
423 | } |
||
424 | return e; |
||
425 | } |
||
426 | |||
427 | void DoFinish(FilterRecordPtr pb){ |
||
428 | int i; |
||
89 | toby | 429 | |
2 | toby | 430 | WriteScriptParamsOnRead(); |
431 | |||
85 | toby | 432 | for(i = 4; i--;){ |
2 | toby | 433 | freetree(tree[i]); |
434 | if(expr[i]) free(expr[i]); |
||
435 | } |
||
436 | } |