Rev 8 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | toby | 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 | #include "node.h" |
||
25 | #include "parser.h" |
||
26 | #include "scripting.h" |
||
27 | |||
28 | struct node *tree[4]; |
||
29 | char *err[4]; |
||
30 | int errpos[4],errstart[4],nplanes,srcradused,chunksize,toprow; |
||
31 | value_type slider[8],cell[0x100],map[4][0x100]; |
||
32 | char *expr[4],*defaultexpr[]={"r","g","b","a"}; |
||
33 | |||
34 | #ifdef MAC_ENV |
||
35 | /* fake this Windows-only global */ |
||
36 | Handle hDllInstance = NULL; |
||
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 | void checkandinitparams(long *data,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; // was: 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 | if(!pb->parameters) |
||
80 | pb->parameters = PINEWHANDLE(0); |
||
81 | |||
82 | checkandinitparams(data,pb->parameters); |
||
83 | |||
84 | if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){ |
||
85 | if( maindialog(pb) ){ |
||
86 | /* update parameters */ |
||
87 | saveparams(pb->parameters); |
||
88 | }else |
||
89 | e = userCanceledErr; |
||
90 | wantdialog = false; |
||
91 | } |
||
92 | |||
93 | if(!e){ |
||
94 | if(setup(pb)){ |
||
95 | DoStart(pb); |
||
96 | }else{ |
||
97 | SYSBEEP(1); |
||
98 | e = filterBadParameters; |
||
99 | } |
||
100 | } |
||
101 | break; |
||
102 | case filterSelectorContinue: |
||
103 | e = DoContinue(pb); |
||
104 | break; |
||
105 | case filterSelectorFinish: |
||
106 | DoFinish(pb); |
||
107 | break; |
||
108 | default: |
||
109 | e = filterBadParameters; |
||
110 | } |
||
111 | |||
112 | *result = e; |
||
113 | |||
114 | ExitCodeResource(); |
||
115 | } |
||
116 | |||
117 | void checkandinitparams(long *data,Handle params){ |
||
118 | char *reasonstr; |
||
119 | int i; |
||
120 | char *reason; |
||
121 | |||
122 | if(!(params && readparams(params,false,&reasonstr))){ |
||
123 | /* either the parameter handle was uninitialised, |
||
124 | or the parameter data couldn't be read; set default values */ |
||
125 | |||
126 | if(readPARMresource(hDllInstance,&reason)) |
||
127 | gdata->standalone = true; |
||
128 | else{ |
||
129 | /* no scripted parameters - dialog wanted */ |
||
130 | for(i=0;i<8;++i) |
||
131 | slider[i] = i*10+100; |
||
132 | for(i=4;i--;) |
||
133 | expr[i] = my_strdup(defaultexpr[i]); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | if( ! ReadScriptParamsOnRead() ) |
||
138 | saveparams(gpb->parameters); /* save the scripted params */ |
||
139 | |||
140 | /* check for NULL expression pointers (?) */ |
||
141 | for(i=4;i--;) |
||
142 | if(!expr[i]) expr[i] = my_strdup(defaultexpr[i]); |
||
143 | } |
||
144 | |||
145 | void DoPrepare(FilterRecordPtr pb){ |
||
146 | int i; |
||
147 | |||
148 | for(i=4;i--;){ |
||
149 | if(expr[i]||tree[i]) DBG("expr[] or tree[] nonzero in Prepare!"); |
||
150 | expr[i] = NULL; |
||
151 | tree[i] = NULL; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | void RequestNext(FilterRecordPtr pb,long toprow){ |
||
156 | /* Request next block of the image */ |
||
157 | |||
158 | pb->inLoPlane = pb->outLoPlane = 0; |
||
159 | pb->inHiPlane = pb->outHiPlane = nplanes-1; |
||
160 | |||
161 | if(srcradused){ |
||
162 | SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v); |
||
163 | }else{ |
||
164 | pb->inRect.left = pb->filterRect.left; |
||
165 | pb->inRect.right = pb->filterRect.right; |
||
166 | pb->inRect.top = toprow; |
||
167 | pb->inRect.bottom = MIN(toprow + chunksize,pb->filterRect.bottom); |
||
168 | } |
||
169 | pb->outRect = pb->inRect; |
||
170 | /* |
||
171 | {char s[0x100];sprintf(s,"RequestNext srcradused=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)", |
||
172 | srcradused, |
||
173 | pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom, |
||
174 | pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom);dbg(s);} |
||
175 | */ |
||
176 | } |
||
177 | |||
178 | void DoStart(FilterRecordPtr pb){ |
||
179 | //dbg("DoStart"); |
||
180 | /* if src() or rad() functions are used, random access to the image data is required, |
||
181 | so we must request the entire image in a single chunk. */ |
||
182 | chunksize = srcradused ? (pb->filterRect.bottom - pb->filterRect.top) : CHUNK_ROWS; |
||
183 | toprow = pb->filterRect.top; |
||
184 | RequestNext(pb,toprow); |
||
185 | } |
||
186 | |||
187 | OSErr DoContinue(FilterRecordPtr pb){ |
||
188 | OSErr e = noErr; |
||
189 | Rect *fr = srcradused ? &pb->filterRect : &pb->inRect; |
||
190 | long outoffset = (long)pb->outRowBytes*(fr->top - pb->outRect.top) |
||
191 | + (long)nplanes*(fr->left - pb->outRect.left); |
||
192 | |||
193 | // {char s[0x100];sprintf(s,"DoContinue: outoffset=%d\n",outoffset);dbg(s);} |
||
194 | |||
195 | if(!(e = process_scaled(pb,true, |
||
196 | &pb->inRect, |
||
197 | fr, |
||
198 | fr,//&pb->outRect, |
||
199 | (Ptr)pb->outData+outoffset,pb->outRowBytes, 1.))){ |
||
200 | toprow += chunksize; |
||
201 | if(toprow < pb->filterRect.bottom) |
||
202 | RequestNext(pb,toprow); |
||
203 | else{ |
||
204 | SETRECT(pb->inRect,0,0,0,0); |
||
205 | pb->outRect = pb->maskRect = pb->inRect; |
||
206 | } |
||
207 | } |
||
208 | return e; |
||
209 | } |
||
210 | |||
211 | void DoFinish(FilterRecordPtr pb){ |
||
212 | int i; |
||
213 | |||
214 | WriteScriptParamsOnRead(); |
||
215 | |||
216 | for(i=4;i--;){ |
||
217 | freetree(tree[i]); |
||
218 | if(expr[i]) free(expr[i]); |
||
219 | } |
||
220 | } |