Subversion Repositories filter_foundry

Rev

Rev 481 | Rev 483 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
  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 "str.h"
  27. #include "node.h"
  28. #include "funcs.h"
  29. #include "y.tab.h"
  30. #include "scripting.h"
  31. #include <math.h>
  32. #include "PIBufferSuite.h"
  33.  
  34. // GIMP (PSPI) and IrfanView preserve neither *data(gdata), nor pb->parameters between invocations!
  35. // For debugging, we can simulate it here
  36. //#define DEBUG_SIMULATE_GIMP
  37.  
  38. // Used to find out which host signatures and memory settings a plugin host has
  39. //#define SHOW_HOST_DEBUG
  40.  
  41. // Here are working variables:
  42. struct node *tree[4];
  43. char *err[4];
  44. int errpos[4],errstart[4],nplanes,cnvused,chunksize,toprow;
  45. uint8_t slider[8]; // this is the "working data". We cannot always use gdata->parm, because parm will not be loaded if a AFS file is read
  46. char* expr[4]; // this is the "working data". We cannot always use gdata->parm, because parm will not be loaded if a AFS file is read
  47. value_type cell[NUM_CELLS];
  48.  
  49. // this is the only memory area that keeps preserved by Photoshop:
  50. globals_t *gdata;
  51. FilterRecordPtr gpb;
  52.  
  53. #ifdef MAC_ENV
  54.         #define HINSTANCE HANDLE
  55.         #define hDllInstance NULL /* fake this Windows-only global */
  56. #endif
  57.  
  58. #ifdef WIN_ENV
  59. #include "manifest.h"
  60. #endif
  61.  
  62. extern struct sym_rec predefs[];
  63. extern int nplanes,varused[];
  64.  
  65. int checkandinitparams(Handle params);
  66.  
  67. // MPW MrC requires prototype
  68. DLLEXPORT MACPASCAL
  69. void ENTRYPOINT(short selector,FilterRecordPtr pb,intptr_t *data,short *result);
  70.  
  71. unsigned long get_parm_hash(PARM_T *parm) {
  72.         unsigned long hash;
  73.         int i;
  74.  
  75.         hash = djb2(parm->szCategory);
  76.         hash += djb2(parm->szTitle);
  77.         hash += djb2(parm->szCopyright);
  78.         hash += djb2(parm->szAuthor);
  79.         for (i = 0; i < 4; i++) hash += djb2(parm->szMap[i]);
  80.         for (i = 0; i < 8; i++) hash += djb2(parm->szCtl[i]);
  81.         for (i = 0; i < 4; i++) hash += djb2(parm->szFormula[i]);
  82.  
  83.         return hash;
  84. }
  85.  
  86. size_t get_temp_afs(LPTSTR outfilename, Boolean isStandalone, PARM_T *parm) {
  87.         char* atempdir;
  88.         int hash;
  89.         size_t i, j;
  90.         TCHAR out[MAX_PATH + 1];
  91.         char ahash[20];
  92.  
  93.         // out = getenv("TMP")
  94.         atempdir = getenv("TMP");
  95.         for (i = 0; i < strlen(atempdir); i++) {
  96.                 out[i] = (TCHAR)atempdir[i];
  97.                 out[i + 1] = 0;
  98.         }
  99.  
  100.         #ifdef WIN_ENV
  101.         if (xstrlen(out) > 0) xstrcat(out, TEXT("\\"));
  102.         #else
  103.         if (xstrlen(out) > 0) xstrcat(out, TEXT("/"));
  104.         #endif
  105.  
  106.         hash = (isStandalone) ? get_parm_hash(parm) : 0;
  107.  
  108.         // sprintf(outfilename, "%sFilterFoundry%d.afs", atempdir, hash);
  109.         xstrcat(out, TEXT("FilterFoundry"));
  110.         _itoa(hash, &ahash[0], 10);
  111.         for (i = 0; i < strlen(ahash); i++) {
  112.                 j = xstrlen(out);
  113.                 out[j] = (TCHAR)ahash[i];
  114.                 out[j + 1] = 0;
  115.         }
  116.         xstrcat(out, TEXT(".afs"));
  117.         if (outfilename != NULL) {
  118.                 xstrcpy(outfilename, out);
  119.         }
  120.         return xstrlen(out);
  121. }
  122.  
  123. void CALLBACK FakeRundll32(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) {
  124.         UNREFERENCED_PARAMETER(hwnd);
  125.         UNREFERENCED_PARAMETER(hinst);
  126.         UNREFERENCED_PARAMETER(lpszCmdLine);
  127.         UNREFERENCED_PARAMETER(nCmdShow);
  128.  
  129.         /*
  130.         char* tmp = (char*)malloc(512);
  131.         if (tmp != 0) {
  132.                 sprintf(tmp, "hwnd: %p\nhinst: %p\nlpszCmdLine: %s\nCmdShow: %d", (void*)(hwnd), (void*)(hinst), lpszCmdLine, nCmdShow);
  133.                 MessageBoxA(0, tmp, 0, 0);
  134.                 free(tmp);
  135.         }
  136.         */
  137.  
  138.         simplealert((TCHAR*)TEXT("You tried to execute this method with RunDLL32. This is a Photoshop plugin!"));
  139.         return;
  140. }
  141.  
  142. // this method is currently not used. We keep it in the code in case it is useful for someone...
  143. Ptr NewPtrClearUsingBufferSuite(size_t nBytes) {
  144.         PSBufferSuite1* pSBufferSuite32 = NULL;
  145.         PSBufferSuite2* pSBufferSuite64 = NULL;
  146.         void* data;
  147.  
  148.         if ((gpb->sSPBasic != 0) &&
  149.                 (gpb->sSPBasic->AcquireSuite(kPSBufferSuite, kPSBufferSuiteVersion2, (const void**)&pSBufferSuite64) == noErr) &&
  150.                 (pSBufferSuite64 != NULL) &&
  151.                 (pSBufferSuite64 != (PSBufferSuite2*)gpb->bufferProcs /*Implementation mistake in old Photoshop versions! (see note below)*/)
  152.                 )
  153.         {
  154.                 // PICA Buffer Suite 2.0 (64 bit)
  155.                 //
  156.                 // Note: Windows Photoshop 7 and CS 2 (Other Photoshop versions were not tested) accept
  157.                 // kPSBufferSuiteVersion2, but dont't correctly implement it:
  158.                 // Instead of returning a pointer to a PSBufferSuite2 structure,
  159.                 // they return the pointer RecordPtr->bufferProcs (structure BufferProcs)!
  160.                 //
  161.                 // 64-bit support for Windows was established in Photoshop CS 4,
  162.                 // and PSBufferSuite2 was first documented in SDK CS 6.
  163.                 //
  164.                 // So, kPSBufferSuiteVersion2 probably was partially implemented as hidden "Work in progress" version
  165.                 // before it was publicly documented.
  166.                 // Side note:  pb->bufferSpace64/pb->maxSpace64 was documented in SDK CC 2017.
  167.                 //             pb->bufferProcs->allocateProc64/spaceProc64 was documented in SDK CS 6.
  168.                 unsigned32 siz = nBytes;
  169.                 data = (void*)pSBufferSuite64->New(&siz, siz);
  170.                 if (siz < nBytes) data = NULL;
  171.                 gpb->sSPBasic->ReleaseSuite(kPSBufferSuite, kPSBufferSuiteVersion2);
  172.         }
  173.         else if ((gpb->sSPBasic != 0) &&
  174.                 (gpb->sSPBasic->AcquireSuite(kPSBufferSuite, kPSBufferSuiteVersion1, (const void**)&pSBufferSuite32) == noErr) &&
  175.                 (pSBufferSuite32 != NULL))
  176.         {
  177.                 // PICA Buffer Suite 1.0 (32 bit)
  178.                 unsigned32 siz = nBytes;
  179.                 data = (void*)pSBufferSuite32->New(&siz, siz);
  180.                 if (siz < nBytes) data = NULL;
  181.                 gpb->sSPBasic->ReleaseSuite(kPSBufferSuite, kPSBufferSuiteVersion1);
  182.         }
  183.         else
  184.         {
  185.                 // Standard Buffer Suite (deprecated)
  186.                 BufferID tempId;
  187.                 if ((/* *result = */ gpb->bufferProcs->allocateProc(nBytes, &tempId))) {
  188.                         data = NULL;
  189.                         return (Ptr)data;
  190.                 }
  191.                 data = (void*)gpb->bufferProcs->lockProc(tempId, true);
  192.         }
  193.  
  194.         if (data) memset(data, 0, nBytes);
  195.         return (Ptr)data;
  196. }
  197.  
  198. void CreateDataPointer(intptr_t* data) {
  199.         // Register "gdata" that contains the PARM information and other things which need to be persistant
  200.         // and preserve them in *data
  201.         // This memory allocation is never freed, because the filter can always be invoked again.
  202.         // The memory allocation will be kept for the lifetime of the Photoshop process and
  203.         // freed together with the application termination.
  204.  
  205.         // We have at least 5 options to allocate memory:
  206.  
  207.         // (Method 1)
  208.         // 1. The deprecated Standard Buffer Suite (pb->bufferProcs) - works fine!
  209.         //BufferID tempId;
  210.         //if ((/* *result = */ gpb->bufferProcs->allocateProc(sizeof(globals_t), &tempId))) {
  211.         //      *data = NULL;
  212.         //      return *data;
  213.         //}
  214.         //*data = (void*)gpb->bufferProcs->lockProc(tempId, true);
  215.  
  216.         // (Method 2) *DOES NOT WORK*
  217.         // 2. The recommended buffer suite (kPSBufferSuite),
  218.         //    It does not work, since it causes memory corruption when the filter is invoked a second time.
  219.         //    Probably the BufferSuite cannot be used to share data between filter invocations?
  220.         //    Also, the buffer suite is only available in a Adobe Photoshop host.
  221.         //*data = (intptr_t)NewPtrClearUsingBufferSuite(sizeof(globals_t));
  222.  
  223.         // (Method 3)
  224.         // 3. Using malloc(), which works also fine and is more independent from the host. It is also easier.
  225.         //    However, we do not know how malloc() is implemented, and it might cause problems if the
  226.         //    DLL is unloaded between invocations.
  227.         //*data = (intptr_t)malloc(sizeof(globals_t));
  228.         //if (*data) memset(*data, 0, sizeof(globals_t));
  229.  
  230.         // (Method 4)
  231.         // 4. Using PLUGIN.DLL:NewPtr(). This does FilterFactory 3.0.4, but requires an Adobe host.
  232.         //    In Plugin.dll, the function NewPtr() and NewPtrClear() are implemented as GlobalAlloc/GlobalLock.
  233.         //    Nothing special.
  234.  
  235.         // (Method 5)
  236.         // 5. Using GlobalAlloc/GlobalLock. This does FilterFactory 3.00 (Flags GHND = GMEM_MOVEABLE and GMEM_ZEROINIT).
  237.         //    This is Windows dependant. (However, on Mac we will just call NewPtr.)
  238.         //    GlobalAlloc and LocalAlloc are equal in 32-bit windows. The memory allocation is NOT global anymore,
  239.         //    instead it is on the private heap of the application. (Therefore we do not cause a leak when
  240.         //    Photoshop is closed).
  241.         // In Filter Foundry 1.7.0.17 we define a function called NewPtrClear, which is natively supported by Mac,
  242.         // and in Windows it will be implemented using GlobalAlloc/GlobalLock.
  243.         *data = (intptr_t)NewPtrClear(sizeof(globals_t));
  244. }
  245.  
  246. DLLEXPORT MACPASCAL
  247. void ENTRYPOINT(short selector, FilterRecordPtr pb, intptr_t *data, short *result){
  248.         static Boolean wantdialog = false;
  249.         static Boolean premiereWarnedOnce = false;
  250.         OSErr e = noErr;
  251.         char *reason;
  252.  
  253.         #ifdef SHOW_HOST_DEBUG
  254.         char* tmp;
  255.         #endif
  256.  
  257.         #ifdef WIN_ENV
  258.         // For Windows, we use an activation context to enforce that our Manifest resource will
  259.         // be used. This allows us to use Visual Styles, even if the host application does not
  260.         // support it.
  261.         ManifestActivationCtx manifestVars;
  262.         BOOL activationContextUsed;
  263.         #endif
  264.  
  265.         // ---------------------------------------------------------------------
  266.  
  267.         EnterCodeResource();
  268.  
  269.         #ifdef WIN_ENV
  270.         if ((intptr_t)result == SW_SHOWDEFAULT) {
  271.                 // When the 8BF file is analyzed with VirusTotal.com, it will invoke each
  272.                 // exported function by calling
  273.                 // loaddll64.exe 'C:\Users\user\Desktop\attachment.dll'
  274.                 //        ==>  rundll32.exe C:\Users\user\Desktop\attachment.dll,PluginMain
  275.                 //           ==> C:\Windows\system32\WerFault.exe -u -p 6612 -s 480
  276.                 //
  277.                 // But RunDLL32 requires following signature:
  278.                 //    void __stdcall EntryPoint(HWND hwnd,      HINSTANCE hinst,    LPSTR lpszCmdLine, int nCmdShow);
  279.                 // Our signature is:
  280.                 //    void           PluginMain(short selector, FilterRecordPtr pb, intptr_t *data,    short *result);
  281.                 //
  282.                 // Obviously, this will cause an Exception. (It crashes at *result=e because result is 0xA)
  283.                 // Here is the problem: The crash will be handled by WerFault.exe inside the
  284.                 // VirusTotal virtual machine. WerFault connects to various servers (9 DNS resolutions!) and does
  285.                 // a lot of weird things, but VirusTotal thinks that our plugin does all that stuff,
  286.                 // and so they mark our plugin as "malware"!
  287.                 // This is a problem with VirusTotal! It shall not assume that WerFault.exe actions are our actions!
  288.                 // Even processes like "MicrosoftEdgeUpdate.exe" and "SpeechRuntime.exe" are reported to be our
  289.                 // actions, although they have nothing to do with us!
  290.                 // See https://www.virustotal.com/gui/file/1f1012c567208186be455b81afc1ee407ae6476c197d633c70cc70929113223a/behavior
  291.                 //
  292.                 // TODO: Usually, The first 64KB of address space are always invalid. However, in Win32s (Windows 3.11), the
  293.                 //       variable "result" is <=0xFFFF ! Let's just hope that it is never 0x000A (SW_SHOWDEFAULT),
  294.                 //       otherwise we have a problem here!
  295.                 // I don't understand why this works! Aren't we __cdecl and rundll expected __stdcall? But why is the parameter order correct and not reversed?
  296.                 FakeRundll32((HWND)(intptr_t)selector, (HINSTANCE)pb, (LPSTR)data, (int)(intptr_t)result);
  297.                 return;
  298.         }
  299.         #endif
  300.  
  301.         #ifdef SHOW_HOST_DEBUG
  302.         tmp = (char*)malloc(512);
  303.         sprintf(tmp, "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 == 0 ? -999/*About has no BufferProcs*/ : pb->bufferProcs->numBufferProcs);
  304.         simplealert(tmp);
  305.         free(tmp);
  306.         #endif
  307.  
  308.         if (pb->hostSig == HOSTSIG_ADOBE_PREMIERE) {
  309.                 // DM 19.07.2021 : Tried running the 8BF file in Adobe Premiere 5 (yes, that's possible,
  310.                 // and there is even a FilterFactory for Premiere!),
  311.                 // but it crashes in evalpixel() where there is write-access to the "outp".
  312.                 // Probably the canvas structure is different (maybe it contains frames to achieve transitions?)
  313.                 if (!premiereWarnedOnce) {
  314.                         simplealert((TCHAR*)TEXT("This version of Filter Foundry is not compatible with Adobe Premiere!"));
  315.                 }
  316.                 premiereWarnedOnce = true;
  317.                 *result = errPlugInHostInsufficient;
  318.                 return;
  319.         }
  320.  
  321.         #ifdef DEBUG_SIMULATE_GIMP
  322.         *data = 0;
  323.         pb->parameters = pb->handleProcs->newProc(1);
  324.         #endif
  325.  
  326.         gpb = pb;
  327.  
  328.         if (selector != filterSelectorAbout && !*data) {
  329.                 // The filter was never called before. We allocate (zeroed) memory now.
  330.                 // Note: gdata->standalone and gdata->parmloaded will be set later
  331.                 CreateDataPointer(data);
  332.                 if (!*data) {
  333.                         *result = memFullErr;
  334.                         return;
  335.                 }
  336.         }
  337.  
  338.         gdata = (globals_t*)*data;
  339.  
  340.         #ifdef WIN_ENV
  341.         activationContextUsed = ActivateManifest((HMODULE)hDllInstance, 1, &manifestVars);
  342.         #endif
  343.  
  344.         nplanes = MIN(pb->planes,4);
  345.  
  346.         switch (selector){
  347.         case filterSelectorAbout:
  348.                 if (!gdata) {
  349.                         // This is a temporary gdata structure just for the About dialog!
  350.                         // Not to be confused with the "real" gdata during the filter invocation (containing more data).
  351.                         gdata = (globals_t*)malloc(sizeof(globals_t));
  352.                         if (!gdata) break;
  353.                         gdata->hWndMainDlg = (HWND)((PlatformData*)((AboutRecordPtr)pb)->platformData)->hwnd; // so that simplealert() works
  354.                         gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason);
  355.                         if (gdata->parmloaded && (gdata->parm.cbSize != PARM_SIZE) && (gdata->parm.cbSize != PARM_SIZE_PREMIERE) && (gdata->parm.cbSize != PARM_SIG_MAC)) {
  356.                                 if (gdata->obfusc) {
  357.                                         simplealert((TCHAR*)TEXT("Incompatible obfuscation."));
  358.                                 }
  359.                                 else {
  360.                                         simplealert((TCHAR*)TEXT("Invalid parameter data."));
  361.                                 }
  362.                         }
  363.                         else {
  364.                                 DoAbout((AboutRecordPtr)pb);
  365.                         }
  366.                         free(gdata);
  367.                         gdata = NULL;
  368.                 } else {
  369.                         DoAbout((AboutRecordPtr)pb);
  370.                 }
  371.                 break;
  372.         case filterSelectorParameters:
  373.                 wantdialog = true;
  374.                 break;
  375.         case filterSelectorPrepare:
  376.                 gdata->hWndMainDlg = 0;
  377.                 DoPrepare(pb);
  378.                 init_symtab(predefs); // ready for parser calls
  379.                 init_trigtab();
  380.                 break;
  381.         case filterSelectorStart:
  382.                 if (HAS_BIG_DOC(pb)) {
  383.                         // The BigDocument structure is required if the document is larger than 30,000 pixels
  384.                         // It deprecates imageSize, filterRect, inRect, outRect, maskRect, floatCoord, and wholeSize.
  385.                         // By setting it to nonzero, we communicate to Photoshop that we support the BigDocument structure.
  386.                         pb->bigDocumentData->PluginUsing32BitCoordinates = true;
  387.                 }
  388.  
  389.                 /* initialise the parameter handle that Photoshop keeps for us */
  390.                 if(!pb->parameters)
  391.                         pb->parameters = PINEWHANDLE(1); // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that.
  392.  
  393.                 if (!pb->parameters) {
  394.                         e = memFullErr;
  395.                 }
  396.                 else
  397.                 {
  398.                         wantdialog |= checkandinitparams(pb->parameters);
  399.  
  400.                         /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */
  401.                         if (wantdialog && (!gdata->standalone || gdata->parm.popDialog)) {
  402.                                 if (maindialog(pb)) {
  403.                                         if (!host_preserves_parameters()) {
  404.                                                 /* Workaround for GIMP/PSPI, to avoid that formulas vanish when you re-open the main window.
  405.                                                    The reason is a bug in PSPI: The host should preserve the value of pb->parameters, which PSPI does not do.
  406.                                                    Also, all global variables are unloaded, so the plugin cannot preserve any data.
  407.                                                    Workaround in FF 1.7: If the host GIMP is detected, then a special mode will be activated.
  408.                                                    This mode saves the filter data into a temporary file "FilterFoundryXX.afs" and loads it
  409.                                                    when the window is opened again. */
  410.                                                    // Workaround: Save settings in "FilterFoundryXX.afs" if the host does not preserve pb->parameters
  411.                                                 TCHAR outfilename[MAX_PATH + 1];
  412.                                                 StandardFileReply sfr;
  413.                                                 char* bakexpr[4];
  414.                                                 int i;
  415.  
  416.                                                 sfr.sfGood = true;
  417.                                                 sfr.sfReplacing = true;
  418.                                                 sfr.sfType = PS_FILTER_FILETYPE;
  419.  
  420.                                                 get_temp_afs(&outfilename[0], gdata->standalone, &gdata->parm);
  421.  
  422.                                                 xstrcpy(sfr.sfFile.szName, outfilename);
  423.                                                 #ifdef WIN_ENV
  424.                                                 sfr.nFileExtension = (WORD)(xstrlen(outfilename) - strlen(".afs") + 1);
  425.                                                 #endif
  426.                                                 sfr.sfScript = 0; // FIXME: is that ok?
  427.  
  428.                                                 // We only want the parameters (ctl,map) in the temporary .afs file
  429.                                                 // It is important to remove the expressions, otherwise they would be
  430.                                                 // revealed in the temporary files.
  431.                                                 for (i = 0; i < 4; i++) {
  432.                                                         bakexpr[i] = expr[i]; // moved out of the if-definition to make the compiler happy
  433.                                                 }
  434.                                                 if (gdata->standalone) {
  435.                                                         expr[0] = _strdup("r");
  436.                                                         expr[1] = _strdup("g");
  437.                                                         expr[2] = _strdup("b");
  438.                                                         expr[3] = _strdup("a");
  439.                                                 }
  440.  
  441.                                                 savefile_afs_pff_picotxt(&sfr);
  442.  
  443.                                                 if (gdata->standalone) {
  444.                                                         for (i = 0; i < 4; i++) {
  445.                                                                 if (expr[i]) free(expr[i]);
  446.                                                                 expr[i] = bakexpr[i];
  447.                                                         }
  448.                                                 }
  449.                                         }
  450.                                         else {
  451.                                                 /* update stored parameters from new user settings */
  452.                                                 if (pb->parameters)
  453.                                                         saveparams_afs_pff(pb->parameters);
  454.                                         }
  455.                                 }
  456.                                 else
  457.                                         e = userCanceledErr;
  458.                         }
  459.                         wantdialog = false;
  460.                 }
  461.  
  462.                 if(e == noErr){
  463.                         if(setup(pb)){
  464.                                 DoStart(pb);
  465.                         }else{
  466.                                 e = filterBadParameters;
  467.                         }
  468.                 }
  469.                 break;
  470.         case filterSelectorContinue:
  471.                 e = DoContinue(pb);
  472.                 break;
  473.         case filterSelectorFinish:
  474.                 DoFinish(pb);
  475.                 break;
  476.         default:
  477.                 e = filterBadParameters;
  478.         }
  479.  
  480.         *result = e;
  481.  
  482.         // TODO: Question: Is that OK to call this every invocation, or should it be only around UI stuff?
  483.         #ifdef WIN_ENV
  484.         if (activationContextUsed) DeactivateManifest(&manifestVars);
  485.         #endif
  486.  
  487.         ExitCodeResource();
  488. }
  489.  
  490. int checkandinitparams(Handle params){
  491.         char *reasonstr,*reason;
  492.         int i;
  493.         Boolean bUninitializedParams;
  494.         Boolean showdialog;
  495.  
  496.         if (!host_preserves_parameters()) {
  497.                 // Workaround: Load settings in "FilterFoundryXX.afs" if host does not preserve pb->parameters
  498.                 TCHAR outfilename[MAX_PATH + 1];
  499.                 Boolean isStandalone;
  500.                 StandardFileReply sfr;
  501.                 char* bakexpr[4];
  502.  
  503.                 sfr.sfGood = true;
  504.                 sfr.sfReplacing = true;
  505.                 sfr.sfType = PS_FILTER_FILETYPE;
  506.  
  507.                 // We need to set gdata->standalone after loadfile(), but we must call readPARMresource() before loadfile()
  508.                 // Reason: readPARMresource() reads parameters from the DLL while loadfile() reads parameters from the AFS file
  509.                 // But loadfile() will reset gdata->standalone ...
  510.                 isStandalone = readPARMresource((HMODULE)hDllInstance, &reason);
  511.                 if (isStandalone && (gdata->parm.cbSize != PARM_SIZE) && (gdata->parm.cbSize != PARM_SIZE_PREMIERE) && (gdata->parm.cbSize != PARM_SIG_MAC)) {
  512.                         if (gdata->obfusc) {
  513.                                 simplealert((TCHAR*)TEXT("Incompatible obfuscation."));
  514.                         }
  515.                         else {
  516.                                 simplealert((TCHAR*)TEXT("Invalid parameter data."));
  517.                         }
  518.                         gdata->parmloaded = false;
  519.                         return false;
  520.                 }
  521.  
  522.                 get_temp_afs(&outfilename[0], isStandalone, &gdata->parm);
  523.  
  524.                 xstrcpy(sfr.sfFile.szName, outfilename);
  525.                 #ifdef WIN_ENV
  526.                 sfr.nFileExtension = (WORD)(xstrlen(outfilename) - strlen(".afs") + 1);
  527.                 #endif
  528.                 sfr.sfScript = 0; // FIXME: is that ok?
  529.  
  530.                 // We only want the parameters (ctl,map) in the temporary .afs file
  531.                 if (isStandalone) {
  532.                         for (i = 0; i < 4; i++) {
  533.                                 bakexpr[i] = my_strdup(expr[i]);
  534.                         }
  535.                 }
  536.  
  537.                 if (loadfile(&sfr, &reason)) {
  538.                         gdata->standalone = gdata->parmloaded = isStandalone;
  539.  
  540.                         if (isStandalone) {
  541.                                 for (i = 0; i < 4; i++) {
  542.                                         if (expr[i]) free(expr[i]);
  543.                                         expr[i] = bakexpr[i];
  544.                                 }
  545.                         }
  546.  
  547.                         return true;
  548.                 }
  549.         }
  550.  
  551.         if( (bUninitializedParams = !(params && readparams_afs_pff(params,&reasonstr))) ){
  552.                 /* either the parameter handle was uninitialised,
  553.                    or the parameter data couldn't be read; set default values */
  554.  
  555.                 // see if saved parameters exist
  556.                 gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason);
  557.                 if (gdata->parmloaded && (gdata->parm.cbSize != PARM_SIZE) && (gdata->parm.cbSize != PARM_SIZE_PREMIERE) && (gdata->parm.cbSize != PARM_SIG_MAC)) {
  558.                         if (gdata->obfusc) {
  559.                                 simplealert((TCHAR*)TEXT("Incompatible obfuscation."));
  560.                         }
  561.                         else {
  562.                                 simplealert((TCHAR*)TEXT("Invalid parameter data."));
  563.                         }
  564.                         gdata->parmloaded = false;
  565.                         return false;
  566.                 }
  567.  
  568.                 if(!gdata->standalone){
  569.                         // no saved settings (not standalone)
  570.                         for(i = 0; i < 8; ++i)
  571.                                 slider[i] = (uint8_t)(i*10+100);
  572.                         for(i = 0; i < 4; ++i)
  573.                                 if(expr[i])
  574.                                         free(expr[i]);
  575.                         if(gpb->imageMode == plugInModeRGBColor){
  576.                                 expr[0] = _strdup("r");
  577.                                 expr[1] = _strdup("g");
  578.                                 expr[2] = _strdup("b");
  579.                                 expr[3] = _strdup("a");
  580.                         }else{
  581.                                 expr[0] = _strdup("c");
  582.                                 expr[1] = _strdup("c");
  583.                                 expr[2] = _strdup("c");
  584.                                 expr[3] = _strdup("c");
  585.                         }
  586.                 }
  587.         }
  588.  
  589.         // let scripting system change parameters, if we're scripted;
  590.         // user may want to force display of dialog during scripting playback
  591.         switch (ReadScriptParamsOnRead()) {
  592.         case SCR_SHOW_DIALOG:
  593.                 showdialog = true;
  594.                 break;
  595.         case SCR_HIDE_DIALOG:
  596.                 showdialog = false;
  597.                 break;
  598.         default:
  599.         case SCR_NO_SCRIPT:
  600.                 showdialog = bUninitializedParams;
  601.                 break;
  602.         }
  603.  
  604.         if (params) saveparams_afs_pff(params);
  605.  
  606.         return showdialog;
  607. }
  608.  
  609. Boolean host_preserves_parameters() {
  610.         #ifdef DEBUG_SIMULATE_GIMP
  611.         return false;
  612.         #endif
  613.  
  614.         if (gpb->hostSig == HOSTSIG_GIMP) return false;
  615.         if (gpb->hostSig == HOSTSIG_IRFANVIEW) return false;
  616.  
  617.         // We just assume the other hosts preserve the parameters
  618.         return true;
  619. }
  620.  
  621. int64_t maxspace(){
  622.         // Please see "Hosts.md" for details about the MaxSpace implementations of tested plugins
  623.  
  624.         // Plugins that don't support MaxSpace64 shall set the field to zero; then we will use MaxSpace instead.
  625.         // Also check "gpb->bufferProcs->numBufferProcs" to see if 64 bit API is available
  626.         if ((gpb->bufferProcs->numBufferProcs >= 8) && (gpb->maxSpace64 > 0)) {
  627.                 uint64_t maxSpace64 = gpb->maxSpace64;
  628.  
  629.                 return maxSpace64;
  630.         } else {
  631.                 // 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.
  632.                 unsigned int maxSpace32 = (unsigned int) gpb->maxSpace;
  633.                 uint64_t maxSpace64 = maxSpace32;
  634.  
  635.                 if (gpb->hostSig == HOSTSIG_IRFANVIEW) maxSpace64 *= 1024; // IrfanView is giving Kilobytes instead of Bytes
  636.                 //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???
  637.  
  638.                 return maxSpace64;
  639.         }
  640. }
  641.  
  642. Boolean maxspace_available() {
  643.         // Please see "Hosts.md" for details about the MaxSpace implementations of tested plugins
  644.  
  645.         // GIMP PSPI sets MaxSpace to hardcoded 100 MB
  646.         if (gpb->hostSig == HOSTSIG_GIMP) return false;
  647.  
  648.         // HOSTSIG_PAINT_NET sets MaxSpace to hardcoded 1 GB, see https://github.com/0xC0000054/PSFilterPdn/issues/5
  649.         // Comment by the host author "This was done to avoid any compatibility issues with plugins handling 2 GB - 1"
  650.         if (gpb->hostSig == HOSTSIG_PAINT_NET) return false;
  651.  
  652.         return true;
  653. }
  654.  
  655. void DoPrepare(FilterRecordPtr pb){
  656.         int i;
  657.  
  658.         for(i = 4; i--;){
  659.                 expr[i] = NULL;
  660.                 tree[i] = NULL;
  661.                 err[i] = NULL;
  662.         }
  663.  
  664.         // Commented out by DM, 18 Dec 2018:
  665.         // This code did not work on systems with 8 GB RAM:
  666.         /*
  667.         long space = (pb->maxSpace*9)/10; // don't ask for more than 90% of available memory
  668.  
  669.         maxSpace = 512L<<10; // this is a wild guess, actually
  670.         if(maxSpace > space)
  671.                         maxSpace = space;
  672.         pb->maxSpace = maxSpace;
  673.         */
  674.  
  675.         // New variant:
  676.         if (maxspace_available()) {
  677.                 // don't ask for more than 90% of available memory
  678.                 int64 ninetyPercent = (int64)ceil((maxspace() / 10.) * 9);
  679.                 if ((gpb->bufferProcs->numBufferProcs >= 8) && (gpb->maxSpace64 > 0)) {
  680.                         pb->maxSpace64 = ninetyPercent;
  681.                 }
  682.                 if (ninetyPercent <= 0x7FFFFFFF) {
  683.                         pb->maxSpace = (int32)ninetyPercent;
  684.                 }
  685.         }
  686. }
  687.  
  688. void RequestNext(FilterRecordPtr pb){
  689.         /* Request next block of the image */
  690.  
  691.         pb->inLoPlane = pb->outLoPlane = 0;
  692.         pb->inHiPlane = pb->outHiPlane = nplanes-1;
  693.  
  694.         if (HAS_BIG_DOC(pb)) {
  695.                 // if any of the formulae involve random access to image pixels,
  696.                 // ask for the entire image
  697.                 if (needall) {
  698.                         SETRECT(BIGDOC_IN_RECT(pb), 0, 0, BIGDOC_IMAGE_SIZE(pb).h, BIGDOC_IMAGE_SIZE(pb).v);
  699.                 } else {
  700.                         // TODO: This does not work with GIMP. So, if we are using GIMP, we should
  701.                         //       somehow always use "needall=true", and/or find out why this doesn't work
  702.                         //       with GIMP.
  703.  
  704.                         // otherwise, process the filtered area, by chunksize parts
  705.                         BIGDOC_IN_RECT(pb).left = BIGDOC_FILTER_RECT(pb).left;
  706.                         BIGDOC_IN_RECT(pb).right = BIGDOC_FILTER_RECT(pb).right;
  707.                         BIGDOC_IN_RECT(pb).top = (int32)toprow;
  708.                         BIGDOC_IN_RECT(pb).bottom = (int32)MIN(toprow + chunksize, BIGDOC_FILTER_RECT(pb).bottom);
  709.  
  710.                         if (cnvused) {
  711.                                 // cnv() needs one extra pixel in each direction
  712.                                 if (BIGDOC_IN_RECT(pb).left > 0)
  713.                                         --BIGDOC_IN_RECT(pb).left;
  714.                                 if (BIGDOC_IN_RECT(pb).right < BIGDOC_IMAGE_SIZE(pb).h)
  715.                                         ++BIGDOC_IN_RECT(pb).right;
  716.                                 if (BIGDOC_IN_RECT(pb).top > 0)
  717.                                         --BIGDOC_IN_RECT(pb).top;
  718.                                 if (BIGDOC_IN_RECT(pb).bottom < BIGDOC_IMAGE_SIZE(pb).v)
  719.                                         ++BIGDOC_IN_RECT(pb).bottom;
  720.                         }
  721.                 }
  722.                 BIGDOC_OUT_RECT(pb) = BIGDOC_FILTER_RECT(pb);
  723.                 /*
  724.                 {char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
  725.                                 needall,
  726.                                 BIGDOC_IN_RECT(pb).left,BIGDOC_IN_RECT(pb).top,BIGDOC_IN_RECT(pb).right,BIGDOC_IN_RECT(pb).bottom,
  727.                                 BIGDOC_FILTER_RECT(pb).left,BIGDOC_FILTER_RECT(pb).top,BIGDOC_FILTER_RECT(pb).right,BIGDOC_FILTER_RECT(pb).bottom);dbg(s);}
  728.                 */
  729.         } else {
  730.                 // if any of the formulae involve random access to image pixels,
  731.                 // ask for the entire image
  732.                 if (needall) {
  733.                         SETRECT(IN_RECT(pb), 0, 0, IMAGE_SIZE(pb).h, IMAGE_SIZE(pb).v);
  734.                 }
  735.                 else {
  736.                         // TODO: This does not work with GIMP. So, if we are using GIMP, we should
  737.                         //       somehow always use "needall=true", and/or find out why this doesn't work
  738.                         //       with GIMP.
  739.  
  740.                         // otherwise, process the filtered area, by chunksize parts
  741.                         IN_RECT(pb).left = FILTER_RECT(pb).left;
  742.                         IN_RECT(pb).right = FILTER_RECT(pb).right;
  743.                         IN_RECT(pb).top = (int16)toprow;
  744.                         IN_RECT(pb).bottom = (int16)MIN(toprow + chunksize, FILTER_RECT(pb).bottom);
  745.  
  746.                         if (cnvused) {
  747.                                 // cnv() needs one extra pixel in each direction
  748.                                 if (IN_RECT(pb).left > 0)
  749.                                         --IN_RECT(pb).left;
  750.                                 if (IN_RECT(pb).right < IMAGE_SIZE(pb).h)
  751.                                         ++IN_RECT(pb).right;
  752.                                 if (IN_RECT(pb).top > 0)
  753.                                         --IN_RECT(pb).top;
  754.                                 if (IN_RECT(pb).bottom < IMAGE_SIZE(pb).v)
  755.                                         ++IN_RECT(pb).bottom;
  756.                         }
  757.                 }
  758.                 OUT_RECT(pb) = FILTER_RECT(pb);
  759.                 /*
  760.                 {char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
  761.                                 needall,
  762.                                 IN_RECT(pb).left,IN_RECT(pb).top,IN_RECT(pb).right,IN_RECT(pb).bottom,
  763.                                 FILTER_RECT(pb).left,FILTER_RECT(pb).top,FILTER_RECT(pb).right,FILTER_RECT(pb).bottom);dbg(s);}
  764.                 */
  765.         }
  766. }
  767.  
  768. void DoStart(FilterRecordPtr pb){
  769.         /* Global variable "needall": if src() or rad() functions are used, random access to the image data is required,
  770.            so we must request the entire image in a single chunk, otherwise we will use chunksize "CHUNK_ROWS". */
  771.         if (HAS_BIG_DOC(pb)) {
  772.                 chunksize = needall ? (BIGDOC_FILTER_RECT(pb).bottom - BIGDOC_FILTER_RECT(pb).top) : CHUNK_ROWS;
  773.                 toprow = BIGDOC_FILTER_RECT(pb).top;
  774.         } else {
  775.                 chunksize = needall ? (FILTER_RECT(pb).bottom - FILTER_RECT(pb).top) : CHUNK_ROWS;
  776.                 toprow = FILTER_RECT(pb).top;
  777.         }
  778.         RequestNext(pb);
  779. }
  780.  
  781. OSErr DoContinue(FilterRecordPtr pb){
  782.         OSErr e = noErr;
  783.         long outoffset;
  784.  
  785.         if (HAS_BIG_DOC(pb)) {
  786.                 VRect fr;
  787.                 if (needall) {
  788.                         fr = BIGDOC_FILTER_RECT(pb);  // filter whole selection at once
  789.                 } else if (cnvused) {
  790.                         // we've requested one pixel extra all around
  791.                         // (see RequestNext()), just for access purposes. But filter
  792.                         // original selection only.
  793.                         fr.left = BIGDOC_FILTER_RECT(pb).left;
  794.                         fr.right = BIGDOC_FILTER_RECT(pb).right;
  795.                         fr.top = toprow;
  796.                         fr.bottom = MIN(toprow + chunksize, BIGDOC_FILTER_RECT(pb).bottom);
  797.                 } else {  // filter whatever portion we've been given
  798.                         fr = BIGDOC_IN_RECT(pb);
  799.                 }
  800.  
  801.                 outoffset = (long)pb->outRowBytes * (fr.top - BIGDOC_OUT_RECT(pb).top)
  802.                         + (long)nplanes * (fr.left - BIGDOC_OUT_RECT(pb).left);
  803.  
  804.                 if (!(e = process_scaled_bigdoc(pb, true, fr, fr,
  805.                         (Ptr)pb->outData + outoffset, pb->outRowBytes, 1.)))
  806.                 {
  807.                         toprow += chunksize;
  808.                         if (toprow < BIGDOC_FILTER_RECT(pb).bottom)
  809.                                 RequestNext(pb);
  810.                         else {
  811.                                 SETRECT(BIGDOC_IN_RECT(pb), 0, 0, 0, 0);
  812.                                 BIGDOC_OUT_RECT(pb) = BIGDOC_MASK_RECT(pb) = BIGDOC_IN_RECT(pb);
  813.                         }
  814.                 }
  815.         } else {
  816.                 Rect fr;
  817.                 if (needall) {
  818.                         fr = FILTER_RECT(pb);  // filter whole selection at once
  819.                 } else if (cnvused) {
  820.                         // we've requested one pixel extra all around
  821.                         // (see RequestNext()), just for access purposes. But filter
  822.                         // original selection only.
  823.                         fr.left = FILTER_RECT(pb).left;
  824.                         fr.right = FILTER_RECT(pb).right;
  825.                         fr.top = toprow;
  826.                         fr.bottom = MIN(toprow + chunksize, FILTER_RECT(pb).bottom);
  827.                 } else {  // filter whatever portion we've been given
  828.                         fr = IN_RECT(pb);
  829.                 }
  830.  
  831.                 outoffset = (long)pb->outRowBytes*(fr.top - OUT_RECT(pb).top)
  832.                         + (long)nplanes*(fr.left - OUT_RECT(pb).left);
  833.  
  834.                 if(!(e = process_scaled_olddoc(pb, true, fr, fr,
  835.                         (Ptr)pb->outData+outoffset, pb->outRowBytes, 1.)))
  836.                 {
  837.                         toprow += chunksize;
  838.                         if(toprow < FILTER_RECT(pb).bottom)
  839.                                 RequestNext(pb);
  840.                         else{
  841.                                 SETRECT(IN_RECT(pb),0,0,0,0);
  842.                                 OUT_RECT(pb) = MASK_RECT(pb) = IN_RECT(pb);
  843.                         }
  844.                 }
  845.         }
  846.         return e;
  847. }
  848.  
  849. void DoFinish(FilterRecordPtr pb){
  850.         int i;
  851.  
  852.         UNREFERENCED_PARAMETER(pb);
  853.  
  854.         WriteScriptParamsOnRead();
  855.  
  856.         for(i = 4; i--;){
  857.                 freetree(tree[i]);
  858.                 if(expr[i]) free(expr[i]);
  859.         }
  860. }
  861.  
  862. InternalState saveInternalState() {
  863.         InternalState ret;
  864.         int i;
  865.  
  866.         ret.bak_obfusc = gdata->obfusc;
  867.         ret.bak_standalone = gdata->standalone;
  868.         ret.bak_parmloaded = gdata->parmloaded;
  869.         memcpy(&ret.bak_parm, &gdata->parm, sizeof(PARM_T));
  870.         for (i = 0; i < 4; i++) ret.bak_expr[i] = my_strdup(expr[i]);
  871.         for (i = 0; i < 8; i++) ret.bak_slider[i] = slider[i];
  872.  
  873.         return ret;
  874. }
  875.  
  876. void restoreInternalState(InternalState state) {
  877.         int i;
  878.         gdata->obfusc = state.bak_obfusc;
  879.         gdata->standalone = state.bak_standalone;
  880.         gdata->parmloaded = state.bak_parmloaded;
  881.         memcpy(&gdata->parm, &state.bak_parm, sizeof(PARM_T));
  882.         for (i = 0; i < 4; i++) {
  883.                 if (expr[i]) free(expr[i]);
  884.                 expr[i] = state.bak_expr[i];
  885.         }
  886.         for (i = 0; i < 8; i++) {
  887.                 slider[i] = state.bak_slider[i];
  888.         }
  889. }
  890.