Subversion Repositories filter_foundry

Rev

Rev 8 | Rev 18 | Go to most recent revision | Details | Compare with Previous | 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"
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];
31
int errpos[4],errstart[4],nplanes,srcradused,chunksize,toprow;
32
value_type slider[8],cell[0x100],map[4][0x100];
33
char *expr[4],*defaultexpr[]={"r","g","b","a"};
34
 
35
#ifdef MAC_ENV
15 toby 36
#define hDllInstance NULL /* fake this Windows-only global */
2 toby 37
#endif
38
 
39
extern struct sym_rec predefs[];
40
extern int nplanes,varused[],srcradused;
41
 
42
globals_t *gdata;
43
FilterRecordPtr gpb;
44
 
15 toby 45
int checkandinitparams(Handle params);
2 toby 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");
15 toby 70
                wantdialog = true;
2 toby 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
 
15 toby 80
                if(!pb->parameters){
81
                        //dbg("pb->parameters = PINEWHANDLE(0)"),
82
                        pb->parameters = PINEWHANDLE(0); /* initialise the parameter handle that Photoshop keeps for us */
83
                        if(!pb->parameters) dbg("filterSelectorStart NULL handle @ PINEWHANDLE(0)");
84
                }
2 toby 85
 
15 toby 86
                wantdialog |= checkandinitparams(pb->parameters);
87
 
88
                /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */
2 toby 89
                if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){
90
                        if( maindialog(pb) ){
15 toby 91
                                //dbg("maindialog OK!");
92
                                /* update stored parameters from new user settings */
2 toby 93
                                saveparams(pb->parameters);
94
                        }else
95
                                e = userCanceledErr;
96
                        wantdialog = false;
97
                }
98
 
99
                if(!e){
100
                        if(setup(pb)){
101
                                DoStart(pb);
102
                        }else{
103
                                SYSBEEP(1);
104
                                e = filterBadParameters;
105
                        }
106
                }
107
                break;
108
        case filterSelectorContinue:
109
                e = DoContinue(pb);
110
                break;
111
        case filterSelectorFinish:
112
                DoFinish(pb);
113
                break;
114
        default:
115
                e = filterBadParameters;
116
        }
117
 
118
        *result = e;
119
 
120
        ExitCodeResource();
121
}
122
 
15 toby 123
int checkandinitparams(Handle params){
124
        char *reasonstr,*reason;
125
        int i,f;
2 toby 126
 
15 toby 127
        if( !(params && readparams(params,false,&reasonstr)) ){
2 toby 128
                /* either the parameter handle was uninitialised,
129
                   or the parameter data couldn't be read; set default values */
130
 
131
                if(readPARMresource(hDllInstance,&reason))
132
                        gdata->standalone = true;
133
                else{
15 toby 134
                        //dbg("checkandinitparams: setting DEFAULTS!");
2 toby 135
                        /* no scripted parameters - dialog wanted */
136
                        for(i=0;i<8;++i)
137
                                slider[i] = i*10+100;
138
                        for(i=4;i--;)
139
                                expr[i] = my_strdup(defaultexpr[i]);
140
                }
141
        }
142
 
15 toby 143
        f = true;//ReadScriptParamsOnRead(); /* look for anything from the scripting engine */
2 toby 144
 
15 toby 145
        /* sanity check for NULL expression pointers (?) */
2 toby 146
        for(i=4;i--;)
147
                if(!expr[i]) expr[i] = my_strdup(defaultexpr[i]);
15 toby 148
 
149
        saveparams(params); /* keep what we got */
150
        return f;
2 toby 151
}
152
 
153
void DoPrepare(FilterRecordPtr pb){
154
        int i;
155
 
156
        for(i=4;i--;){
8 toby 157
                if(expr[i]||tree[i]) DBG("expr[] or tree[] non-NULL in Prepare!");
2 toby 158
                expr[i] = NULL;
159
                tree[i] = NULL;
160
        }
15 toby 161
 
162
//      pb->maxSpace = 256L<<10; /* nominal memory, neither here nor there */
2 toby 163
}
164
 
165
void RequestNext(FilterRecordPtr pb,long toprow){
166
        /* Request next block of the image */
167
 
168
        pb->inLoPlane = pb->outLoPlane = 0;
169
        pb->inHiPlane = pb->outHiPlane = nplanes-1;
170
 
171
        if(srcradused){
172
                SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
173
        }else{
174
                pb->inRect.left = pb->filterRect.left;
175
                pb->inRect.right = pb->filterRect.right;
176
                pb->inRect.top = toprow;
177
                pb->inRect.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
178
        }
179
        pb->outRect = pb->inRect;
180
/*
181
{char s[0x100];sprintf(s,"RequestNext srcradused=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
182
        srcradused,
183
        pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom,
184
        pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom);dbg(s);}
185
*/
186
}
187
 
188
void DoStart(FilterRecordPtr pb){
189
//dbg("DoStart");
190
        /* if src() or rad() functions are used, random access to the image data is required,
191
           so we must request the entire image in a single chunk. */
192
        chunksize = srcradused ? (pb->filterRect.bottom - pb->filterRect.top) : CHUNK_ROWS;
193
        toprow = pb->filterRect.top;
194
        RequestNext(pb,toprow);
195
}
196
 
197
OSErr DoContinue(FilterRecordPtr pb){
198
        OSErr e = noErr;
199
        Rect *fr = srcradused ? &pb->filterRect : &pb->inRect;
200
        long outoffset = (long)pb->outRowBytes*(fr->top - pb->outRect.top)
201
                                         + (long)nplanes*(fr->left - pb->outRect.left);
202
 
203
//      {char s[0x100];sprintf(s,"DoContinue: outoffset=%d\n",outoffset);dbg(s);}
204
 
205
        if(!(e = process_scaled(pb,true,
206
                                &pb->inRect,
207
                                fr,
208
                                fr,//&pb->outRect,
209
                                (Ptr)pb->outData+outoffset,pb->outRowBytes, 1.))){
210
                toprow += chunksize;
211
                if(toprow < pb->filterRect.bottom)
212
                        RequestNext(pb,toprow);
213
                else{
214
                        SETRECT(pb->inRect,0,0,0,0);
215
                        pb->outRect = pb->maskRect = pb->inRect;
216
                }
217
        }
218
        return e;
219
}
220
 
221
void DoFinish(FilterRecordPtr pb){
222
        int i;
223
 
224
        WriteScriptParamsOnRead();
225
 
226
        for(i=4;i--;){
227
                freetree(tree[i]);
228
                if(expr[i]) free(expr[i]);
229
        }
230
}