Subversion Repositories filter_foundry

Rev

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