Subversion Repositories filter_foundry

Rev

Rev 106 | Rev 146 | 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-9 Toby Thain, toby@telegraphics.com.au
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. //#include <stdio.h>
  21. //#include <sound.h>
  22.  
  23. #include "ff.h"
  24.  
  25. #include "node.h"
  26. #include "y.tab.h"
  27. #include "scripting.h"
  28.  
  29. struct node *tree[4];
  30. char *err[4];
  31. int errpos[4],errstart[4],nplanes,cnvused,chunksize,toprow;
  32. value_type slider[8],cell[0x100],map[4][0x100];
  33. char *expr[4];
  34. // long maxSpace;
  35. globals_t *gdata;
  36. FilterRecordPtr gpb;
  37.  
  38. #ifdef MAC_ENV
  39.         #define hDllInstance NULL /* fake this Windows-only global */
  40. #endif
  41.  
  42. extern struct sym_rec predefs[];
  43. extern int nplanes,varused[];
  44.  
  45. int checkandinitparams(Handle params);
  46.  
  47. // MPW MrC requires prototype
  48. DLLEXPORT MACPASCAL
  49. void ENTRYPOINT(short selector,FilterRecordPtr pb,intptr_t *data,short *result);
  50.  
  51. DLLEXPORT MACPASCAL
  52. void ENTRYPOINT(short selector, FilterRecordPtr pb, intptr_t *data, short *result){
  53.         static Boolean wantdialog = false;
  54.         OSErr e = noErr;
  55.         char *reason;
  56.  
  57.         if(selector != filterSelectorAbout && !*data){
  58.                 BufferID tempId;
  59.                 if( (*result = PS_BUFFER_ALLOC(sizeof(globals_t), &tempId)) )
  60.                         return;
  61.                 *data = (intptr_t)PS_BUFFER_LOCK(tempId, true);
  62.                 gdata = (globals_t*)*data;
  63.                 gdata->standalone = gdata->parmloaded = false;
  64.         }else
  65.                 gdata = (globals_t*)*data;
  66.  
  67.         EnterCodeResource();
  68.  
  69.         gpb = pb;
  70.  
  71.         nplanes = MIN(pb->planes,4);
  72.  
  73.         switch (selector){
  74.         case filterSelectorAbout:
  75.                 if(gdata && !gdata->parmloaded)
  76.                         gdata->standalone = gdata->parmloaded = readPARMresource(hDllInstance,&reason,1);
  77.                 DoAbout((AboutRecordPtr)pb);
  78.                 break;
  79.         case filterSelectorParameters:
  80.                 wantdialog = true;
  81.                 break;
  82.         case filterSelectorPrepare:
  83.                 DoPrepare(pb);
  84.                 init_symtab(predefs); // ready for parser calls
  85.                 init_trigtab();
  86.                 break;
  87.         case filterSelectorStart:
  88.                 /* initialise the parameter handle that Photoshop keeps for us */
  89.                 if(!pb->parameters)
  90.                         pb->parameters = PINEWHANDLE(0);
  91.  
  92.                 wantdialog |= checkandinitparams(pb->parameters);
  93.  
  94.                 /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */
  95.                 if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){
  96.                         if( maindialog(pb) ){
  97.                                 /* update stored parameters from new user settings */
  98.                                 saveparams(pb->parameters);
  99.                         }else
  100.                                 e = userCanceledErr;
  101.                 }
  102.                 wantdialog = false;
  103.  
  104.                 if(!e){
  105.                         if(setup(pb)){
  106.                                 DoStart(pb);
  107.                         }else{
  108.                                 SYSBEEP(1);
  109.                                 e = filterBadParameters;
  110.                         }
  111.                 }
  112.                 break;
  113.         case filterSelectorContinue:
  114.                 e = DoContinue(pb);
  115.                 break;
  116.         case filterSelectorFinish:
  117.                 DoFinish(pb);
  118.                 break;
  119.         default:
  120.                 e = filterBadParameters;
  121.         }
  122.  
  123.         *result = e;
  124.  
  125.         ExitCodeResource();
  126. }
  127.  
  128. int checkandinitparams(Handle params){
  129.         char *reasonstr,*reason;
  130.         int i,f,showdialog;
  131.  
  132.         if( (f = !(params && readparams(params,false,&reasonstr))) ){
  133.                 /* either the parameter handle was uninitialised,
  134.                    or the parameter data couldn't be read; set default values */
  135.  
  136.                 // see if saved parameters exist
  137.                 gdata->standalone = gdata->parmloaded = readPARMresource(hDllInstance,&reason,1);
  138.  
  139.                 if(!gdata->standalone){
  140.                         // no saved settings (not standalone)
  141.                         for(i = 0; i < 8; ++i)
  142.                                 slider[i] = i*10+100;
  143.                         if(gpb->imageMode == plugInModeRGBColor){
  144.                                 expr[0] = my_strdup("r");
  145.                                 expr[1] = my_strdup("g");
  146.                                 expr[2] = my_strdup("b");
  147.                                 expr[3] = my_strdup("a");
  148.                         }else{
  149.                                 expr[0] = my_strdup("c");
  150.                                 expr[1] = my_strdup("c");
  151.                                 expr[2] = my_strdup("c");
  152.                                 expr[3] = my_strdup("c");
  153.                         }
  154.                 }
  155.         }
  156.  
  157.         // let scripting system change parameters, if we're scripted;
  158.         // user may want to force display of dialog during scripting playback
  159.         showdialog = ReadScriptParamsOnRead();
  160.  
  161.         saveparams(params);
  162.         return f || showdialog;
  163. }
  164.  
  165. void DoPrepare(FilterRecordPtr pb){
  166.         int i;
  167.  
  168.         for(i = 4; i--;){
  169.                 if(expr[i]||tree[i]) DBG("expr[] or tree[] non-NULL in Prepare!");
  170.                 expr[i] = NULL;
  171.                 tree[i] = NULL;
  172.         }
  173.  
  174.         // Commented out by DM, 18 Dec 2018:
  175.         // This code did not work on systems with 8 GB RAM:
  176.         /*
  177.         long space = (pb->maxSpace*9)/10; // don't ask for more than 90% of available memory
  178.  
  179.         maxSpace = 512L<<10; // this is a wild guess, actually
  180.         if(maxSpace > space)
  181.                 maxSpace = space;
  182.         pb->maxSpace = maxSpace;
  183.         */
  184.  
  185.         // New variant:
  186.         // TODO: Programmatically test if host supports pb->maxSpace64, and if it does so, use this value instead.
  187.         pb->maxSpace = ((double)pb->maxSpace/10)*9; // don't ask for more than 90% of available memory
  188. }
  189.  
  190. void RequestNext(FilterRecordPtr pb,long toprow){
  191.         /* Request next block of the image */
  192.  
  193.         pb->inLoPlane = pb->outLoPlane = 0;
  194.         pb->inHiPlane = pb->outHiPlane = nplanes-1;
  195.  
  196.         // if any of the formulae involve random access to image pixels,
  197.         // ask for the entire image
  198.         if(needall){
  199.                 SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
  200.         }else{
  201.                 // otherwise, process the filtered area, by chunksize parts
  202.                 pb->inRect.left = pb->filterRect.left;
  203.                 pb->inRect.right = pb->filterRect.right;
  204.                 pb->inRect.top = toprow;
  205.                 pb->inRect.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
  206.  
  207.                 if(cnvused){
  208.                         // cnv() needs one extra pixel in each direction
  209.                         if(pb->inRect.left > 0)
  210.                                 --pb->inRect.left;
  211.                         if(pb->inRect.right < pb->imageSize.h)
  212.                                 ++pb->inRect.right;
  213.                         if(pb->inRect.top > 0)
  214.                                 --pb->inRect.top;
  215.                         if(pb->inRect.bottom < pb->imageSize.v)
  216.                                 ++pb->inRect.bottom;
  217.                 }
  218.         }
  219.         pb->outRect = pb->filterRect;
  220. /*
  221. {char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
  222.         needall,
  223.         pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom,
  224.         pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom);dbg(s);}
  225. */
  226. }
  227.  
  228. void DoStart(FilterRecordPtr pb){
  229. //dbg("DoStart");
  230.         /* if src() or rad() functions are used, random access to the image data is required,
  231.            so we must request the entire image in a single chunk. */
  232.         chunksize = needall ? (pb->filterRect.bottom - pb->filterRect.top) : CHUNK_ROWS;
  233.         toprow = pb->filterRect.top;
  234.         RequestNext(pb,toprow);
  235. }
  236.  
  237. OSErr DoContinue(FilterRecordPtr pb){
  238.         OSErr e = noErr;
  239.         Rect fr;
  240.         long outoffset;
  241.  
  242.         if(needall)
  243.                 fr = pb->filterRect;  // filter whole selection at once
  244.         else if(cnvused){
  245.                 // we've requested one pixel extra all around
  246.                 // (see RequestNext()), just for access purposes. But filter
  247.                 // original selection only.
  248.                 fr.left = pb->filterRect.left;
  249.                 fr.right = pb->filterRect.right;
  250.                 fr.top = toprow;
  251.                 fr.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
  252.         }else  // filter whatever portion we've been given
  253.                 fr = pb->inRect;
  254.  
  255.         outoffset = (long)pb->outRowBytes*(fr.top - pb->outRect.top)
  256.                                 + (long)nplanes*(fr.left - pb->outRect.left);
  257.  
  258.         if(!(e = process_scaled(pb, true, &fr, &fr,
  259.                                 (Ptr)pb->outData+outoffset, pb->outRowBytes, 1.)))
  260.         {
  261.                 toprow += chunksize;
  262.                 if(toprow < pb->filterRect.bottom)
  263.                         RequestNext(pb,toprow);
  264.                 else{
  265.                         SETRECT(pb->inRect,0,0,0,0);
  266.                         pb->outRect = pb->maskRect = pb->inRect;
  267.                 }
  268.         }
  269.         return e;
  270. }
  271.  
  272. void DoFinish(FilterRecordPtr pb){
  273.         int i;
  274.  
  275.         WriteScriptParamsOnRead();
  276.  
  277.         for(i = 4; i--;){
  278.                 freetree(tree[i]);
  279.                 if(expr[i]) free(expr[i]);
  280.         }
  281. }
  282.