Subversion Repositories filter_foundry

Rev

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