Subversion Repositories filter_foundry

Rev

Rev 94 | Rev 103 | 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 != formatSelectorAbout && !*data){
  58.                 BufferID tempId;
  59.                 if( (*result = PS_BUFFER_ALLOC(sizeof(globals_t), &tempId)) )
  60.                         return;
  61.                 gdata = (globals_t*)*data = (intptr_t)PS_BUFFER_LOCK(tempId, true);
  62.                 gdata->standalone = gdata->parmloaded = false;
  63.         }else
  64.                 gdata = (globals_t*)*data;
  65.  
  66.         EnterCodeResource();
  67.  
  68.         gpb = pb;
  69.  
  70.         nplanes = MIN(pb->planes,4);
  71.  
  72.         switch (selector){
  73.         case filterSelectorAbout:
  74.                 if(gdata && !gdata->parmloaded)
  75.                         gdata->standalone = gdata->parmloaded = readPARMresource(hDllInstance,&reason,1);
  76.                 DoAbout((AboutRecordPtr)pb);
  77.                 break;
  78.         case filterSelectorParameters:
  79.                 wantdialog = true;
  80.                 break;
  81.         case filterSelectorPrepare:
  82.                 DoPrepare(pb);
  83.                 init_symtab(predefs); // ready for parser calls
  84.                 break;
  85.         case filterSelectorStart:
  86.                 /* initialise the parameter handle that Photoshop keeps for us */
  87.                 if(!pb->parameters)
  88.                         pb->parameters = PINEWHANDLE(0);
  89.  
  90.                 wantdialog |= checkandinitparams(pb->parameters);
  91.  
  92.                 /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */
  93.                 if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){
  94.                         if( maindialog(pb) ){
  95.                                 /* update stored parameters from new user settings */
  96.                                 saveparams(pb->parameters);
  97.                         }else
  98.                                 e = userCanceledErr;
  99.                 }
  100.                 wantdialog = false;
  101.  
  102.                 if(!e){
  103.                         if(setup(pb)){
  104.                                 DoStart(pb);
  105.                         }else{
  106.                                 SYSBEEP(1);
  107.                                 e = filterBadParameters;
  108.                         }
  109.                 }
  110.                 break;
  111.         case filterSelectorContinue:
  112.                 e = DoContinue(pb);
  113.                 break;
  114.         case filterSelectorFinish:
  115.                 DoFinish(pb);
  116.                 break;
  117.         default:
  118.                 e = filterBadParameters;
  119.         }
  120.                
  121.         *result = e;
  122.  
  123.         ExitCodeResource();
  124. }
  125.  
  126. int checkandinitparams(Handle params){
  127.         char *reasonstr,*reason;
  128.         int i,f,showdialog;
  129.        
  130.         if( (f = !(params && readparams(params,false,&reasonstr))) ){
  131.                 /* either the parameter handle was uninitialised,
  132.                    or the parameter data couldn't be read; set default values */
  133.  
  134.                 // see if saved parameters exist
  135.                 gdata->standalone = gdata->parmloaded = readPARMresource(hDllInstance,&reason,1);
  136.  
  137.                 if(!gdata->standalone){
  138.                         // no saved settings (not standalone)
  139.                         for(i = 0; i < 8; ++i)
  140.                                 slider[i] = i*10+100;
  141.                         if(gpb->imageMode == plugInModeRGBColor){
  142.                                 expr[0] = my_strdup("r");
  143.                                 expr[1] = my_strdup("g");
  144.                                 expr[2] = my_strdup("b");
  145.                                 expr[3] = my_strdup("a");
  146.                         }else{
  147.                                 expr[0] = my_strdup("c");
  148.                                 expr[1] = my_strdup("c");
  149.                                 expr[2] = my_strdup("c");
  150.                                 expr[3] = my_strdup("c");
  151.                         }
  152.                 }
  153.         }
  154.        
  155.         // let scripting system change parameters, if we're scripted;
  156.         // user may want to force display of dialog during scripting playback
  157.         showdialog = ReadScriptParamsOnRead();
  158.  
  159.         saveparams(params);
  160.         return f || showdialog;
  161. }
  162.  
  163. void DoPrepare(FilterRecordPtr pb){
  164.         int i;
  165.  
  166.         for(i = 4; i--;){
  167.                 if(expr[i]||tree[i]) DBG("expr[] or tree[] non-NULL in Prepare!");
  168.                 expr[i] = NULL;
  169.                 tree[i] = NULL;
  170.         }
  171.         maxSpace = pb->maxSpace;
  172. }
  173.  
  174. void RequestNext(FilterRecordPtr pb,long toprow){
  175.         /* Request next block of the image */
  176.  
  177.         pb->inLoPlane = pb->outLoPlane = 0;
  178.         pb->inHiPlane = pb->outHiPlane = nplanes-1;
  179.  
  180.         // if any of the formulae involve random access to image pixels,
  181.         // ask for the entire image
  182.         if(needall){
  183.                 SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
  184.         }else{
  185.                 // otherwise, process the filtered area, by chunksize parts
  186.                 pb->inRect.left = pb->filterRect.left;
  187.                 pb->inRect.right = pb->filterRect.right;
  188.                 pb->inRect.top = toprow;
  189.                 pb->inRect.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
  190.                
  191.                 if(cnvused){
  192.                         // cnv() needs one extra pixel in each direction
  193.                         if(pb->inRect.left > 0)
  194.                                 --pb->inRect.left;
  195.                         if(pb->inRect.right < pb->imageSize.h)
  196.                                 ++pb->inRect.right;
  197.                         if(pb->inRect.top > 0)
  198.                                 --pb->inRect.top;
  199.                         if(pb->inRect.bottom < pb->imageSize.v)
  200.                                 ++pb->inRect.bottom;
  201.                 }
  202.         }
  203.         pb->outRect = pb->filterRect;
  204. /*
  205. {char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
  206.         needall,
  207.         pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom,
  208.         pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom);dbg(s);}
  209. */
  210. }
  211.  
  212. void DoStart(FilterRecordPtr pb){
  213. //dbg("DoStart");
  214.         /* if src() or rad() functions are used, random access to the image data is required,
  215.            so we must request the entire image in a single chunk. */
  216.         chunksize = needall ? (pb->filterRect.bottom - pb->filterRect.top) : CHUNK_ROWS;
  217.         toprow = pb->filterRect.top;
  218.         RequestNext(pb,toprow);
  219. }
  220.  
  221. OSErr DoContinue(FilterRecordPtr pb){
  222.         OSErr e = noErr;
  223.         Rect fr;
  224.         long outoffset;
  225.  
  226.         if(needall)
  227.                 fr = pb->filterRect;  // filter whole selection at once
  228.         else if(cnvused){
  229.                 // we've requested one pixel extra all around
  230.                 // (see RequestNext()), just for access purposes. But filter
  231.                 // original selection only.
  232.                 fr.left = pb->filterRect.left;
  233.                 fr.right = pb->filterRect.right;
  234.                 fr.top = toprow;
  235.                 fr.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
  236.         }else  // filter whatever portion we've been given
  237.                 fr = pb->inRect;
  238.  
  239.         outoffset = (long)pb->outRowBytes*(fr.top - pb->outRect.top)
  240.                                 + (long)nplanes*(fr.left - pb->outRect.left);
  241.  
  242.         if(!(e = process_scaled(pb, true, &fr, &fr,
  243.                                 (Ptr)pb->outData+outoffset, pb->outRowBytes, 1.)))
  244.         {
  245.                 toprow += chunksize;
  246.                 if(toprow < pb->filterRect.bottom)
  247.                         RequestNext(pb,toprow);
  248.                 else{
  249.                         SETRECT(pb->inRect,0,0,0,0);
  250.                         pb->outRect = pb->maskRect = pb->inRect;
  251.                 }
  252.         }
  253.         return e;
  254. }
  255.  
  256. void DoFinish(FilterRecordPtr pb){
  257.         int i;
  258.  
  259.         WriteScriptParamsOnRead();
  260.  
  261.         for(i = 4; i--;){
  262.                 freetree(tree[i]);
  263.                 if(expr[i]) free(expr[i]);
  264.         }
  265. }
  266.