Subversion Repositories filter_foundry

Rev

Rev 106 | Rev 146 | 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];
106 dmarschall 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
135 dmarschall 85
                init_trigtab();
2 toby 86
                break;
89 toby 87
        case filterSelectorStart:
88
                /* initialise the parameter handle that Photoshop keeps for us */
89
                if(!pb->parameters)
90
                        pb->parameters = PINEWHANDLE(0);
2 toby 91
 
15 toby 92
                wantdialog |= checkandinitparams(pb->parameters);
93
 
94
                /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */
2 toby 95
                if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){
96
                        if( maindialog(pb) ){
15 toby 97
                                /* update stored parameters from new user settings */
2 toby 98
                                saveparams(pb->parameters);
99
                        }else
100
                                e = userCanceledErr;
101
                }
18 toby 102
                wantdialog = false;
2 toby 103
 
104
                if(!e){
105
                        if(setup(pb)){
106
                                DoStart(pb);
107
                        }else{
108
                                SYSBEEP(1);
109
                                e = filterBadParameters;
110
                        }
111
                }
112
                break;
103 toby 113
        case filterSelectorContinue:
114
                e = DoContinue(pb);
2 toby 115
                break;
116
        case filterSelectorFinish:
117
                DoFinish(pb);
118
                break;
119
        default:
120
                e = filterBadParameters;
121
        }
103 toby 122
 
2 toby 123
        *result = e;
124
 
125
        ExitCodeResource();
126
}
127
 
15 toby 128
int checkandinitparams(Handle params){
129
        char *reasonstr,*reason;
21 toby 130
        int i,f,showdialog;
103 toby 131
 
23 toby 132
        if( (f = !(params && readparams(params,false,&reasonstr))) ){
2 toby 133
                /* either the parameter handle was uninitialised,
134
                   or the parameter data couldn't be read; set default values */
135
 
89 toby 136
                // see if saved parameters exist
87 toby 137
                gdata->standalone = gdata->parmloaded = readPARMresource(hDllInstance,&reason,1);
89 toby 138
 
87 toby 139
                if(!gdata->standalone){
21 toby 140
                        // no saved settings (not standalone)
85 toby 141
                        for(i = 0; i < 8; ++i)
2 toby 142
                                slider[i] = i*10+100;
89 toby 143
                        if(gpb->imageMode == plugInModeRGBColor){
144
                                expr[0] = my_strdup("r");
145
                                expr[1] = my_strdup("g");
146
                                expr[2] = my_strdup("b");
147
                                expr[3] = my_strdup("a");
148
                        }else{
149
                                expr[0] = my_strdup("c");
150
                                expr[1] = my_strdup("c");
151
                                expr[2] = my_strdup("c");
152
                                expr[3] = my_strdup("c");
153
                        }
2 toby 154
                }
155
        }
103 toby 156
 
89 toby 157
        // let scripting system change parameters, if we're scripted;
103 toby 158
        // user may want to force display of dialog during scripting playback
21 toby 159
        showdialog = ReadScriptParamsOnRead();
2 toby 160
 
89 toby 161
        saveparams(params);
21 toby 162
        return f || showdialog;
2 toby 163
}
164
 
165
void DoPrepare(FilterRecordPtr pb){
166
        int i;
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
        }
106 dmarschall 173
 
174
        // Commented out by DM, 18 Dec 2018:
175
        // This code did not work on systems with 8 GB RAM:
176
        /*
177
        long space = (pb->maxSpace*9)/10; // don't ask for more than 90% of available memory
178
 
103 toby 179
        maxSpace = 512L<<10; // this is a wild guess, actually
180
        if(maxSpace > space)
181
                maxSpace = space;
182
        pb->maxSpace = maxSpace;
106 dmarschall 183
        */
184
 
185
        // New variant:
186
        // TODO: Programmatically test if host supports pb->maxSpace64, and if it does so, use this value instead.
187
        pb->maxSpace = ((double)pb->maxSpace/10)*9; // don't ask for more than 90% of available memory
2 toby 188
}
189
 
190
void RequestNext(FilterRecordPtr pb,long toprow){
191
        /* Request next block of the image */
192
 
193
        pb->inLoPlane = pb->outLoPlane = 0;
194
        pb->inHiPlane = pb->outHiPlane = nplanes-1;
195
 
66 toby 196
        // if any of the formulae involve random access to image pixels,
89 toby 197
        // ask for the entire image
94 toby 198
        if(needall){
2 toby 199
                SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
200
        }else{
66 toby 201
                // otherwise, process the filtered area, by chunksize parts
2 toby 202
                pb->inRect.left = pb->filterRect.left;
203
                pb->inRect.right = pb->filterRect.right;
204
                pb->inRect.top = toprow;
205
                pb->inRect.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
103 toby 206
 
71 toby 207
                if(cnvused){
208
                        // cnv() needs one extra pixel in each direction
209
                        if(pb->inRect.left > 0)
210
                                --pb->inRect.left;
211
                        if(pb->inRect.right < pb->imageSize.h)
212
                                ++pb->inRect.right;
213
                        if(pb->inRect.top > 0)
214
                                --pb->inRect.top;
215
                        if(pb->inRect.bottom < pb->imageSize.v)
216
                                ++pb->inRect.bottom;
217
                }
2 toby 218
        }
66 toby 219
        pb->outRect = pb->filterRect;
2 toby 220
/*
94 toby 221
{char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
103 toby 222
        needall,
2 toby 223
        pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom,
224
        pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom);dbg(s);}
225
*/
226
}
227
 
228
void DoStart(FilterRecordPtr pb){
229
//dbg("DoStart");
230
        /* if src() or rad() functions are used, random access to the image data is required,
231
           so we must request the entire image in a single chunk. */
94 toby 232
        chunksize = needall ? (pb->filterRect.bottom - pb->filterRect.top) : CHUNK_ROWS;
2 toby 233
        toprow = pb->filterRect.top;
234
        RequestNext(pb,toprow);
235
}
236
 
237
OSErr DoContinue(FilterRecordPtr pb){
238
        OSErr e = noErr;
71 toby 239
        Rect fr;
240
        long outoffset;
2 toby 241
 
94 toby 242
        if(needall)
71 toby 243
                fr = pb->filterRect;  // filter whole selection at once
244
        else if(cnvused){
245
                // we've requested one pixel extra all around
246
                // (see RequestNext()), just for access purposes. But filter
247
                // original selection only.
248
                fr.left = pb->filterRect.left;
249
                fr.right = pb->filterRect.right;
250
                fr.top = toprow;
251
                fr.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
252
        }else  // filter whatever portion we've been given
253
                fr = pb->inRect;
254
 
103 toby 255
        outoffset = (long)pb->outRowBytes*(fr.top - pb->outRect.top)
71 toby 256
                                + (long)nplanes*(fr.left - pb->outRect.left);
257
 
258
        if(!(e = process_scaled(pb, true, &fr, &fr,
62 toby 259
                                (Ptr)pb->outData+outoffset, pb->outRowBytes, 1.)))
260
        {
2 toby 261
                toprow += chunksize;
262
                if(toprow < pb->filterRect.bottom)
263
                        RequestNext(pb,toprow);
264
                else{
265
                        SETRECT(pb->inRect,0,0,0,0);
266
                        pb->outRect = pb->maskRect = pb->inRect;
267
                }
268
        }
269
        return e;
270
}
271
 
272
void DoFinish(FilterRecordPtr pb){
273
        int i;
89 toby 274
 
2 toby 275
        WriteScriptParamsOnRead();
276
 
85 toby 277
        for(i = 4; i--;){
2 toby 278
                freetree(tree[i]);
279
                if(expr[i]) free(expr[i]);
280
        }
281
}