Subversion Repositories filter_foundry

Rev

Rev 23 | Rev 62 | 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-5 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,srcradused,chunksize,toprow;
  32. value_type slider[8],cell[0x100],map[4][0x100];
  33. char *expr[4],*defaultexpr[]={"r","g","b","a"};
  34. long maxSpace;
  35.  
  36. #ifdef MAC_ENV
  37. #define hDllInstance NULL /* fake this Windows-only global */
  38. #endif
  39.  
  40. extern struct sym_rec predefs[];
  41. extern int nplanes,varused[],srcradused;
  42.  
  43. globals_t *gdata;
  44. FilterRecordPtr gpb;
  45.  
  46. int checkandinitparams(Handle params);
  47.  
  48. DLLEXPORT MACPASCAL
  49. void ENTRYPOINT(short selector,FilterRecordPtr pb,long *data,short *result){
  50.         OSErr e = noErr;
  51.         static Boolean wantdialog = false;
  52.        
  53.         EnterCodeResource();
  54.        
  55.         gpb = pb;
  56.        
  57.         if(!*data){
  58.                 gdata = (globals_t*)( *data = (long)malloc(sizeof(globals_t)) ) ;
  59.                 gdata->standalone = gdata->parmloaded = false;
  60.         }else
  61.                 gdata = (globals_t*)*data;
  62.        
  63.         nplanes = MIN(pb->planes,4);
  64.  
  65.         switch (selector){
  66.         case filterSelectorAbout:
  67.                 DoAbout((AboutRecordPtr)pb);
  68.                 break;
  69.         case filterSelectorParameters:
  70. //              dbg("filterSelectorParameters");
  71.                 wantdialog = true;
  72.                 break;
  73.         case filterSelectorPrepare:
  74. //              dbg("filterSelectorPrepare");
  75.                 DoPrepare(pb);
  76.                 init_symtab(predefs); // ready for parser calls
  77.                 break;
  78.         case filterSelectorStart:
  79. //              dbg("filterSelectorStart");
  80.  
  81.                 if(!pb->parameters){
  82.                         //dbg("pb->parameters = PINEWHANDLE(0)"),
  83.                         pb->parameters = PINEWHANDLE(0); /* initialise the parameter handle that Photoshop keeps for us */
  84.                         if(!pb->parameters) dbg("filterSelectorStart NULL handle @ PINEWHANDLE(0)");
  85.                 }
  86.  
  87.                 wantdialog |= checkandinitparams(pb->parameters);
  88.  
  89.                 /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */
  90.                 if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){
  91.                         if( maindialog(pb) ){
  92.                                 //dbg("maindialog OK!");
  93.                                 /* update stored parameters from new user settings */
  94.                                 saveparams(pb->parameters);
  95.                         }else
  96.                                 e = userCanceledErr;
  97.                 }
  98.                 wantdialog = false;
  99.  
  100.                 if(!e){
  101.                         if(setup(pb)){
  102.                                 DoStart(pb);
  103.                         }else{
  104.                                 SYSBEEP(1);
  105.                                 e = filterBadParameters;
  106.                         }
  107.                 }
  108.                 break;
  109.         case filterSelectorContinue:
  110.                 e = DoContinue(pb);
  111.                 break;
  112.         case filterSelectorFinish:
  113.                 DoFinish(pb);
  114.                 break;
  115.         default:
  116.                 e = filterBadParameters;
  117.         }
  118.                
  119.         *result = e;
  120.  
  121.         ExitCodeResource();
  122. }
  123.  
  124. int checkandinitparams(Handle params){
  125.         char *reasonstr,*reason;
  126.         int i,f,showdialog;
  127.        
  128.         if( (f = !(params && readparams(params,false,&reasonstr))) ){
  129.                 /* either the parameter handle was uninitialised,
  130.                    or the parameter data couldn't be read; set default values */
  131.  
  132.                 if(readPARMresource(hDllInstance,&reason))
  133.                         gdata->standalone = true;
  134.                 else{
  135.                         // no saved settings (not standalone)
  136.                         for(i=0;i<8;++i)
  137.                                 slider[i] = i*10+100;
  138.                         for(i=4;i--;)
  139.                                 expr[i] = my_strdup(defaultexpr[i]);
  140.                 }
  141.         }
  142.        
  143.         // let scripting system change parameters, if we're scripted
  144.         // user may want to force display of dialog during scripting playback
  145.         showdialog = ReadScriptParamsOnRead();
  146.  
  147.         /* sanity check for NULL expression pointers (?) */
  148.         for(i=4;i--;)
  149.                 if(!expr[i]) expr[i] = my_strdup(defaultexpr[i]);
  150.  
  151.         saveparams(params); /* keep what we got */
  152.         return f || showdialog;
  153. }
  154.  
  155. void DoPrepare(FilterRecordPtr pb){
  156.         int i;
  157.  
  158.         for(i=4;i--;){
  159.                 if(expr[i]||tree[i]) DBG("expr[] or tree[] non-NULL in Prepare!");
  160.                 expr[i] = NULL;
  161.                 tree[i] = NULL;
  162.         }
  163.         maxSpace = pb->maxSpace;
  164. }
  165.  
  166. void RequestNext(FilterRecordPtr pb,long toprow){
  167.         /* Request next block of the image */
  168.  
  169.         pb->inLoPlane = pb->outLoPlane = 0;
  170.         pb->inHiPlane = pb->outHiPlane = nplanes-1;
  171.  
  172.         if(srcradused){
  173.                 SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
  174.         }else{
  175.                 pb->inRect.left = pb->filterRect.left;
  176.                 pb->inRect.right = pb->filterRect.right;
  177.                 pb->inRect.top = toprow;
  178.                 pb->inRect.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
  179.         }
  180.         pb->outRect = pb->inRect;
  181. /*
  182. {char s[0x100];sprintf(s,"RequestNext srcradused=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
  183.         srcradused,
  184.         pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom,
  185.         pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom);dbg(s);}
  186. */
  187. }
  188.  
  189. void DoStart(FilterRecordPtr pb){
  190. //dbg("DoStart");
  191.         /* if src() or rad() functions are used, random access to the image data is required,
  192.            so we must request the entire image in a single chunk. */
  193.         chunksize = srcradused ? (pb->filterRect.bottom - pb->filterRect.top) : CHUNK_ROWS;
  194.         toprow = pb->filterRect.top;
  195.         RequestNext(pb,toprow);
  196. }
  197.  
  198. OSErr DoContinue(FilterRecordPtr pb){
  199.         OSErr e = noErr;
  200.         Rect *fr = srcradused ? &pb->filterRect : &pb->inRect;
  201.         long outoffset = (long)pb->outRowBytes*(fr->top - pb->outRect.top)
  202.                                          + (long)nplanes*(fr->left - pb->outRect.left);
  203.  
  204. //      {char s[0x100];sprintf(s,"DoContinue: outoffset=%d\n",outoffset);dbg(s);}
  205.        
  206.         if(!(e = process_scaled(pb,true,
  207.                                 &pb->inRect,
  208.                                 fr,
  209.                                 fr,//&pb->outRect,
  210.                                 (Ptr)pb->outData+outoffset,pb->outRowBytes, 1.))){
  211.                 toprow += chunksize;
  212.                 if(toprow < pb->filterRect.bottom)
  213.                         RequestNext(pb,toprow);
  214.                 else{
  215.                         SETRECT(pb->inRect,0,0,0,0);
  216.                         pb->outRect = pb->maskRect = pb->inRect;
  217.                 }
  218.         }
  219.         return e;
  220. }
  221.  
  222. void DoFinish(FilterRecordPtr pb){
  223.         int i;
  224.        
  225.         WriteScriptParamsOnRead();
  226.  
  227.         for(i=4;i--;){
  228.                 freetree(tree[i]);
  229.                 if(expr[i]) free(expr[i]);
  230.         }
  231. }
  232.