Subversion Repositories filter_foundry

Rev

Rev 192 | Rev 198 | 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
192 daniel-mar 3
    Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
4
    Copyright (C) 2018-2019 Daniel Marschall, ViaThinkSoft
2 toby 5
 
6
    This program is free software; you can redistribute it and/or modify
103 toby 7
    it under the terms of the GNU General Public License as published by
2 toby 8
    the Free Software Foundation; either version 2 of the License, or
9
    (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
103 toby 16
    You should have received a copy of the GNU General Public License
2 toby 17
    along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
*/
20
 
21
//#include <stdio.h>
22
//#include <sound.h>
23
 
24
#include "ff.h"
8 toby 25
 
2 toby 26
#include "node.h"
172 dmarschall 27
#include "funcs.h"
8 toby 28
#include "y.tab.h"
2 toby 29
#include "scripting.h"
185 dmarschall 30
#include <math.h>
2 toby 31
 
32
struct node *tree[4];
33
char *err[4];
94 toby 34
int errpos[4],errstart[4],nplanes,cnvused,chunksize,toprow;
158 dmarschall 35
value_type slider[8],cell[NUM_CELLS],map[4][0x100];
89 toby 36
char *expr[4];
106 dmarschall 37
// long maxSpace;
71 toby 38
globals_t *gdata;
39
FilterRecordPtr gpb;
2 toby 40
 
41
#ifdef MAC_ENV
62 toby 42
        #define hDllInstance NULL /* fake this Windows-only global */
2 toby 43
#endif
44
 
159 dmarschall 45
#ifdef WIN_ENV
46
#include "manifest.h"
47
#endif
48
 
2 toby 49
extern struct sym_rec predefs[];
71 toby 50
extern int nplanes,varused[];
2 toby 51
 
15 toby 52
int checkandinitparams(Handle params);
2 toby 53
 
102 toby 54
// MPW MrC requires prototype
103 toby 55
DLLEXPORT MACPASCAL
102 toby 56
void ENTRYPOINT(short selector,FilterRecordPtr pb,intptr_t *data,short *result);
57
 
103 toby 58
DLLEXPORT MACPASCAL
102 toby 59
void ENTRYPOINT(short selector, FilterRecordPtr pb, intptr_t *data, short *result){
87 toby 60
        static Boolean wantdialog = false;
2 toby 61
        OSErr e = noErr;
87 toby 62
        char *reason;
102 toby 63
 
158 dmarschall 64
#ifdef WIN_ENV
65
        // For Windows, we use an activation context to enforce that our Manifest resource will
66
        // be used. This allows us to use Visual Styles, even if the host application does not
67
        // support it.
159 dmarschall 68
        ManifestActivationCtx manifestVars;
69
        BOOL activationContextUsed;
158 dmarschall 70
 
194 daniel-mar 71
        activationContextUsed = ActivateManifest((HMODULE)hDllInstance, 1, &manifestVars);
158 dmarschall 72
#endif
73
 
105 toby 74
        if(selector != filterSelectorAbout && !*data){
102 toby 75
                BufferID tempId;
194 daniel-mar 76
                if( (*result = PS_BUFFER_ALLOC(sizeof(globals_t), &tempId)) ) {
77
#ifdef WIN_ENV
78
                        if (activationContextUsed) DeactivateManifest(&manifestVars);
79
#endif
102 toby 80
                        return;
194 daniel-mar 81
                }
103 toby 82
                *data = (intptr_t)PS_BUFFER_LOCK(tempId, true);
83
                gdata = (globals_t*)*data;
2 toby 84
                gdata->standalone = gdata->parmloaded = false;
85
        }else
86
                gdata = (globals_t*)*data;
102 toby 87
 
88
        EnterCodeResource();
89
 
90
        gpb = pb;
91
 
2 toby 92
        nplanes = MIN(pb->planes,4);
93
 
94
        switch (selector){
87 toby 95
        case filterSelectorAbout:
102 toby 96
                if(gdata && !gdata->parmloaded)
194 daniel-mar 97
                        gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason,1);
103 toby 98
                DoAbout((AboutRecordPtr)pb);
2 toby 99
                break;
100
        case filterSelectorParameters:
15 toby 101
                wantdialog = true;
2 toby 102
                break;
103
        case filterSelectorPrepare:
104
                DoPrepare(pb);
105
                init_symtab(predefs); // ready for parser calls
135 dmarschall 106
                init_trigtab();
2 toby 107
                break;
89 toby 108
        case filterSelectorStart:
109
                /* initialise the parameter handle that Photoshop keeps for us */
110
                if(!pb->parameters)
167 dmarschall 111
                        pb->parameters = PINEWHANDLE(1); // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that.
2 toby 112
 
15 toby 113
                wantdialog |= checkandinitparams(pb->parameters);
114
 
115
                /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */
2 toby 116
                if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){
117
                        if( maindialog(pb) ){
183 dmarschall 118
                                if (!host_preserves_parameters()) {
119
                                        /* Workaround for GIMP/PSPI, to avoid that formulas vanish when you re-open the main window.
120
                                           The reason is a bug in PSPI: The host should preserve the value of pb->parameters, which PSPI does not do.
121
                                           Also, all global variables are unloaded, so the plugin cannot preserve any data.
194 daniel-mar 122
                                           Workaround in FF 1.7: If the host GIMP is detected, then a special mode will be activated.
183 dmarschall 123
                                           This mode saves the filter data into a temporary file "FilterFoundry.afs" and loads it
124
                                           when the window is opened again. */
125
                                        // Workaround: Save settings in "FilterFoundry.afs" if the host does not preserve pb->parameters
182 dmarschall 126
                                        char outfilename[255];
127
                                        char* tempdir;
167 dmarschall 128
                                        StandardFileReply sfr;
129
                                        sfr.sfGood = true;
130
                                        sfr.sfReplacing = true;
131
                                        sfr.sfType = PS_FILTER_FILETYPE;
182 dmarschall 132
 
133
                                        tempdir = getenv("TMP");
134
                                        #ifdef WIN_ENV
135
                                        if (strlen(tempdir) > 0) strcat(tempdir, "\\");
136
                                        #else
137
                                        if (strlen(tempdir) > 0) strcat(tempdir, "/");
138
                                        #endif
183 dmarschall 139
                                        sprintf(outfilename, "%sFilterFoundry.afs", tempdir);
182 dmarschall 140
 
141
                                        myc2pstrcpy(sfr.sfFile.name, outfilename);
188 dmarschall 142
                                        #ifdef WIN_ENV
185 dmarschall 143
                                        sfr.nFileExtension = (WORD)(strlen(outfilename) - strlen(".afs"));
188 dmarschall 144
                                        #endif
167 dmarschall 145
                                        sfr.sfScript = 0; // FIXME: is that ok?
146
                                        savefile(&sfr);
147
                                }
148
 
15 toby 149
                                /* update stored parameters from new user settings */
2 toby 150
                                saveparams(pb->parameters);
151
                        }else
152
                                e = userCanceledErr;
153
                }
18 toby 154
                wantdialog = false;
2 toby 155
 
156
                if(!e){
157
                        if(setup(pb)){
158
                                DoStart(pb);
159
                        }else{
160
                                SYSBEEP(1);
161
                                e = filterBadParameters;
162
                        }
163
                }
164
                break;
103 toby 165
        case filterSelectorContinue:
166
                e = DoContinue(pb);
2 toby 167
                break;
168
        case filterSelectorFinish:
169
                DoFinish(pb);
170
                break;
171
        default:
172
                e = filterBadParameters;
173
        }
103 toby 174
 
2 toby 175
        *result = e;
176
 
177
        ExitCodeResource();
158 dmarschall 178
 
179
#ifdef WIN_ENV
194 daniel-mar 180
        if (activationContextUsed) DeactivateManifest(&manifestVars);
158 dmarschall 181
#endif
2 toby 182
}
183
 
15 toby 184
int checkandinitparams(Handle params){
185
        char *reasonstr,*reason;
21 toby 186
        int i,f,showdialog;
103 toby 187
 
183 dmarschall 188
        if (!host_preserves_parameters()) {
189
                // Workaround: Load settings in "FilterFoundry.afs" if host does not preserve pb->parameters
182 dmarschall 190
                char outfilename[255];
191
                char* tempdir;
167 dmarschall 192
                StandardFileReply sfr;
193
                sfr.sfGood = true;
194
                sfr.sfReplacing = true;
195
                sfr.sfType = PS_FILTER_FILETYPE;
182 dmarschall 196
 
197
                tempdir = getenv("TMP");
198
                #ifdef WIN_ENV
199
                if (strlen(tempdir) > 0) strcat(tempdir, "\\");
200
                #else
201
                if (strlen(tempdir) > 0) strcat(tempdir, "/");
202
                #endif
183 dmarschall 203
                sprintf(outfilename, "%sFilterFoundry.afs", tempdir);
182 dmarschall 204
 
205
                myc2pstrcpy(sfr.sfFile.name, outfilename);
188 dmarschall 206
                #ifdef WIN_ENV
185 dmarschall 207
                sfr.nFileExtension = (WORD)(strlen(outfilename) - strlen(".afs"));
188 dmarschall 208
                #endif
167 dmarschall 209
                sfr.sfScript = 0; // FIXME: is that ok?
210
                if (loadfile(&sfr, &reason)) return true;
211
        }
212
 
23 toby 213
        if( (f = !(params && readparams(params,false,&reasonstr))) ){
2 toby 214
                /* either the parameter handle was uninitialised,
215
                   or the parameter data couldn't be read; set default values */
216
 
89 toby 217
                // see if saved parameters exist
194 daniel-mar 218
                gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason,1);
89 toby 219
 
87 toby 220
                if(!gdata->standalone){
21 toby 221
                        // no saved settings (not standalone)
85 toby 222
                        for(i = 0; i < 8; ++i)
2 toby 223
                                slider[i] = i*10+100;
146 dmarschall 224
                        for(i = 0; i < 4; ++i)
225
                                if(expr[i])
226
                                        free(expr[i]);
89 toby 227
                        if(gpb->imageMode == plugInModeRGBColor){
228
                                expr[0] = my_strdup("r");
229
                                expr[1] = my_strdup("g");
230
                                expr[2] = my_strdup("b");
231
                                expr[3] = my_strdup("a");
232
                        }else{
233
                                expr[0] = my_strdup("c");
234
                                expr[1] = my_strdup("c");
235
                                expr[2] = my_strdup("c");
236
                                expr[3] = my_strdup("c");
237
                        }
2 toby 238
                }
239
        }
103 toby 240
 
89 toby 241
        // let scripting system change parameters, if we're scripted;
103 toby 242
        // user may want to force display of dialog during scripting playback
21 toby 243
        showdialog = ReadScriptParamsOnRead();
2 toby 244
 
89 toby 245
        saveparams(params);
21 toby 246
        return f || showdialog;
2 toby 247
}
248
 
185 dmarschall 249
Boolean host_preserves_parameters() {
183 dmarschall 250
        if (gpb->hostSig == HOSTSIG_GIMP) return false;
251
        if (gpb->hostSig == HOSTSIG_IRFANVIEW) return false;
252
 
253
        /*
254
        char x[100];
255
        sprintf(x, "Host Signature: %u", gpb->hostSig);
256
        simplealert(x);
257
        */
258
 
259
        // We just assume the other hosts preserve the parameters
260
        return true;
261
}
262
 
182 dmarschall 263
int64_t maxspace(){
181 dmarschall 264
        if (gpb->maxSpace64 != 0) {
265
                // If this is non-zero, the host either support 64-bit OR the host ignored the rule "set reserved fields to 0".
266
                uint64_t maxSpace64 = gpb->maxSpace64;
183 dmarschall 267
 
268
                /*
269
                char x[100];
270
                sprintf(x, "maxSpace64: %lld", maxSpace64);
271
                simplealert(x);
272
                */
273
 
274
                // Note: IrfanView currently does not support maxSpace64, so we don't handle HOSTSIG_IRFANVIEW
181 dmarschall 275
                return maxSpace64;
276
        } else {
277
                // Note: If maxSpace gets converted from Int32 to unsigned int, we can reach up to 4 GB RAM. However, after this, there will be a wrap to 0 GB again.
278
                unsigned int maxSpace32 = (unsigned int) gpb->maxSpace;
279
                uint64_t maxSpace64 = maxSpace32;
183 dmarschall 280
 
281
                /*
282
                char x[100];
283
                sprintf(x, "maxSpace32: %lld", maxSpace64);
284
                simplealert(x);
285
                */
286
 
287
                if (gpb->hostSig == HOSTSIG_IRFANVIEW) maxSpace64 *= 1024; // IrfanView is giving Kilobytes instead of Bytes
190 dmarschall 288
                // TODO: Serif PhotoPlus also gives Kilobytes instead of bytes. But since it uses not a unique host signature, there is nothing we can do???
181 dmarschall 289
                return maxSpace64;
290
        }
291
}
292
 
185 dmarschall 293
Boolean maxspace_available() {
182 dmarschall 294
        // GIMP sets MaxSpace to hardcoded 100 MB
183 dmarschall 295
        if (gpb->hostSig == HOSTSIG_GIMP) return false;
182 dmarschall 296
 
297
        return true;
298
}
299
 
2 toby 300
void DoPrepare(FilterRecordPtr pb){
301
        int i;
302
 
85 toby 303
        for(i = 4; i--;){
8 toby 304
                if(expr[i]||tree[i]) DBG("expr[] or tree[] non-NULL in Prepare!");
2 toby 305
                expr[i] = NULL;
306
                tree[i] = NULL;
307
        }
106 dmarschall 308
 
309
        // Commented out by DM, 18 Dec 2018:
310
        // This code did not work on systems with 8 GB RAM:
311
        /*
312
        long space = (pb->maxSpace*9)/10; // don't ask for more than 90% of available memory
313
 
103 toby 314
        maxSpace = 512L<<10; // this is a wild guess, actually
315
        if(maxSpace > space)
316
                maxSpace = space;
317
        pb->maxSpace = maxSpace;
106 dmarschall 318
        */
319
 
320
        // New variant:
182 dmarschall 321
        if (maxspace_available()) {
185 dmarschall 322
                pb->maxSpace = (int32)ceil((maxspace()/10.)*9); // don't ask for more than 90% of available memory
323
                // FIXME: Also maxSpace64
182 dmarschall 324
        }
2 toby 325
}
326
 
327
void RequestNext(FilterRecordPtr pb,long toprow){
328
        /* Request next block of the image */
329
 
330
        pb->inLoPlane = pb->outLoPlane = 0;
331
        pb->inHiPlane = pb->outHiPlane = nplanes-1;
332
 
66 toby 333
        // if any of the formulae involve random access to image pixels,
89 toby 334
        // ask for the entire image
94 toby 335
        if(needall){
2 toby 336
                SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
337
        }else{
164 dmarschall 338
                // TODO: This does not work with GIMP. So, if we are using GIMP, we should
339
                //       somehow always use "needall=true", and/or find out why this doesn't work
340
                //       with GIMP.
341
 
66 toby 342
                // otherwise, process the filtered area, by chunksize parts
2 toby 343
                pb->inRect.left = pb->filterRect.left;
344
                pb->inRect.right = pb->filterRect.right;
185 dmarschall 345
                pb->inRect.top = (int16)toprow;
346
                pb->inRect.bottom = (int16)MIN(toprow + chunksize,pb->filterRect.bottom);
103 toby 347
 
71 toby 348
                if(cnvused){
349
                        // cnv() needs one extra pixel in each direction
350
                        if(pb->inRect.left > 0)
351
                                --pb->inRect.left;
352
                        if(pb->inRect.right < pb->imageSize.h)
353
                                ++pb->inRect.right;
354
                        if(pb->inRect.top > 0)
355
                                --pb->inRect.top;
356
                        if(pb->inRect.bottom < pb->imageSize.v)
357
                                ++pb->inRect.bottom;
358
                }
2 toby 359
        }
66 toby 360
        pb->outRect = pb->filterRect;
2 toby 361
/*
94 toby 362
{char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
103 toby 363
        needall,
2 toby 364
        pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom,
365
        pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom);dbg(s);}
366
*/
367
}
368
 
369
void DoStart(FilterRecordPtr pb){
370
//dbg("DoStart");
371
        /* if src() or rad() functions are used, random access to the image data is required,
372
           so we must request the entire image in a single chunk. */
94 toby 373
        chunksize = needall ? (pb->filterRect.bottom - pb->filterRect.top) : CHUNK_ROWS;
2 toby 374
        toprow = pb->filterRect.top;
375
        RequestNext(pb,toprow);
376
}
377
 
378
OSErr DoContinue(FilterRecordPtr pb){
379
        OSErr e = noErr;
71 toby 380
        Rect fr;
381
        long outoffset;
2 toby 382
 
94 toby 383
        if(needall)
71 toby 384
                fr = pb->filterRect;  // filter whole selection at once
385
        else if(cnvused){
386
                // we've requested one pixel extra all around
387
                // (see RequestNext()), just for access purposes. But filter
388
                // original selection only.
389
                fr.left = pb->filterRect.left;
390
                fr.right = pb->filterRect.right;
391
                fr.top = toprow;
392
                fr.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
393
        }else  // filter whatever portion we've been given
394
                fr = pb->inRect;
395
 
103 toby 396
        outoffset = (long)pb->outRowBytes*(fr.top - pb->outRect.top)
71 toby 397
                                + (long)nplanes*(fr.left - pb->outRect.left);
398
 
399
        if(!(e = process_scaled(pb, true, &fr, &fr,
62 toby 400
                                (Ptr)pb->outData+outoffset, pb->outRowBytes, 1.)))
401
        {
2 toby 402
                toprow += chunksize;
403
                if(toprow < pb->filterRect.bottom)
404
                        RequestNext(pb,toprow);
405
                else{
406
                        SETRECT(pb->inRect,0,0,0,0);
407
                        pb->outRect = pb->maskRect = pb->inRect;
408
                }
409
        }
410
        return e;
411
}
412
 
413
void DoFinish(FilterRecordPtr pb){
414
        int i;
89 toby 415
 
2 toby 416
        WriteScriptParamsOnRead();
417
 
85 toby 418
        for(i = 4; i--;){
2 toby 419
                freetree(tree[i]);
420
                if(expr[i]) free(expr[i]);
421
        }
422
}