Subversion Repositories filter_foundry

Rev

Rev 18 | Rev 66 | 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 "ff.h"
  21. #include "symtab.h"
  22. #include "node.h"
  23. #include "funcs.h"
  24. #include "y.tab.h"
  25.  
  26. extern value_type var[];
  27. extern int nplanes,varused[],srcradused;
  28. extern struct node *tree[];
  29. int needinput;
  30.  
  31. /* get prepared to evaluate expression trees--
  32.    this assumes that tree[] array is already set up
  33.    return TRUE if we're ready to go
  34. */
  35.  
  36. Boolean setup(FilterRecordPtr pb){
  37.         int i;
  38.  
  39.         INITRANDSEED();
  40.         var['X'] = pb->imageSize.h;
  41.         var['Y'] = pb->imageSize.v;
  42.         var['Z'] = nplanes;
  43.         var['D'] = 1024;
  44.         var['M'] = ff_c2m(var['X'],var['Y'])/2;
  45.  
  46.         /* initialise flags for tracking special variable usage */
  47.         for( i=0 ; i<0x100 ; i++ )
  48.                 varused[i] = 0;
  49.         srcradused = 0;
  50.         for(i=0;i<nplanes;++i){
  51. //char s[100];sprintf(s,"expr[%d]=%#x",i,expr[i]);dbg(s);
  52.                 if( tree[i] || ( tree[i] = parseexpr(expr[i]) ) )
  53.                         checkvars(tree[i],varused,&srcradused);
  54.                 else
  55.                         break;
  56.         }
  57.         needinput = (srcradused || varused['r'] || varused['g'] || varused['b'] || varused['a']
  58.                                                         || varused['i'] || varused['u'] || varused['v'] || varused['c']) ;
  59.  
  60.         return i==nplanes; /* all required expressions parse OK */
  61. }
  62.  
  63. void evalpixel(unsigned char *outp,unsigned char *inp){
  64.         int f,k;
  65.  
  66.         if(needinput){
  67.                 var['r'] = inp[0];
  68.                 var['g'] = nplanes > 1 ? inp[1] : 0;
  69.                 var['b'] = nplanes > 2 ? inp[2] : 0;
  70.                 var['a'] = nplanes > 3 ? inp[3] : 0;
  71.                
  72.                 if(varused['i']) var['i'] = (( 76L*var['r'])+(150L*var['g'])+( 29L*var['b']))/256;
  73.                 if(varused['u']) var['u'] = ((-19L*var['r'])+(-37L*var['g'])+( 56L*var['b']))/256;
  74.                 if(varused['v']) var['v'] = (( 78L*var['r'])+(-65L*var['g'])+(-13L*var['b']))/256;
  75.         }
  76.         if(varused['d']) var['d'] = ff_c2d(var['X']/2-var['x'],var['Y']/2-var['y']);
  77.         if(varused['m']) var['m'] = ff_c2m(var['X']/2-var['x'],var['Y']/2-var['y']);
  78.        
  79.         for( k=0 ; k<nplanes ; ++k ){
  80.                 if(needinput)
  81.                         var['c'] = inp[k];
  82.                 var['z'] = k;
  83.                 f = eval(tree[k]);
  84.                 outp[k] = f<0 ? 0 : ( f>255 ? 255 : f ); // clamp channel value to 0-255
  85.         }
  86. }
  87.  
  88. OSErr process(FilterRecordPtr pb,Boolean progress,
  89.                           Rect *inRect,Rect *filterRect,Rect *outRect,
  90.                           void *outData,long outRowBytes){
  91.         unsigned char *inrow,*outrow,*inp,*outp;
  92.         int j,i,ncols = filterRect->right - filterRect->left;
  93.         long t,ticks = TICKCOUNT();
  94.  
  95.         long inoffset = (long)pb->inRowBytes*(filterRect->top - inRect->top)
  96.                                         + (long)nplanes*(filterRect->left - inRect->left),
  97.                  outoffset = (long)outRowBytes*(filterRect->top - outRect->top)
  98.                                          + (long)nplanes*(filterRect->left - outRect->left);
  99. /*
  100. {char s[0x100];sprintf(s,"process: inoffset=%d outoffset=%d inData=%#x inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
  101.         inoffset,outoffset,pb->inData,
  102.         inRect->left,inRect->top,inRect->right,inRect->bottom,
  103.         filterRect->left,filterRect->top,filterRect->right,filterRect->bottom);dbg(s);}
  104. */
  105.  
  106.         for( inrow = (unsigned char*)pb->inData + inoffset, outrow = (unsigned char*)outData + outoffset,
  107.                         j = filterRect->bottom - filterRect->top, var['y'] = filterRect->top
  108.                         ; j-- ; ++var['y'], inrow += pb->inRowBytes, outrow += outRowBytes ){
  109.  
  110.                 for ( inp=inrow, outp=outrow, i=ncols, var['x']=filterRect->left ; i-- ; ++var['x'], inp+=nplanes, outp+=nplanes ){
  111.                         evalpixel(outp,inp); /* var['x'] & var['y'] are implicit parameters */
  112.                 }
  113.  
  114.                 if(progress && (t = TICKCOUNT()) > ticks){
  115.                         ticks = t + TICKS_SEC/4;
  116.                         if(pb->abortProc())
  117.                                 return userCanceledErr;
  118.                         else
  119.                                 pb->progressProc(var['y'] - pb->filterRect.top,pb->filterRect.bottom - pb->filterRect.top);
  120.                 }
  121. #ifdef MAC_ENV
  122.                 else{
  123.                         /* to reduce annoying delays during typing of expressions,
  124.                            immediately abort preview calculation if a key or mouse has been pressed. */
  125.                         EventRecord event;
  126.                         if(EventAvail(mDownMask|keyDownMask|autoKeyMask,&event))
  127.                                 return userCanceledErr;
  128.                 }
  129. #endif
  130.         }
  131.         return noErr;
  132.  
  133. }
  134.  
  135. OSErr process_scaled(FilterRecordPtr pb,Boolean progress,
  136.                           Rect *inRect,Rect *filterRect,Rect *outRect,
  137.                           void *outData,long outRowBytes,double zoom){
  138.         unsigned char *inrow,*outrow,*outp;
  139.         int j,i;
  140.         long t,ticks = TICKCOUNT();
  141.         double x,y;
  142. /*
  143. {char s[0x100];sprintf(s,"process_scaled: pb->inData=%#x  outData=%#x\n\
  144. inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d) outRect=(%d,%d,%d,%d)\n\
  145. pb->filterRect=(%d,%d,%d,%d)\n",
  146.         pb->inData,outData,
  147.         inRect->left,inRect->top,inRect->right,inRect->bottom,
  148.         filterRect->left,filterRect->top,filterRect->right,filterRect->bottom,
  149.         outRect->left,outRect->top,outRect->right,outRect->bottom,
  150.         pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom); dbg(s);}
  151. */
  152.         if(needinput && !pb->inData){
  153.                 simplealert("Input error (process_scaled: pb->inData == NULL). This problem is being investigated. Cannot apply the filter; please re-launch Photoshop and try again.");
  154.                 return userCanceledErr;
  155.         }else
  156.                 for( j = outRect->top, outrow = (unsigned char*)outData, y = filterRect->top
  157.                                 ; j < outRect->bottom ; ++j, outrow += outRowBytes, y += zoom ){
  158.                         var['y'] = y;
  159.                         inrow = (unsigned char*)pb->inData
  160.                                 + (var['y'] - inRect->top)*pb->inRowBytes
  161.                                 - (long)nplanes*inRect->left;
  162.        
  163.                         for( outp=outrow, i=outRect->left, x=filterRect->left ; i<outRect->right ; ++i, outp+=nplanes, x+=zoom ){
  164.                                 var['x'] = x;
  165.                                 evalpixel(outp,inrow + (long)var['x']*nplanes); /* var['x'] & var['y'] are implicit parameters */
  166.                         }
  167.        
  168.                         if(progress && (t = TICKCOUNT()) > ticks){
  169.                                 ticks = t + TICKS_SEC/4;
  170.                                 if(pb->abortProc())
  171.                                         return userCanceledErr;
  172.                                 else
  173.                                         pb->progressProc((int)y - pb->filterRect.top,pb->filterRect.bottom - pb->filterRect.top);
  174.                         }
  175. #ifdef MAC_ENV
  176.                         else{
  177.                                 /* to stop delays during typing of expressions,
  178.                                    immediately abort preview calculation if a key or mouse has been pressed. */
  179.                                 EventRecord event;
  180.                                 if(EventAvail(mDownMask|keyDownMask|autoKeyMask,&event))
  181.                                         return userCanceledErr;
  182.                         }
  183. #endif
  184.                 }
  185.  
  186.         return noErr;
  187. }
  188.