Subversion Repositories filter_foundry

Rev

Rev 231 | Rev 233 | 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.  
  32. struct node *tree[4];
  33. char *err[4];
  34. int errpos[4],errstart[4],nplanes,cnvused,chunksize,toprow;
  35. value_type slider[8],cell[NUM_CELLS],map[4][0x100];
  36. char *expr[4];
  37. // long maxSpace;
  38. globals_t *gdata;
  39. FilterRecordPtr gpb;
  40.  
  41. #ifdef MAC_ENV
  42.         #define HINSTANCE HANDLE
  43.         #define hDllInstance NULL /* fake this Windows-only global */
  44. #endif
  45.  
  46. #ifdef WIN_ENV
  47. #include "manifest.h"
  48. #endif
  49.  
  50. extern struct sym_rec predefs[];
  51. extern int nplanes,varused[];
  52.  
  53. int checkandinitparams(Handle params);
  54.  
  55. // MPW MrC requires prototype
  56. DLLEXPORT MACPASCAL
  57. void ENTRYPOINT(short selector,FilterRecordPtr pb,intptr_t *data,short *result);
  58.  
  59. DLLEXPORT MACPASCAL
  60. void ENTRYPOINT(short selector, FilterRecordPtr pb, intptr_t *data, short *result){
  61.         static Boolean wantdialog = false;
  62.         static Boolean premiereWarnedOnce = false;
  63.         OSErr e = noErr;
  64.         char *reason;
  65. #ifdef WIN_ENV
  66.         // For Windows, we use an activation context to enforce that our Manifest resource will
  67.         // be used. This allows us to use Visual Styles, even if the host application does not
  68.         // support it.
  69.         ManifestActivationCtx manifestVars;
  70.         BOOL activationContextUsed;
  71. #endif
  72.  
  73.         /*
  74.         char* s = (char*)malloc(512);
  75.         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);
  76.         simplealert(s);
  77.         */
  78.  
  79.         if (pb->hostSig == HOSTSIG_ADOBE_PREMIERE) {
  80.                 // DM 19.07.2021 : Tried running the 8BF file in Adobe Premiere 5 (yes, that's possible,
  81.                 // and there is even a FilterFactory for Premeire!),
  82.                 // but it crashes in evalpixel() where there is write-access to the "outp".
  83.                 // Probably the canvas structure is different (maybe it contains frames to achieve transitions?)
  84.                 if (!premiereWarnedOnce) {
  85.                         simplealert("This version of Filter Foundry is not compatible with Adobe Premiere!");
  86.                 }
  87.                 premiereWarnedOnce = true;
  88.                 *result = filterBadParameters;
  89.                 return;
  90.         }
  91.  
  92. #ifdef WIN_ENV
  93.         activationContextUsed = ActivateManifest((HMODULE)hDllInstance, 1, &manifestVars);
  94. #endif
  95.  
  96.         if(selector != filterSelectorAbout && !*data){
  97.                 BufferID tempId;
  98.                 if( (*result = PS_BUFFER_ALLOC(sizeof(globals_t), &tempId)) ) {
  99. #ifdef WIN_ENV
  100.                         if (activationContextUsed) DeactivateManifest(&manifestVars);
  101. #endif
  102.                         return;
  103.                 }
  104.                 *data = (intptr_t)PS_BUFFER_LOCK(tempId, true);
  105.                 gdata = (globals_t*)*data;
  106.                 gdata->standalone = gdata->parmloaded = false;
  107.         } else {
  108.                 gdata = (globals_t*)*data;
  109.         }
  110.  
  111.         EnterCodeResource();
  112.  
  113.         gpb = pb;
  114.  
  115.         nplanes = MIN(pb->planes,4);
  116.  
  117.         switch (selector){
  118.         case filterSelectorAbout:
  119.                 if (!gdata) {
  120.                         gdata = (globals_t*)malloc(sizeof(globals_t));
  121.                         if (!gdata) break;
  122.                         gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason,READ_OBFUSC);
  123.                         DoAbout((AboutRecordPtr)pb);
  124.                         free(gdata);
  125.                         gdata = NULL;
  126.                 } else {
  127.                         DoAbout((AboutRecordPtr)pb);
  128.                 }
  129.                 break;
  130.         case filterSelectorParameters:
  131.                 wantdialog = true;
  132.                 break;
  133.         case filterSelectorPrepare:
  134.                 DoPrepare(pb);
  135.                 init_symtab(predefs); // ready for parser calls
  136.                 init_trigtab();
  137.                 break;
  138.         case filterSelectorStart:
  139.                 if (pb->bigDocumentData != NULL) {
  140.                         // The BigDocument structure is required if the document is larger than 30,000 pixels
  141.                         // It deprecates imageSize, filterRect, inRect, outRect, maskRect, floatCoord, and wholeSize.
  142.                         // By setting it to nonzero, we communicate to Photoshop that we support the BigDocument structure.
  143.                         pb->bigDocumentData->PluginUsing32BitCoordinates = true;
  144.                 }
  145.  
  146.                 /* initialise the parameter handle that Photoshop keeps for us */
  147.                 if(!pb->parameters)
  148.                         pb->parameters = PINEWHANDLE(1); // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that.
  149.  
  150.                 wantdialog |= checkandinitparams(pb->parameters);
  151.  
  152.                 /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */
  153.                 if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){
  154.                         if( maindialog(pb) ){
  155.                                 if (!host_preserves_parameters()) {
  156.                                         /* Workaround for GIMP/PSPI, to avoid that formulas vanish when you re-open the main window.
  157.                                            The reason is a bug in PSPI: The host should preserve the value of pb->parameters, which PSPI does not do.
  158.                                            Also, all global variables are unloaded, so the plugin cannot preserve any data.
  159.                                            Workaround in FF 1.7: If the host GIMP is detected, then a special mode will be activated.
  160.                                            This mode saves the filter data into a temporary file "FilterFoundry.afs" and loads it
  161.                                            when the window is opened again. */
  162.                                         // Workaround: Save settings in "FilterFoundry.afs" if the host does not preserve pb->parameters
  163.                                         char outfilename[255];
  164.                                         char* tempdir;
  165.                                         StandardFileReply sfr;
  166.                                         sfr.sfGood = true;
  167.                                         sfr.sfReplacing = true;
  168.                                         sfr.sfType = PS_FILTER_FILETYPE;
  169.  
  170.                                         tempdir = getenv("TMP");
  171.                                         #ifdef WIN_ENV
  172.                                         if (strlen(tempdir) > 0) strcat(tempdir, "\\");
  173.                                         #else
  174.                                         if (strlen(tempdir) > 0) strcat(tempdir, "/");
  175.                                         #endif
  176.                                         sprintf(outfilename, "%sFilterFoundry.afs", tempdir);
  177.  
  178.                                         myc2pstrcpy(sfr.sfFile.name, outfilename);
  179.                                         #ifdef WIN_ENV
  180.                                         sfr.nFileExtension = (WORD)(strlen(outfilename) - strlen(".afs"));
  181.                                         #endif
  182.                                         sfr.sfScript = 0; // FIXME: is that ok?
  183.                                         savefile(&sfr);
  184.                                 }
  185.  
  186.                                 /* update stored parameters from new user settings */
  187.                                 saveparams(pb->parameters);
  188.                         }else
  189.                                 e = userCanceledErr;
  190.                 }
  191.                 wantdialog = false;
  192.  
  193.                 if(!e){
  194.                         if(setup(pb)){
  195.                                 DoStart(pb);
  196.                         }else{
  197.                                 SYSBEEP(1);
  198.                                 e = filterBadParameters;
  199.                         }
  200.                 }
  201.                 break;
  202.         case filterSelectorContinue:
  203.                 e = DoContinue(pb);
  204.                 break;
  205.         case filterSelectorFinish:
  206.                 DoFinish(pb);
  207.                 break;
  208.         default:
  209.                 e = filterBadParameters;
  210.         }
  211.  
  212.         *result = e;
  213.  
  214. #ifdef WIN_ENV
  215.         if (activationContextUsed) DeactivateManifest(&manifestVars);
  216. #endif
  217.  
  218.         ExitCodeResource();
  219. }
  220.  
  221. int checkandinitparams(Handle params){
  222.         char *reasonstr,*reason;
  223.         int i,bUninitializedParams;
  224.         Boolean showdialog;
  225.  
  226.         if (!host_preserves_parameters()) {
  227.                 // Workaround: Load settings in "FilterFoundry.afs" if host does not preserve pb->parameters
  228.                 char outfilename[255];
  229.                 char* tempdir;
  230.                 StandardFileReply sfr;
  231.                 sfr.sfGood = true;
  232.                 sfr.sfReplacing = true;
  233.                 sfr.sfType = PS_FILTER_FILETYPE;
  234.  
  235.                 tempdir = getenv("TMP");
  236.                 #ifdef WIN_ENV
  237.                 if (strlen(tempdir) > 0) strcat(tempdir, "\\");
  238.                 #else
  239.                 if (strlen(tempdir) > 0) strcat(tempdir, "/");
  240.                 #endif
  241.                 sprintf(outfilename, "%sFilterFoundry.afs", tempdir);
  242.  
  243.                 myc2pstrcpy(sfr.sfFile.name, outfilename);
  244.                 #ifdef WIN_ENV
  245.                 sfr.nFileExtension = (WORD)(strlen(outfilename) - strlen(".afs"));
  246.                 #endif
  247.                 sfr.sfScript = 0; // FIXME: is that ok?
  248.                 if (loadfile(&sfr, &reason)) return true;
  249.         }
  250.  
  251.         if( (bUninitializedParams = !(params && readparams(params,false,&reasonstr))) ){
  252.                 /* either the parameter handle was uninitialised,
  253.                    or the parameter data couldn't be read; set default values */
  254.  
  255.                 // see if saved parameters exist
  256.                 gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason,READ_OBFUSC);
  257.  
  258.  
  259.                 if(!gdata->standalone){
  260.                         // no saved settings (not standalone)
  261.                         for(i = 0; i < 8; ++i)
  262.                                 slider[i] = i*10+100;
  263.                         for(i = 0; i < 4; ++i)
  264.                                 if(expr[i])
  265.                                         free(expr[i]);
  266.                         if(gpb->imageMode == plugInModeRGBColor){
  267.                                 expr[0] = _strdup("r");
  268.                                 expr[1] = _strdup("g");
  269.                                 expr[2] = _strdup("b");
  270.                                 expr[3] = _strdup("a");
  271.                         }else{
  272.                                 expr[0] = _strdup("c");
  273.                                 expr[1] = _strdup("c");
  274.                                 expr[2] = _strdup("c");
  275.                                 expr[3] = _strdup("c");
  276.                         }
  277.                 }
  278.         }
  279.  
  280.         // let scripting system change parameters, if we're scripted;
  281.         // user may want to force display of dialog during scripting playback
  282.         switch (ReadScriptParamsOnRead()) {
  283.                 case SCR_SHOW_DIALOG:
  284.                         showdialog = true;
  285.                         break;
  286.                 case SCR_HIDE_DIALOG:
  287.                         showdialog = false;
  288.                         break;
  289.                 default:
  290.                 case SCR_NO_SCRIPT:
  291.                         showdialog = bUninitializedParams;
  292.                         break;
  293.         }
  294.  
  295.         saveparams(params);
  296.  
  297.         return showdialog;
  298. }
  299.  
  300. Boolean host_preserves_parameters() {
  301.         if (gpb->hostSig == HOSTSIG_GIMP) return false;
  302.         if (gpb->hostSig == HOSTSIG_IRFANVIEW) return false;
  303.  
  304.         /*
  305.         char x[100];
  306.         sprintf(x, "Host Signature: %u", gpb->hostSig);
  307.         simplealert(x);
  308.         */
  309.  
  310.         // We just assume the other hosts preserve the parameters
  311.         return true;
  312. }
  313.  
  314. int64_t maxspace(){
  315.         // Please see "Hosts.md" for details about the MaxSpace implementations of tested plugins
  316.  
  317.         // Plugins that don't support MaxSpace64 shall set the field to zero; then we will use MaxSpace instead.
  318.         // Also check "gpb->bufferProcs->numBufferProcs" to see if 64 bit API is available
  319.         if ((gpb->bufferProcs->numBufferProcs >= 8) && (gpb->maxSpace64 > 0)) {
  320.                 uint64_t maxSpace64 = gpb->maxSpace64;
  321.  
  322.                 return maxSpace64;
  323.         } else {
  324.                 // 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.
  325.                 unsigned int maxSpace32 = (unsigned int) gpb->maxSpace;
  326.                 uint64_t maxSpace64 = maxSpace32;
  327.  
  328.                 if (gpb->hostSig == HOSTSIG_IRFANVIEW) maxSpace64 *= 1024; // IrfanView is giving Kilobytes instead of Bytes
  329.                 //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???
  330.  
  331.                 return maxSpace64;
  332.         }
  333. }
  334.  
  335. Boolean maxspace_available() {
  336.         // Please see "Hosts.md" for details about the MaxSpace implementations of tested plugins
  337.  
  338.         // GIMP PSPI sets MaxSpace to hardcoded 100 MB
  339.         if (gpb->hostSig == HOSTSIG_GIMP) return false;
  340.  
  341.         // HOSTSIG_PAINT_NET sets MaxSpace to hardcoded 1 GB, see https://github.com/0xC0000054/PSFilterPdn/issues/5
  342.         // Comment by the host author "This was done to avoid any compatibility issues with plugins handling 2 GB - 1"
  343.         if (gpb->hostSig == HOSTSIG_PAINT_NET) return false;
  344.  
  345.         return true;
  346. }
  347.  
  348. void DoPrepare(FilterRecordPtr pb){
  349.         int i;
  350.  
  351.         for(i = 4; i--;){
  352.                 if(expr[i]||tree[i]) DBG("expr[] or tree[] non-NULL in Prepare!");
  353.                 expr[i] = NULL;
  354.                 tree[i] = NULL;
  355.         }
  356.  
  357.         // Commented out by DM, 18 Dec 2018:
  358.         // This code did not work on systems with 8 GB RAM:
  359.         /*
  360.         long space = (pb->maxSpace*9)/10; // don't ask for more than 90% of available memory
  361.  
  362.         maxSpace = 512L<<10; // this is a wild guess, actually
  363.         if(maxSpace > space)
  364.                 maxSpace = space;
  365.         pb->maxSpace = maxSpace;
  366.         */
  367.  
  368.         // New variant:
  369.         if (maxspace_available()) {
  370.                 pb->maxSpace = (int32)ceil((maxspace()/10.)*9); // don't ask for more than 90% of available memory
  371.                 // FIXME: Also maxSpace64
  372.         }
  373. }
  374.  
  375. void RequestNext(FilterRecordPtr pb,long toprow){
  376.         /* Request next block of the image */
  377.  
  378.         pb->inLoPlane = pb->outLoPlane = 0;
  379.         pb->inHiPlane = pb->outHiPlane = nplanes-1;
  380.  
  381.         if (HAS_BIG_DOC(pb)) {
  382.                 // if any of the formulae involve random access to image pixels,
  383.                 // ask for the entire image
  384.                 if (needall) {
  385.                         SETRECT(BIGDOC_IN_RECT(pb), 0, 0, BIGDOC_IMAGE_SIZE(pb).h, BIGDOC_IMAGE_SIZE(pb).v);
  386.                 } else {
  387.                         // TODO: This does not work with GIMP. So, if we are using GIMP, we should
  388.                         //       somehow always use "needall=true", and/or find out why this doesn't work
  389.                         //       with GIMP.
  390.  
  391.                         // otherwise, process the filtered area, by chunksize parts
  392.                         BIGDOC_IN_RECT(pb).left = BIGDOC_FILTER_RECT(pb).left;
  393.                         BIGDOC_IN_RECT(pb).right = BIGDOC_FILTER_RECT(pb).right;
  394.                         BIGDOC_IN_RECT(pb).top = (int32)toprow;
  395.                         BIGDOC_IN_RECT(pb).bottom = (int32)MIN(toprow + chunksize, BIGDOC_FILTER_RECT(pb).bottom);
  396.  
  397.                         if (cnvused) {
  398.                                 // cnv() needs one extra pixel in each direction
  399.                                 if (BIGDOC_IN_RECT(pb).left > 0)
  400.                                         --BIGDOC_IN_RECT(pb).left;
  401.                                 if (BIGDOC_IN_RECT(pb).right < BIGDOC_IMAGE_SIZE(pb).h)
  402.                                         ++BIGDOC_IN_RECT(pb).right;
  403.                                 if (BIGDOC_IN_RECT(pb).top > 0)
  404.                                         --BIGDOC_IN_RECT(pb).top;
  405.                                 if (BIGDOC_IN_RECT(pb).bottom < BIGDOC_IMAGE_SIZE(pb).v)
  406.                                         ++BIGDOC_IN_RECT(pb).bottom;
  407.                         }
  408.                 }
  409.                 BIGDOC_OUT_RECT(pb) = BIGDOC_FILTER_RECT(pb);
  410.                 /*
  411.                 {char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
  412.                         needall,
  413.                         BIGDOC_IN_RECT(pb).left,BIGDOC_IN_RECT(pb).top,BIGDOC_IN_RECT(pb).right,BIGDOC_IN_RECT(pb).bottom,
  414.                         BIGDOC_FILTER_RECT(pb).left,BIGDOC_FILTER_RECT(pb).top,BIGDOC_FILTER_RECT(pb).right,BIGDOC_FILTER_RECT(pb).bottom);dbg(s);}
  415.                 */
  416.         } else {
  417.                 // if any of the formulae involve random access to image pixels,
  418.                 // ask for the entire image
  419.                 if (needall) {
  420.                         SETRECT(IN_RECT(pb), 0, 0, IMAGE_SIZE(pb).h, IMAGE_SIZE(pb).v);
  421.                 }
  422.                 else {
  423.                         // TODO: This does not work with GIMP. So, if we are using GIMP, we should
  424.                         //       somehow always use "needall=true", and/or find out why this doesn't work
  425.                         //       with GIMP.
  426.  
  427.                         // otherwise, process the filtered area, by chunksize parts
  428.                         IN_RECT(pb).left = FILTER_RECT(pb).left;
  429.                         IN_RECT(pb).right = FILTER_RECT(pb).right;
  430.                         IN_RECT(pb).top = (int16)toprow;
  431.                         IN_RECT(pb).bottom = (int16)MIN(toprow + chunksize, FILTER_RECT(pb).bottom);
  432.  
  433.                         if (cnvused) {
  434.                                 // cnv() needs one extra pixel in each direction
  435.                                 if (IN_RECT(pb).left > 0)
  436.                                         --IN_RECT(pb).left;
  437.                                 if (IN_RECT(pb).right < IMAGE_SIZE(pb).h)
  438.                                         ++IN_RECT(pb).right;
  439.                                 if (IN_RECT(pb).top > 0)
  440.                                         --IN_RECT(pb).top;
  441.                                 if (IN_RECT(pb).bottom < IMAGE_SIZE(pb).v)
  442.                                         ++IN_RECT(pb).bottom;
  443.                         }
  444.                 }
  445.                 OUT_RECT(pb) = FILTER_RECT(pb);
  446.                 /*
  447.                 {char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
  448.                         needall,
  449.                         IN_RECT(pb).left,IN_RECT(pb).top,IN_RECT(pb).right,IN_RECT(pb).bottom,
  450.                         FILTER_RECT(pb).left,FILTER_RECT(pb).top,FILTER_RECT(pb).right,FILTER_RECT(pb).bottom);dbg(s);}
  451.                 */
  452.         }
  453. }
  454.  
  455. void DoStart(FilterRecordPtr pb){
  456. //dbg("DoStart");
  457.         /* if src() or rad() functions are used, random access to the image data is required,
  458.            so we must request the entire image in a single chunk. */
  459.         if (HAS_BIG_DOC(pb)) {
  460.                 chunksize = needall ? (BIGDOC_FILTER_RECT(pb).bottom - BIGDOC_FILTER_RECT(pb).top) : CHUNK_ROWS;
  461.                 toprow = BIGDOC_FILTER_RECT(pb).top;
  462.         } else {
  463.                 chunksize = needall ? (FILTER_RECT(pb).bottom - FILTER_RECT(pb).top) : CHUNK_ROWS;
  464.                 toprow = FILTER_RECT(pb).top;
  465.         }
  466.         RequestNext(pb, toprow);
  467. }
  468.  
  469. OSErr DoContinue(FilterRecordPtr pb){
  470.         OSErr e = noErr;
  471.         long outoffset;
  472.  
  473.         if (HAS_BIG_DOC(pb)) {
  474.                 VRect fr;
  475.                 if (needall) {
  476.                         fr = BIGDOC_FILTER_RECT(pb);  // filter whole selection at once
  477.                 } else if (cnvused) {
  478.                         // we've requested one pixel extra all around
  479.                         // (see RequestNext()), just for access purposes. But filter
  480.                         // original selection only.
  481.                         fr.left = BIGDOC_FILTER_RECT(pb).left;
  482.                         fr.right = BIGDOC_FILTER_RECT(pb).right;
  483.                         fr.top = toprow;
  484.                         fr.bottom = MIN(toprow + chunksize, BIGDOC_FILTER_RECT(pb).bottom);
  485.                 } else {  // filter whatever portion we've been given
  486.                         fr = BIGDOC_IN_RECT(pb);
  487.                 }
  488.  
  489.                 outoffset = (long)pb->outRowBytes * (fr.top - BIGDOC_OUT_RECT(pb).top)
  490.                         + (long)nplanes * (fr.left - BIGDOC_OUT_RECT(pb).left);
  491.  
  492.                 if (!(e = process_scaled_bigdoc(pb, true, fr, fr,
  493.                         (Ptr)pb->outData + outoffset, pb->outRowBytes, 1.)))
  494.                 {
  495.                         toprow += chunksize;
  496.                         if (toprow < BIGDOC_FILTER_RECT(pb).bottom)
  497.                                 RequestNext(pb, toprow);
  498.                         else {
  499.                                 SETRECT(BIGDOC_IN_RECT(pb), 0, 0, 0, 0);
  500.                                 BIGDOC_OUT_RECT(pb) = BIGDOC_MASK_RECT(pb) = BIGDOC_IN_RECT(pb);
  501.                         }
  502.                 }
  503.         } else {
  504.                 Rect fr;
  505.                 if (needall) {
  506.                         fr = FILTER_RECT(pb);  // filter whole selection at once
  507.                 } else if (cnvused) {
  508.                         // we've requested one pixel extra all around
  509.                         // (see RequestNext()), just for access purposes. But filter
  510.                         // original selection only.
  511.                         fr.left = FILTER_RECT(pb).left;
  512.                         fr.right = FILTER_RECT(pb).right;
  513.                         fr.top = toprow;
  514.                         fr.bottom = MIN(toprow + chunksize, FILTER_RECT(pb).bottom);
  515.                 } else {  // filter whatever portion we've been given
  516.                         fr = IN_RECT(pb);
  517.                 }
  518.  
  519.                 outoffset = (long)pb->outRowBytes*(fr.top - OUT_RECT(pb).top)
  520.                                         + (long)nplanes*(fr.left - OUT_RECT(pb).left);
  521.  
  522.                 if(!(e = process_scaled_olddoc(pb, true, fr, fr,
  523.                                         (Ptr)pb->outData+outoffset, pb->outRowBytes, 1.)))
  524.                 {
  525.                         toprow += chunksize;
  526.                         if(toprow < FILTER_RECT(pb).bottom)
  527.                                 RequestNext(pb,toprow);
  528.                         else{
  529.                                 SETRECT(IN_RECT(pb),0,0,0,0);
  530.                                 OUT_RECT(pb) = MASK_RECT(pb) = IN_RECT(pb);
  531.                         }
  532.                 }
  533.         }
  534.         return e;
  535. }
  536.  
  537. void DoFinish(FilterRecordPtr pb){
  538.         int i;
  539.  
  540.         WriteScriptParamsOnRead();
  541.  
  542.         for(i = 4; i--;){
  543.                 freetree(tree[i]);
  544.                 if(expr[i]) free(expr[i]);
  545.         }
  546. }
  547.