Rev 276 | Rev 284 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
259 | daniel-mar | 1 | /* |
268 | daniel-mar | 2 | This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop |
259 | daniel-mar | 3 | Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au |
4 | Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft |
||
5 | |||
6 | This program is free software; you can redistribute it and/or modify |
||
7 | it under the terms of the GNU General Public License as published by |
||
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 | |||
16 | You should have received a copy of the GNU General Public License |
||
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" |
||
25 | |||
26 | #include "node.h" |
||
27 | #include "funcs.h" |
||
28 | #include "y.tab.h" |
||
29 | #include "scripting.h" |
||
30 | #include <math.h> |
||
31 | #include "PIBufferSuite.h" |
||
32 | |||
33 | // GIMP (PSPI) and IrfanView preserve neither *data(gdata), nor pb->parameters between invocations! |
||
34 | // For debugging, we can simulate it here |
||
35 | //#define DEBUG_SIMULATE_GIMP |
||
36 | |||
264 | daniel-mar | 37 | // Used to find out which host signatures and memory settings a plugin host has |
38 | //#define SHOW_HOST_DEBUG |
||
39 | |||
259 | daniel-mar | 40 | struct node *tree[4]; |
41 | char *err[4]; |
||
42 | int errpos[4],errstart[4],nplanes,cnvused,chunksize,toprow; |
||
43 | value_type slider[8],cell[NUM_CELLS],map[4][0x100]; |
||
44 | char *expr[4]; |
||
45 | // long maxSpace; |
||
46 | globals_t *gdata; |
||
47 | FilterRecordPtr gpb; |
||
48 | |||
49 | #ifdef MAC_ENV |
||
50 | #define HINSTANCE HANDLE |
||
51 | #define hDllInstance NULL /* fake this Windows-only global */ |
||
52 | #endif |
||
53 | |||
54 | #ifdef WIN_ENV |
||
55 | #include "manifest.h" |
||
56 | #endif |
||
57 | |||
58 | extern struct sym_rec predefs[]; |
||
59 | extern int nplanes,varused[]; |
||
60 | |||
61 | int checkandinitparams(Handle params); |
||
62 | |||
63 | // MPW MrC requires prototype |
||
64 | DLLEXPORT MACPASCAL |
||
65 | void ENTRYPOINT(short selector,FilterRecordPtr pb,intptr_t *data,short *result); |
||
66 | |||
276 | daniel-mar | 67 | unsigned long get_parm_hash(PARM_T *parm) { |
259 | daniel-mar | 68 | unsigned long hash; |
69 | int i; |
||
70 | |||
276 | daniel-mar | 71 | hash = djb2((char*)parm->category); |
72 | hash += djb2((char*)parm->title); |
||
73 | hash += djb2((char*)parm->copyright); |
||
74 | hash += djb2((char*)parm->author); |
||
75 | for (i = 0; i < 4; i++) hash += hash += djb2((char*)parm->map[i]); |
||
76 | for (i = 0; i < 8; i++) hash += hash += djb2((char*)parm->ctl[i]); |
||
77 | for (i = 0; i < 4; i++) hash += hash += djb2((char*)parm->formula[i]); |
||
259 | daniel-mar | 78 | |
79 | return hash; |
||
80 | } |
||
81 | |||
82 | DLLEXPORT MACPASCAL |
||
83 | void ENTRYPOINT(short selector, FilterRecordPtr pb, intptr_t *data, short *result){ |
||
84 | static Boolean wantdialog = false; |
||
85 | static Boolean premiereWarnedOnce = false; |
||
86 | OSErr e = noErr; |
||
87 | char *reason; |
||
268 | daniel-mar | 88 | |
89 | #ifdef WIN_ENV |
||
259 | daniel-mar | 90 | // For Windows, we use an activation context to enforce that our Manifest resource will |
91 | // be used. This allows us to use Visual Styles, even if the host application does not |
||
92 | // support it. |
||
93 | ManifestActivationCtx manifestVars; |
||
94 | BOOL activationContextUsed; |
||
268 | daniel-mar | 95 | #endif |
259 | daniel-mar | 96 | |
283 | daniel-mar | 97 | #ifdef WIN_ENV |
98 | // The first 64KB of address space is always invalid |
||
99 | if ((intptr_t)result <= 0xffff) { |
||
100 | // When the 8BF file is analyzed with VirusTotal.com, it will invoke each |
||
101 | // exported function by calling |
||
102 | // C:\Windows\System32\rundll32.exe rundll32.exe FilterFoundry.8bf,PluginMain |
||
103 | // But RunDLL32 requires following signature: |
||
104 | // void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow); |
||
105 | // Obviously, this will cause an Exception. (It crashes at *result=e because result is 0xA) |
||
106 | // Here is the problem: The crash will be handled by WerFault.exe inside the |
||
107 | // VirusTotal virtual machine. WerFault connects to various servers (9 DNS resolutions!) and does |
||
108 | // a lot of weird things, but VirusTotal thinks that our plugin does all that stuff, |
||
109 | // and so they mark our plugin as "malware"! |
||
110 | // This is a problem with VirusTotal! It shall not assume that WerFault.exe actions are our actions! |
||
111 | // Even processes like "MicrosoftEdgeUpdate.exe" and "SpeechRuntime.exe" are reported to be our |
||
112 | // actions, although they have nothing to do with us! |
||
113 | // See https://www.virustotal.com/gui/file/1f1012c567208186be455b81afc1ee407ae6476c197d633c70cc70929113223a/behavior |
||
114 | // |
||
115 | // TODO: Not 100% sure if the calling convention is correct... |
||
116 | // are we corrupting the stack? At least WER isn't triggered... |
||
117 | return; |
||
118 | } |
||
119 | #endif |
||
120 | |||
259 | daniel-mar | 121 | EnterCodeResource(); |
122 | |||
264 | daniel-mar | 123 | #ifdef SHOW_HOST_DEBUG |
259 | daniel-mar | 124 | char* s = (char*)malloc(512); |
125 | sprintf(s, "Host signature: '%c%c%c%c' (%d)\nMaxSpace32 = %d\nMaxSpace64 = %lld\nNum buffer procs: %d", (pb->hostSig >> 24) & 0xFF, (pb->hostSig >> 16) & 0xFF, (pb->hostSig >> 8) & 0xFF, pb->hostSig & 0xFF, pb->hostSig, pb->maxSpace, pb->maxSpace64, pb->bufferProcs->numBufferProcs); |
||
126 | simplealert(s); |
||
264 | daniel-mar | 127 | #endif |
259 | daniel-mar | 128 | |
129 | if (pb->hostSig == HOSTSIG_ADOBE_PREMIERE) { |
||
130 | // DM 19.07.2021 : Tried running the 8BF file in Adobe Premiere 5 (yes, that's possible, |
||
131 | // and there is even a FilterFactory for Premeire!), |
||
132 | // but it crashes in evalpixel() where there is write-access to the "outp". |
||
133 | // Probably the canvas structure is different (maybe it contains frames to achieve transitions?) |
||
134 | if (!premiereWarnedOnce) { |
||
135 | simplealert(_strdup("This version of Filter Foundry is not compatible with Adobe Premiere!")); |
||
136 | } |
||
137 | premiereWarnedOnce = true; |
||
138 | *result = errPlugInHostInsufficient; |
||
139 | return; |
||
140 | } |
||
141 | |||
142 | #ifdef DEBUG_SIMULATE_GIMP |
||
143 | *data = 0; |
||
144 | pb->parameters = pb->handleProcs->newProc(1); |
||
145 | #endif |
||
146 | |||
147 | // Register "gdata" that contains the PARM information and other things which need to be persistant |
||
148 | // and preserve then in *data |
||
149 | // TODO: memory leak? where is the stuff freed? |
||
150 | if (selector != filterSelectorAbout && !*data) { |
||
151 | /* |
||
152 | PSBufferSuite1* pSBufferSuite32 = NULL; |
||
153 | |||
154 | if ((pb->sSPBasic == 0) || |
||
155 | (pb->sSPBasic->AcquireSuite(kPSBufferSuite, kPSBufferSuiteVersion1, (const void**)&pSBufferSuite32)) || |
||
156 | (pSBufferSuite32 == NULL)) |
||
157 | { |
||
158 | // Old deprecated buffer suite |
||
159 | BufferID tempId; |
||
160 | if ((*result = pb->bufferProcs->allocateProc(sizeof(globals_t), &tempId))) return; |
||
161 | *data = (intptr_t)pb->bufferProcs->lockProc(tempId, true); |
||
162 | gdata = (globals_t*)*data; |
||
163 | } |
||
164 | else |
||
165 | { |
||
166 | // New buffer suite (but only 32-bit version 1, because version 2 has problems with old Photoshop versions) |
||
167 | // Windows Photoshop 7 and CS 2 accepts kPSBufferSuiteVersion2, but doesn't correctly implement it: |
||
168 | // The symbols "New" and "GetSpace64" point to memory memory addresses outside the Photoshop.exe address range. |
||
169 | // (Other Photoshop versions were not tested.) |
||
170 | // 64-bit support for Windows was established in Photoshop CS 4, |
||
171 | // and PSBufferSuite2 was first documented in SDK CS 6. |
||
172 | // So, kPSBufferSuiteVersion2 probably was partically implemented as hidden "Work in progress" version |
||
173 | // before it was publicly documented. |
||
174 | // Side note: pb->bufferSpace64/pb->maxSpace64 was documented in SDK CC 2017. |
||
175 | // pb->bufferProcs->allocateProc64/spaceProc64 was documented in SDK CS 6. |
||
176 | unsigned32 siz = sizeof(globals_t); |
||
177 | *data = (intptr_t)pSBufferSuite32->New(&siz, siz); |
||
178 | if ((*data == 0) || (siz == 0)) { |
||
179 | *result = errPlugInHostInsufficient; // TODO: what is the correct error code for "out of memory"? |
||
180 | return; |
||
181 | } |
||
182 | gdata = (globals_t*)*data; |
||
183 | pb->sSPBasic->ReleaseSuite(kPSBufferSuite, kPSBufferSuiteVersion1); |
||
184 | } |
||
185 | gdata->standalone = gdata->parmloaded = false; |
||
186 | */ |
||
187 | |||
188 | // We have 3 options: |
||
189 | // - The deprecated buffer suite (pb->bufferProcs), works fine |
||
190 | // - The recommended buffer suite (kPSBufferSuite), does NOT work (causes memory corruption?) and is not available on some hosts! |
||
191 | // Either I do something wrong, or maybe it cannot be used to store data between invocations? |
||
192 | // - Using malloc(), which works also fine and is more independent from the host and easier |
||
193 | *data = (intptr_t)malloc(sizeof(globals_t)); |
||
194 | if (*data == 0) return; |
||
195 | gdata = (globals_t*)*data; |
||
196 | gdata->standalone = gdata->parmloaded = false; // they will be set later |
||
197 | } |
||
198 | else { |
||
199 | // We have data from the previous invocation. Use it instead |
||
200 | gdata = (globals_t*)*data; |
||
201 | } |
||
202 | |||
203 | #ifdef WIN_ENV |
||
204 | activationContextUsed = ActivateManifest((HMODULE)hDllInstance, 1, &manifestVars); |
||
205 | #endif |
||
206 | |||
207 | gpb = pb; |
||
208 | |||
209 | nplanes = MIN(pb->planes,4); |
||
210 | |||
211 | switch (selector){ |
||
212 | case filterSelectorAbout: |
||
213 | if (!gdata) { |
||
214 | gdata = (globals_t*)malloc(sizeof(globals_t)); |
||
215 | if (!gdata) break; |
||
216 | gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason,READ_OBFUSC); |
||
217 | DoAbout((AboutRecordPtr)pb); |
||
218 | free(gdata); |
||
219 | gdata = NULL; |
||
220 | } else { |
||
221 | DoAbout((AboutRecordPtr)pb); |
||
222 | } |
||
223 | break; |
||
224 | case filterSelectorParameters: |
||
225 | wantdialog = true; |
||
226 | break; |
||
227 | case filterSelectorPrepare: |
||
228 | DoPrepare(pb); |
||
229 | init_symtab(predefs); // ready for parser calls |
||
230 | init_trigtab(); |
||
231 | break; |
||
232 | case filterSelectorStart: |
||
233 | if (HAS_BIG_DOC(pb)) { |
||
234 | // The BigDocument structure is required if the document is larger than 30,000 pixels |
||
235 | // It deprecates imageSize, filterRect, inRect, outRect, maskRect, floatCoord, and wholeSize. |
||
236 | // By setting it to nonzero, we communicate to Photoshop that we support the BigDocument structure. |
||
237 | pb->bigDocumentData->PluginUsing32BitCoordinates = true; |
||
238 | } |
||
239 | |||
240 | /* initialise the parameter handle that Photoshop keeps for us */ |
||
241 | if(!pb->parameters) |
||
242 | pb->parameters = PINEWHANDLE(1); // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that. |
||
243 | |||
244 | wantdialog |= checkandinitparams(pb->parameters); |
||
245 | |||
246 | /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */ |
||
247 | if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){ |
||
248 | if( maindialog(pb) ){ |
||
249 | if (!host_preserves_parameters()) { |
||
264 | daniel-mar | 250 | /* Workaround for GIMP/PSPI, to avoid that formulas vanish when you re-open the main window. |
251 | The reason is a bug in PSPI: The host should preserve the value of pb->parameters, which PSPI does not do. |
||
252 | Also, all global variables are unloaded, so the plugin cannot preserve any data. |
||
253 | Workaround in FF 1.7: If the host GIMP is detected, then a special mode will be activated. |
||
254 | This mode saves the filter data into a temporary file "FilterFoundryXX.afs" and loads it |
||
255 | when the window is opened again. */ |
||
256 | // Workaround: Save settings in "FilterFoundryXX.afs" if the host does not preserve pb->parameters |
||
257 | char outfilename[255]; |
||
258 | char* tempdir; |
||
259 | int hash; |
||
260 | StandardFileReply sfr; |
||
261 | char* bakexpr[4]; |
||
259 | daniel-mar | 262 | |
264 | daniel-mar | 263 | sfr.sfGood = true; |
264 | sfr.sfReplacing = true; |
||
265 | sfr.sfType = PS_FILTER_FILETYPE; |
||
259 | daniel-mar | 266 | |
264 | daniel-mar | 267 | tempdir = getenv("TMP"); |
268 | #ifdef WIN_ENV |
||
269 | if (strlen(tempdir) > 0) strcat(tempdir, "\\"); |
||
270 | #else |
||
271 | if (strlen(tempdir) > 0) strcat(tempdir, "/"); |
||
272 | #endif |
||
259 | daniel-mar | 273 | |
276 | daniel-mar | 274 | hash = (gdata->standalone) ? get_parm_hash(&gdata->parm) : 0; |
264 | daniel-mar | 275 | sprintf(outfilename, "%sFilterFoundry%d.afs", tempdir, hash); |
276 | |||
277 | myc2pstrcpy(sfr.sfFile.name, outfilename); |
||
278 | #ifdef WIN_ENV |
||
279 | sfr.nFileExtension = (WORD)(strlen(outfilename) - strlen(".afs") + 1); |
||
280 | #endif |
||
281 | sfr.sfScript = 0; // FIXME: is that ok? |
||
282 | |||
283 | // We only want the parameters (ctl,map) in the temporary .afs file |
||
284 | // It is important to remove the expressions, otherwise they would be |
||
285 | // revealed in the temporary files. |
||
272 | daniel-mar | 286 | bakexpr[0] = expr[0]; // moved out of the if-definition to make the compiler happy |
287 | bakexpr[1] = expr[1]; |
||
288 | bakexpr[2] = expr[2]; |
||
289 | bakexpr[3] = expr[3]; |
||
264 | daniel-mar | 290 | if (gdata->standalone) { |
291 | expr[0] = _strdup("r"); |
||
292 | expr[1] = _strdup("g"); |
||
293 | expr[2] = _strdup("b"); |
||
294 | expr[3] = _strdup("a"); |
||
259 | daniel-mar | 295 | } |
264 | daniel-mar | 296 | |
297 | savefile(&sfr); |
||
298 | |||
299 | if (gdata->standalone) { |
||
300 | free(expr[0]); expr[0] = bakexpr[0]; |
||
301 | free(expr[1]); expr[1] = bakexpr[1]; |
||
302 | free(expr[2]); expr[2] = bakexpr[2]; |
||
303 | free(expr[3]); expr[3] = bakexpr[3]; |
||
304 | } |
||
305 | } else { |
||
306 | /* update stored parameters from new user settings */ |
||
307 | saveparams(pb->parameters); |
||
259 | daniel-mar | 308 | } |
309 | }else |
||
310 | e = userCanceledErr; |
||
311 | } |
||
312 | wantdialog = false; |
||
313 | |||
314 | if(!e){ |
||
315 | if(setup(pb)){ |
||
316 | DoStart(pb); |
||
317 | }else{ |
||
318 | SYSBEEP(1); |
||
319 | e = filterBadParameters; |
||
320 | } |
||
321 | } |
||
322 | break; |
||
323 | case filterSelectorContinue: |
||
324 | e = DoContinue(pb); |
||
325 | break; |
||
326 | case filterSelectorFinish: |
||
327 | DoFinish(pb); |
||
328 | break; |
||
329 | default: |
||
330 | e = filterBadParameters; |
||
331 | } |
||
332 | |||
333 | *result = e; |
||
334 | |||
335 | #ifdef WIN_ENV |
||
336 | if (activationContextUsed) DeactivateManifest(&manifestVars); |
||
337 | #endif |
||
338 | |||
339 | ExitCodeResource(); |
||
340 | } |
||
341 | |||
342 | int checkandinitparams(Handle params){ |
||
343 | char *reasonstr,*reason; |
||
344 | int i,bUninitializedParams; |
||
345 | Boolean showdialog; |
||
346 | |||
347 | if (!host_preserves_parameters()) { |
||
348 | // Workaround: Load settings in "FilterFoundryXX.afs" if host does not preserve pb->parameters |
||
349 | char outfilename[255]; |
||
350 | char* tempdir; |
||
351 | int hash; |
||
352 | Boolean isStandalone; |
||
353 | StandardFileReply sfr; |
||
264 | daniel-mar | 354 | char* bakexpr[4]; |
355 | |||
259 | daniel-mar | 356 | sfr.sfGood = true; |
357 | sfr.sfReplacing = true; |
||
358 | sfr.sfType = PS_FILTER_FILETYPE; |
||
359 | |||
360 | // We need to set gdata->standalone after loadfile(), but we must call readPARMresource() before loadfile() |
||
361 | // Reason: readPARMresource() reads parameters from the DLL while loadfile() reads parameters from the AFS file |
||
362 | // But loadfile() will reset gdata->standalone ... |
||
363 | isStandalone = readPARMresource((HMODULE)hDllInstance, &reason, READ_OBFUSC); |
||
364 | |||
264 | daniel-mar | 365 | tempdir = getenv("TMP"); |
366 | #ifdef WIN_ENV |
||
367 | if (strlen(tempdir) > 0) strcat(tempdir, "\\"); |
||
368 | #else |
||
369 | if (strlen(tempdir) > 0) strcat(tempdir, "/"); |
||
370 | #endif |
||
259 | daniel-mar | 371 | |
276 | daniel-mar | 372 | hash = (isStandalone) ? get_parm_hash(&gdata->parm) : 0; |
264 | daniel-mar | 373 | sprintf(outfilename, "%sFilterFoundry%d.afs", tempdir, hash); |
259 | daniel-mar | 374 | |
264 | daniel-mar | 375 | myc2pstrcpy(sfr.sfFile.name, outfilename); |
376 | #ifdef WIN_ENV |
||
377 | sfr.nFileExtension = (WORD)(strlen(outfilename) - strlen(".afs") + 1); |
||
378 | #endif |
||
379 | sfr.sfScript = 0; // FIXME: is that ok? |
||
259 | daniel-mar | 380 | |
264 | daniel-mar | 381 | // We only want the parameters (ctl,map) in the temporary .afs file |
382 | if (isStandalone) { |
||
383 | bakexpr[0] = my_strdup(expr[0]); |
||
384 | bakexpr[1] = my_strdup(expr[1]); |
||
385 | bakexpr[2] = my_strdup(expr[2]); |
||
386 | bakexpr[3] = my_strdup(expr[3]); |
||
387 | } |
||
388 | |||
389 | if (loadfile(&sfr, &reason)) { |
||
390 | gdata->standalone = gdata->parmloaded = isStandalone; |
||
391 | |||
392 | if (isStandalone) { |
||
393 | free(expr[0]); expr[0] = bakexpr[0]; |
||
394 | free(expr[1]); expr[1] = bakexpr[1]; |
||
395 | free(expr[2]); expr[2] = bakexpr[2]; |
||
396 | free(expr[3]); expr[3] = bakexpr[3]; |
||
259 | daniel-mar | 397 | } |
264 | daniel-mar | 398 | |
399 | return true; |
||
259 | daniel-mar | 400 | } |
401 | } |
||
402 | |||
403 | if( (bUninitializedParams = !(params && readparams(params,false,&reasonstr))) ){ |
||
404 | /* either the parameter handle was uninitialised, |
||
405 | or the parameter data couldn't be read; set default values */ |
||
406 | |||
407 | // see if saved parameters exist |
||
408 | gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason,READ_OBFUSC); |
||
409 | |||
410 | |||
411 | if(!gdata->standalone){ |
||
412 | // no saved settings (not standalone) |
||
413 | for(i = 0; i < 8; ++i) |
||
414 | slider[i] = i*10+100; |
||
415 | for(i = 0; i < 4; ++i) |
||
416 | if(expr[i]) |
||
417 | free(expr[i]); |
||
418 | if(gpb->imageMode == plugInModeRGBColor){ |
||
419 | expr[0] = _strdup("r"); |
||
420 | expr[1] = _strdup("g"); |
||
421 | expr[2] = _strdup("b"); |
||
422 | expr[3] = _strdup("a"); |
||
423 | }else{ |
||
424 | expr[0] = _strdup("c"); |
||
425 | expr[1] = _strdup("c"); |
||
426 | expr[2] = _strdup("c"); |
||
427 | expr[3] = _strdup("c"); |
||
428 | } |
||
429 | } |
||
430 | } |
||
431 | |||
432 | // let scripting system change parameters, if we're scripted; |
||
433 | // user may want to force display of dialog during scripting playback |
||
434 | switch (ReadScriptParamsOnRead()) { |
||
435 | case SCR_SHOW_DIALOG: |
||
436 | showdialog = true; |
||
437 | break; |
||
438 | case SCR_HIDE_DIALOG: |
||
439 | showdialog = false; |
||
440 | break; |
||
441 | default: |
||
442 | case SCR_NO_SCRIPT: |
||
443 | showdialog = bUninitializedParams; |
||
444 | break; |
||
445 | } |
||
446 | |||
447 | saveparams(params); |
||
448 | |||
449 | return showdialog; |
||
450 | } |
||
451 | |||
452 | Boolean host_preserves_parameters() { |
||
453 | #ifdef DEBUG_SIMULATE_GIMP |
||
454 | return false; |
||
455 | #endif |
||
456 | |||
457 | if (gpb->hostSig == HOSTSIG_GIMP) return false; |
||
458 | if (gpb->hostSig == HOSTSIG_IRFANVIEW) return false; |
||
459 | |||
460 | // We just assume the other hosts preserve the parameters |
||
461 | return true; |
||
462 | } |
||
463 | |||
464 | int64_t maxspace(){ |
||
465 | // Please see "Hosts.md" for details about the MaxSpace implementations of tested plugins |
||
466 | |||
467 | // Plugins that don't support MaxSpace64 shall set the field to zero; then we will use MaxSpace instead. |
||
468 | // Also check "gpb->bufferProcs->numBufferProcs" to see if 64 bit API is available |
||
469 | if ((gpb->bufferProcs->numBufferProcs >= 8) && (gpb->maxSpace64 > 0)) { |
||
470 | uint64_t maxSpace64 = gpb->maxSpace64; |
||
471 | |||
472 | return maxSpace64; |
||
473 | } else { |
||
474 | // 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. |
||
475 | unsigned int maxSpace32 = (unsigned int) gpb->maxSpace; |
||
476 | uint64_t maxSpace64 = maxSpace32; |
||
477 | |||
478 | if (gpb->hostSig == HOSTSIG_IRFANVIEW) maxSpace64 *= 1024; // IrfanView is giving Kilobytes instead of Bytes |
||
479 | //if (gpb->hostSig == HOSTSIG_SERIF_PHOTOPLUS) maxSpace64 *= 1024; // TODO: Serif PhotoPlus also gives Kilobytes instead of bytes. But since it uses not a unique host signature, there is nothing we can do??? |
||
480 | |||
481 | return maxSpace64; |
||
482 | } |
||
483 | } |
||
484 | |||
485 | Boolean maxspace_available() { |
||
486 | // Please see "Hosts.md" for details about the MaxSpace implementations of tested plugins |
||
487 | |||
488 | // GIMP PSPI sets MaxSpace to hardcoded 100 MB |
||
489 | if (gpb->hostSig == HOSTSIG_GIMP) return false; |
||
490 | |||
491 | // HOSTSIG_PAINT_NET sets MaxSpace to hardcoded 1 GB, see https://github.com/0xC0000054/PSFilterPdn/issues/5 |
||
492 | // Comment by the host author "This was done to avoid any compatibility issues with plugins handling 2 GB - 1" |
||
493 | if (gpb->hostSig == HOSTSIG_PAINT_NET) return false; |
||
494 | |||
495 | return true; |
||
496 | } |
||
497 | |||
498 | void DoPrepare(FilterRecordPtr pb){ |
||
499 | int i; |
||
500 | |||
501 | for(i = 4; i--;){ |
||
502 | if(expr[i]||tree[i]) DBG("expr[] or tree[] non-NULL in Prepare!"); |
||
503 | expr[i] = NULL; |
||
504 | tree[i] = NULL; |
||
505 | } |
||
506 | |||
507 | // Commented out by DM, 18 Dec 2018: |
||
508 | // This code did not work on systems with 8 GB RAM: |
||
509 | /* |
||
510 | long space = (pb->maxSpace*9)/10; // don't ask for more than 90% of available memory |
||
511 | |||
512 | maxSpace = 512L<<10; // this is a wild guess, actually |
||
513 | if(maxSpace > space) |
||
514 | maxSpace = space; |
||
515 | pb->maxSpace = maxSpace; |
||
516 | */ |
||
517 | |||
518 | // New variant: |
||
519 | if (maxspace_available()) { |
||
520 | pb->maxSpace = (int32)ceil((maxspace()/10.)*9); // don't ask for more than 90% of available memory |
||
521 | // FIXME: Also maxSpace64 |
||
522 | } |
||
523 | } |
||
524 | |||
525 | void RequestNext(FilterRecordPtr pb,long toprow){ |
||
526 | /* Request next block of the image */ |
||
527 | |||
528 | pb->inLoPlane = pb->outLoPlane = 0; |
||
529 | pb->inHiPlane = pb->outHiPlane = nplanes-1; |
||
530 | |||
531 | if (HAS_BIG_DOC(pb)) { |
||
532 | // if any of the formulae involve random access to image pixels, |
||
533 | // ask for the entire image |
||
534 | if (needall) { |
||
535 | SETRECT(BIGDOC_IN_RECT(pb), 0, 0, BIGDOC_IMAGE_SIZE(pb).h, BIGDOC_IMAGE_SIZE(pb).v); |
||
536 | } else { |
||
537 | // TODO: This does not work with GIMP. So, if we are using GIMP, we should |
||
538 | // somehow always use "needall=true", and/or find out why this doesn't work |
||
539 | // with GIMP. |
||
540 | |||
541 | // otherwise, process the filtered area, by chunksize parts |
||
542 | BIGDOC_IN_RECT(pb).left = BIGDOC_FILTER_RECT(pb).left; |
||
543 | BIGDOC_IN_RECT(pb).right = BIGDOC_FILTER_RECT(pb).right; |
||
544 | BIGDOC_IN_RECT(pb).top = (int32)toprow; |
||
545 | BIGDOC_IN_RECT(pb).bottom = (int32)MIN(toprow + chunksize, BIGDOC_FILTER_RECT(pb).bottom); |
||
546 | |||
547 | if (cnvused) { |
||
548 | // cnv() needs one extra pixel in each direction |
||
549 | if (BIGDOC_IN_RECT(pb).left > 0) |
||
550 | --BIGDOC_IN_RECT(pb).left; |
||
551 | if (BIGDOC_IN_RECT(pb).right < BIGDOC_IMAGE_SIZE(pb).h) |
||
552 | ++BIGDOC_IN_RECT(pb).right; |
||
553 | if (BIGDOC_IN_RECT(pb).top > 0) |
||
554 | --BIGDOC_IN_RECT(pb).top; |
||
555 | if (BIGDOC_IN_RECT(pb).bottom < BIGDOC_IMAGE_SIZE(pb).v) |
||
556 | ++BIGDOC_IN_RECT(pb).bottom; |
||
557 | } |
||
558 | } |
||
559 | BIGDOC_OUT_RECT(pb) = BIGDOC_FILTER_RECT(pb); |
||
560 | /* |
||
561 | {char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)", |
||
562 | needall, |
||
563 | BIGDOC_IN_RECT(pb).left,BIGDOC_IN_RECT(pb).top,BIGDOC_IN_RECT(pb).right,BIGDOC_IN_RECT(pb).bottom, |
||
564 | BIGDOC_FILTER_RECT(pb).left,BIGDOC_FILTER_RECT(pb).top,BIGDOC_FILTER_RECT(pb).right,BIGDOC_FILTER_RECT(pb).bottom);dbg(s);} |
||
565 | */ |
||
566 | } else { |
||
567 | // if any of the formulae involve random access to image pixels, |
||
568 | // ask for the entire image |
||
569 | if (needall) { |
||
570 | SETRECT(IN_RECT(pb), 0, 0, IMAGE_SIZE(pb).h, IMAGE_SIZE(pb).v); |
||
571 | } |
||
572 | else { |
||
573 | // TODO: This does not work with GIMP. So, if we are using GIMP, we should |
||
574 | // somehow always use "needall=true", and/or find out why this doesn't work |
||
575 | // with GIMP. |
||
576 | |||
577 | // otherwise, process the filtered area, by chunksize parts |
||
578 | IN_RECT(pb).left = FILTER_RECT(pb).left; |
||
579 | IN_RECT(pb).right = FILTER_RECT(pb).right; |
||
580 | IN_RECT(pb).top = (int16)toprow; |
||
581 | IN_RECT(pb).bottom = (int16)MIN(toprow + chunksize, FILTER_RECT(pb).bottom); |
||
582 | |||
583 | if (cnvused) { |
||
584 | // cnv() needs one extra pixel in each direction |
||
585 | if (IN_RECT(pb).left > 0) |
||
586 | --IN_RECT(pb).left; |
||
587 | if (IN_RECT(pb).right < IMAGE_SIZE(pb).h) |
||
588 | ++IN_RECT(pb).right; |
||
589 | if (IN_RECT(pb).top > 0) |
||
590 | --IN_RECT(pb).top; |
||
591 | if (IN_RECT(pb).bottom < IMAGE_SIZE(pb).v) |
||
592 | ++IN_RECT(pb).bottom; |
||
593 | } |
||
594 | } |
||
595 | OUT_RECT(pb) = FILTER_RECT(pb); |
||
596 | /* |
||
597 | {char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)", |
||
598 | needall, |
||
599 | IN_RECT(pb).left,IN_RECT(pb).top,IN_RECT(pb).right,IN_RECT(pb).bottom, |
||
600 | FILTER_RECT(pb).left,FILTER_RECT(pb).top,FILTER_RECT(pb).right,FILTER_RECT(pb).bottom);dbg(s);} |
||
601 | */ |
||
602 | } |
||
603 | } |
||
604 | |||
605 | void DoStart(FilterRecordPtr pb){ |
||
264 | daniel-mar | 606 | /* Global variable "needall": if src() or rad() functions are used, random access to the image data is required, |
607 | so we must request the entire image in a single chunk, otherwise we will use chunksize "CHUNK_ROWS". */ |
||
259 | daniel-mar | 608 | if (HAS_BIG_DOC(pb)) { |
609 | chunksize = needall ? (BIGDOC_FILTER_RECT(pb).bottom - BIGDOC_FILTER_RECT(pb).top) : CHUNK_ROWS; |
||
610 | toprow = BIGDOC_FILTER_RECT(pb).top; |
||
611 | } else { |
||
612 | chunksize = needall ? (FILTER_RECT(pb).bottom - FILTER_RECT(pb).top) : CHUNK_ROWS; |
||
613 | toprow = FILTER_RECT(pb).top; |
||
614 | } |
||
615 | RequestNext(pb, toprow); |
||
616 | } |
||
617 | |||
618 | OSErr DoContinue(FilterRecordPtr pb){ |
||
619 | OSErr e = noErr; |
||
620 | long outoffset; |
||
621 | |||
622 | if (HAS_BIG_DOC(pb)) { |
||
623 | VRect fr; |
||
624 | if (needall) { |
||
625 | fr = BIGDOC_FILTER_RECT(pb); // filter whole selection at once |
||
626 | } else if (cnvused) { |
||
627 | // we've requested one pixel extra all around |
||
628 | // (see RequestNext()), just for access purposes. But filter |
||
629 | // original selection only. |
||
630 | fr.left = BIGDOC_FILTER_RECT(pb).left; |
||
631 | fr.right = BIGDOC_FILTER_RECT(pb).right; |
||
632 | fr.top = toprow; |
||
633 | fr.bottom = MIN(toprow + chunksize, BIGDOC_FILTER_RECT(pb).bottom); |
||
634 | } else { // filter whatever portion we've been given |
||
635 | fr = BIGDOC_IN_RECT(pb); |
||
636 | } |
||
637 | |||
638 | outoffset = (long)pb->outRowBytes * (fr.top - BIGDOC_OUT_RECT(pb).top) |
||
639 | + (long)nplanes * (fr.left - BIGDOC_OUT_RECT(pb).left); |
||
640 | |||
641 | if (!(e = process_scaled_bigdoc(pb, true, fr, fr, |
||
642 | (Ptr)pb->outData + outoffset, pb->outRowBytes, 1.))) |
||
643 | { |
||
644 | toprow += chunksize; |
||
645 | if (toprow < BIGDOC_FILTER_RECT(pb).bottom) |
||
646 | RequestNext(pb, toprow); |
||
647 | else { |
||
648 | SETRECT(BIGDOC_IN_RECT(pb), 0, 0, 0, 0); |
||
649 | BIGDOC_OUT_RECT(pb) = BIGDOC_MASK_RECT(pb) = BIGDOC_IN_RECT(pb); |
||
650 | } |
||
651 | } |
||
652 | } else { |
||
653 | Rect fr; |
||
654 | if (needall) { |
||
655 | fr = FILTER_RECT(pb); // filter whole selection at once |
||
656 | } else if (cnvused) { |
||
657 | // we've requested one pixel extra all around |
||
658 | // (see RequestNext()), just for access purposes. But filter |
||
659 | // original selection only. |
||
660 | fr.left = FILTER_RECT(pb).left; |
||
661 | fr.right = FILTER_RECT(pb).right; |
||
662 | fr.top = toprow; |
||
663 | fr.bottom = MIN(toprow + chunksize, FILTER_RECT(pb).bottom); |
||
664 | } else { // filter whatever portion we've been given |
||
665 | fr = IN_RECT(pb); |
||
666 | } |
||
667 | |||
668 | outoffset = (long)pb->outRowBytes*(fr.top - OUT_RECT(pb).top) |
||
669 | + (long)nplanes*(fr.left - OUT_RECT(pb).left); |
||
670 | |||
671 | if(!(e = process_scaled_olddoc(pb, true, fr, fr, |
||
672 | (Ptr)pb->outData+outoffset, pb->outRowBytes, 1.))) |
||
673 | { |
||
674 | toprow += chunksize; |
||
675 | if(toprow < FILTER_RECT(pb).bottom) |
||
676 | RequestNext(pb,toprow); |
||
677 | else{ |
||
678 | SETRECT(IN_RECT(pb),0,0,0,0); |
||
679 | OUT_RECT(pb) = MASK_RECT(pb) = IN_RECT(pb); |
||
680 | } |
||
681 | } |
||
682 | } |
||
683 | return e; |
||
684 | } |
||
685 | |||
686 | void DoFinish(FilterRecordPtr pb){ |
||
687 | int i; |
||
688 | |||
689 | WriteScriptParamsOnRead(); |
||
690 | |||
691 | for(i = 4; i--;){ |
||
692 | freetree(tree[i]); |
||
693 | if(expr[i]) free(expr[i]); |
||
694 | } |
||
695 | } |