Subversion Repositories filter_foundry

Rev

Rev 103 | Rev 106 | 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
102 toby 3
    Copyright (C) 2003-9 Toby Thain, toby@telegraphics.com.au
2 toby 4
 
5
    This program is free software; you can redistribute it and/or modify
103 toby 6
    it under the terms of the GNU General Public License as published by
2 toby 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
 
103 toby 15
    You should have received a copy of the GNU General Public License
2 toby 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];
94 toby 31
int errpos[4],errstart[4],nplanes,cnvused,chunksize,toprow;
2 toby 32
value_type slider[8],cell[0x100],map[4][0x100];
89 toby 33
char *expr[4];
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
 
15 toby 45
int checkandinitparams(Handle params);
2 toby 46
 
102 toby 47
// MPW MrC requires prototype
103 toby 48
DLLEXPORT MACPASCAL
102 toby 49
void ENTRYPOINT(short selector,FilterRecordPtr pb,intptr_t *data,short *result);
50
 
103 toby 51
DLLEXPORT MACPASCAL
102 toby 52
void ENTRYPOINT(short selector, FilterRecordPtr pb, intptr_t *data, short *result){
87 toby 53
        static Boolean wantdialog = false;
2 toby 54
        OSErr e = noErr;
87 toby 55
        char *reason;
102 toby 56
 
105 toby 57
        if(selector != filterSelectorAbout && !*data){
102 toby 58
                BufferID tempId;
59
                if( (*result = PS_BUFFER_ALLOC(sizeof(globals_t), &tempId)) )
60
                        return;
103 toby 61
                *data = (intptr_t)PS_BUFFER_LOCK(tempId, true);
62
                gdata = (globals_t*)*data;
2 toby 63
                gdata->standalone = gdata->parmloaded = false;
64
        }else
65
                gdata = (globals_t*)*data;
102 toby 66
 
67
        EnterCodeResource();
68
 
69
        gpb = pb;
70
 
2 toby 71
        nplanes = MIN(pb->planes,4);
72
 
73
        switch (selector){
87 toby 74
        case filterSelectorAbout:
102 toby 75
                if(gdata && !gdata->parmloaded)
87 toby 76
                        gdata->standalone = gdata->parmloaded = readPARMresource(hDllInstance,&reason,1);
103 toby 77
                DoAbout((AboutRecordPtr)pb);
2 toby 78
                break;
79
        case filterSelectorParameters:
15 toby 80
                wantdialog = true;
2 toby 81
                break;
82
        case filterSelectorPrepare:
83
                DoPrepare(pb);
84
                init_symtab(predefs); // ready for parser calls
85
                break;
89 toby 86
        case filterSelectorStart:
87
                /* initialise the parameter handle that Photoshop keeps for us */
88
                if(!pb->parameters)
89
                        pb->parameters = PINEWHANDLE(0);
2 toby 90
 
15 toby 91
                wantdialog |= checkandinitparams(pb->parameters);
92
 
93
                /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */
2 toby 94
                if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){
95
                        if( maindialog(pb) ){
15 toby 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;
103 toby 112
        case filterSelectorContinue:
113
                e = DoContinue(pb);
2 toby 114
                break;
115
        case filterSelectorFinish:
116
                DoFinish(pb);
117
                break;
118
        default:
119
                e = filterBadParameters;
120
        }
103 toby 121
 
2 toby 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;
103 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
 
89 toby 135
                // see if saved parameters exist
87 toby 136
                gdata->standalone = gdata->parmloaded = readPARMresource(hDllInstance,&reason,1);
89 toby 137
 
87 toby 138
                if(!gdata->standalone){
21 toby 139
                        // no saved settings (not standalone)
85 toby 140
                        for(i = 0; i < 8; ++i)
2 toby 141
                                slider[i] = i*10+100;
89 toby 142
                        if(gpb->imageMode == plugInModeRGBColor){
143
                                expr[0] = my_strdup("r");
144
                                expr[1] = my_strdup("g");
145
                                expr[2] = my_strdup("b");
146
                                expr[3] = my_strdup("a");
147
                        }else{
148
                                expr[0] = my_strdup("c");
149
                                expr[1] = my_strdup("c");
150
                                expr[2] = my_strdup("c");
151
                                expr[3] = my_strdup("c");
152
                        }
2 toby 153
                }
154
        }
103 toby 155
 
89 toby 156
        // let scripting system change parameters, if we're scripted;
103 toby 157
        // user may want to force display of dialog during scripting playback
21 toby 158
        showdialog = ReadScriptParamsOnRead();
2 toby 159
 
89 toby 160
        saveparams(params);
21 toby 161
        return f || showdialog;
2 toby 162
}
163
 
164
void DoPrepare(FilterRecordPtr pb){
165
        int i;
103 toby 166
        long space = (pb->maxSpace*9)/10; // don't ask for more than 90% of available memory
2 toby 167
 
85 toby 168
        for(i = 4; i--;){
8 toby 169
                if(expr[i]||tree[i]) DBG("expr[] or tree[] non-NULL in Prepare!");
2 toby 170
                expr[i] = NULL;
171
                tree[i] = NULL;
172
        }
103 toby 173
        maxSpace = 512L<<10; // this is a wild guess, actually
174
        if(maxSpace > space)
175
                maxSpace = space;
176
        pb->maxSpace = maxSpace;
2 toby 177
}
178
 
179
void RequestNext(FilterRecordPtr pb,long toprow){
180
        /* Request next block of the image */
181
 
182
        pb->inLoPlane = pb->outLoPlane = 0;
183
        pb->inHiPlane = pb->outHiPlane = nplanes-1;
184
 
66 toby 185
        // if any of the formulae involve random access to image pixels,
89 toby 186
        // ask for the entire image
94 toby 187
        if(needall){
2 toby 188
                SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
189
        }else{
66 toby 190
                // otherwise, process the filtered area, by chunksize parts
2 toby 191
                pb->inRect.left = pb->filterRect.left;
192
                pb->inRect.right = pb->filterRect.right;
193
                pb->inRect.top = toprow;
194
                pb->inRect.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
103 toby 195
 
71 toby 196
                if(cnvused){
197
                        // cnv() needs one extra pixel in each direction
198
                        if(pb->inRect.left > 0)
199
                                --pb->inRect.left;
200
                        if(pb->inRect.right < pb->imageSize.h)
201
                                ++pb->inRect.right;
202
                        if(pb->inRect.top > 0)
203
                                --pb->inRect.top;
204
                        if(pb->inRect.bottom < pb->imageSize.v)
205
                                ++pb->inRect.bottom;
206
                }
2 toby 207
        }
66 toby 208
        pb->outRect = pb->filterRect;
2 toby 209
/*
94 toby 210
{char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
103 toby 211
        needall,
2 toby 212
        pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom,
213
        pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom);dbg(s);}
214
*/
215
}
216
 
217
void DoStart(FilterRecordPtr pb){
218
//dbg("DoStart");
219
        /* if src() or rad() functions are used, random access to the image data is required,
220
           so we must request the entire image in a single chunk. */
94 toby 221
        chunksize = needall ? (pb->filterRect.bottom - pb->filterRect.top) : CHUNK_ROWS;
2 toby 222
        toprow = pb->filterRect.top;
223
        RequestNext(pb,toprow);
224
}
225
 
226
OSErr DoContinue(FilterRecordPtr pb){
227
        OSErr e = noErr;
71 toby 228
        Rect fr;
229
        long outoffset;
2 toby 230
 
94 toby 231
        if(needall)
71 toby 232
                fr = pb->filterRect;  // filter whole selection at once
233
        else if(cnvused){
234
                // we've requested one pixel extra all around
235
                // (see RequestNext()), just for access purposes. But filter
236
                // original selection only.
237
                fr.left = pb->filterRect.left;
238
                fr.right = pb->filterRect.right;
239
                fr.top = toprow;
240
                fr.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
241
        }else  // filter whatever portion we've been given
242
                fr = pb->inRect;
243
 
103 toby 244
        outoffset = (long)pb->outRowBytes*(fr.top - pb->outRect.top)
71 toby 245
                                + (long)nplanes*(fr.left - pb->outRect.left);
246
 
247
        if(!(e = process_scaled(pb, true, &fr, &fr,
62 toby 248
                                (Ptr)pb->outData+outoffset, pb->outRowBytes, 1.)))
249
        {
2 toby 250
                toprow += chunksize;
251
                if(toprow < pb->filterRect.bottom)
252
                        RequestNext(pb,toprow);
253
                else{
254
                        SETRECT(pb->inRect,0,0,0,0);
255
                        pb->outRect = pb->maskRect = pb->inRect;
256
                }
257
        }
258
        return e;
259
}
260
 
261
void DoFinish(FilterRecordPtr pb){
262
        int i;
89 toby 263
 
2 toby 264
        WriteScriptParamsOnRead();
265
 
85 toby 266
        for(i = 4; i--;){
2 toby 267
                freetree(tree[i]);
268
                if(expr[i]) free(expr[i]);
269
        }
270
}