Subversion Repositories filter_foundry

Rev

Rev 85 | Rev 89 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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