Subversion Repositories filter_foundry

Rev

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