Subversion Repositories filter_foundry

Rev

Rev 454 | Rev 482 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
256 daniel-mar 1
/*
2
    This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
3
    Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
4
    Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft
5
 
6
    This program is free software; you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
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
 
16
    You should have received a copy of the GNU General Public License
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 "ff.h"
22
 
23
#include "file_compat.h"
24
 
25
#ifdef MAC_ENV
26
#include <Endian.h>
27
#else
28
int EndianS32_LtoN(int num) {
272 daniel-mar 29
        return ((num>>24)&0xff) +      // move byte 3 to byte 0
30
               ((num<<8)&0xff0000) +   // move byte 1 to byte 2
31
               ((num>>8)&0xff00) +     // move byte 2 to byte 1
256 daniel-mar 32
               ((num<<24)&0xff000000); // byte 0 to byte 3
33
}
34
#endif
35
 
36
enum{
37
        BUFSIZE = 4L<<10,
38
        MAXLINE = 0x200,
39
};
40
 
408 daniel-mar 41
Boolean readparams_afs_pff(Handle h,char **reason){
256 daniel-mar 42
        Boolean res = false;
454 daniel-mar 43
        char linebuf[MAXLINE + 1] = { 0 };
44
        char curexpr[MAXEXPR + 1] = { 0 };
45
        char *p, *dataend, *q;
256 daniel-mar 46
        char c;
47
        int linecnt, lineptr, exprcnt;
48
 
49
        if(!h){
50
                *reason = _strdup("readparams: Null parameter handle.");
51
                return false;
52
        }
53
 
54
        p = PILOCKHANDLE(h,false);
55
        dataend = p + PIGETHANDLESIZE(h);
56
 
57
        q = curexpr;
58
        linecnt = exprcnt = lineptr = 0;
59
 
408 daniel-mar 60
        //*reason = _strdup("File was too short.");
256 daniel-mar 61
        while(p < dataend){
62
 
63
                c = *p++;
64
 
65
                if(c==CR || c==LF){ /* detected end of line */
66
 
421 daniel-mar 67
                        /* look ahead to see if we need to skip a line feed (DOS CRLF EOL convention) */
68
                        if(p < dataend && c == CR && *p == LF)
256 daniel-mar 69
                                ++p;
70
 
71
                        linebuf[lineptr] = 0; /* add terminating NUL to line buffer */
72
 
73
                        /* process complete line */
74
                        if(linecnt==0){
75
                                if(strcmp(linebuf,"%RGB-1.0")){
408 daniel-mar 76
                                        // *reason = _strdup("This doesn't look like a Filter Factory file (first line is not \"%RGB-1.0\").");
256 daniel-mar 77
                                        break;
78
                                }
79
                        }else if(linecnt<=8){
453 daniel-mar 80
                                int v;
81
                                v = atoi(linebuf);
82
                                if (v < 0) v = 0;
456 daniel-mar 83
                                else if (v > 255) v = 255;
84
                                slider[linecnt-1] = (uint8_t)v;
256 daniel-mar 85
                        }else{
86
                                if(lineptr){
87
                                        /* it's not an empty line; append it to current expr string */
88
                                        if( q+lineptr > curexpr+MAXEXPR ){
89
                                                *reason = _strdup("Found an expression longer than 1024 characters.");
90
                                                break;
91
                                        }
92
                                        q = cat(q,linebuf);
93
                                }else{
94
                                        /* it's an empty line: we've completed the expr string */
95
                                        if(expr[exprcnt])
96
                                                free(expr[exprcnt]);
97
                                        *q = 0;
98
                                        if(!(expr[exprcnt] = my_strdup(curexpr))){
99
                                                *reason = _strdup("Could not get memory for expression.");
100
                                                break;
101
                                        }
102
 
103
                                        if(++exprcnt == 4){
104
                                                res = true;
105
                                                break; /* got everything we want */
106
                                        }
107
 
108
                                        q = curexpr; /* empty current expr, ready for next one */
109
                                }
110
                        }
111
 
112
                        ++linecnt;
113
                        lineptr = 0;
114
                }else{
115
                        /* store character */
116
                        if(c=='\\'){ /* escape sequence */
117
                                if(p < dataend){
118
                                        c = *p++;
119
                                        switch(c){
120
                                        case 'r':
268 daniel-mar 121
                                                #if WIN_ENV
122
                                                c = CR;
123
                                                if (lineptr < MAXLINE)
274 daniel-mar 124
                                                        linebuf[lineptr++] = c;
268 daniel-mar 125
                                                c = LF;
126
                                                #else
127
                                                c = CR;
128
                                                #endif
129
                                                break;
256 daniel-mar 130
                                        case '\\': break;
408 daniel-mar 131
                                        //default:
444 daniel-mar 132
                                        //      if(alerts) alertuser((TCHAR*)TEXT("Warning:"),TEXT("Unknown escape sequence in input."));
256 daniel-mar 133
                                        }
444 daniel-mar 134
                                }//else if(alerts) alertuser((TCHAR*)TEXT("Warning:"),TEXT("truncated escape sequence ends input"));
256 daniel-mar 135
                        }
136
 
137
                        if(lineptr < MAXLINE)
138
                                linebuf[lineptr++] = c;
139
                }
140
        }
141
 
142
        PIUNLOCKHANDLE(h);
143
 
144
        return res;
145
}
146
 
147
void convert_premiere_to_photoshop(PARM_T* photoshop, PARM_T_PREMIERE* premiere) {
148
        int i;
149
 
150
        photoshop->cbSize = sizeof(PARM_T);
151
        photoshop->standalone = premiere->standalone;
152
        for (i=0;i<8;++i)
272 daniel-mar 153
                photoshop->val[i] = premiere->val[i];
256 daniel-mar 154
        photoshop->popDialog = premiere->popDialog;
155
        photoshop->unknown1 = premiere->unknown1;
156
        photoshop->unknown2 = premiere->unknown2;
157
        photoshop->unknown3 = premiere->unknown3;
158
        for (i=0;i<4;++i)
272 daniel-mar 159
                photoshop->map_used[i] = premiere->map_used[i];
256 daniel-mar 160
        for (i=0;i<8;++i)
272 daniel-mar 161
                photoshop->ctl_used[i] = premiere->ctl_used[i];
393 daniel-mar 162
        sprintf(photoshop->szCategory, "Filter Factory"); // Premiere plugins do not have a category attribute
256 daniel-mar 163
        photoshop->iProtected = 0; // Premiere plugins do not have a protect flag
393 daniel-mar 164
        memcpy((void*)photoshop->szTitle, (void*)premiere->szTitle, sizeof(photoshop->szTitle));
165
        memcpy((void*)photoshop->szCopyright, (void*)premiere->szCopyright, sizeof(photoshop->szCopyright));
166
        memcpy((void*)photoshop->szAuthor, (void*)premiere->szAuthor, sizeof(photoshop->szAuthor));
256 daniel-mar 167
        for (i=0;i<4;++i)
393 daniel-mar 168
                memcpy((void*)photoshop->szMap[i], (void*)premiere->szMap[i], sizeof(photoshop->szMap[i]));
256 daniel-mar 169
        for (i=0;i<8;++i)
393 daniel-mar 170
                memcpy((void*)photoshop->szCtl[i], (void*)premiere->szCtl[i], sizeof(photoshop->szCtl[i]));
256 daniel-mar 171
 
172
        if (premiere->singleExpression) {
393 daniel-mar 173
                memcpy((void*)photoshop->szFormula[0], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
174
                memcpy((void*)photoshop->szFormula[1], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
175
                memcpy((void*)photoshop->szFormula[2], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
176
                memcpy((void*)photoshop->szFormula[3], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
256 daniel-mar 177
        } else {
393 daniel-mar 178
                memcpy((void*)photoshop->szFormula[0], (void*)premiere->szFormula[2], sizeof(photoshop->szFormula[2]));
179
                memcpy((void*)photoshop->szFormula[1], (void*)premiere->szFormula[1], sizeof(photoshop->szFormula[1]));
180
                memcpy((void*)photoshop->szFormula[2], (void*)premiere->szFormula[0], sizeof(photoshop->szFormula[0]));
181
                memcpy((void*)photoshop->szFormula[3], (void*)premiere->szFormula[3], sizeof(photoshop->szFormula[3]));
256 daniel-mar 182
        }
183
}
184
 
366 daniel-mar 185
char* _ffx_read_str(char** q) {
186
        uint32_t len;
187
        char* val;
188
 
189
        len = *((uint32_t*)*q);
190
        *q += sizeof(uint32_t);
454 daniel-mar 191
        val = (char*)malloc((size_t)len + 1);
366 daniel-mar 192
        if (val != NULL) {
193
                memcpy(val, (char*)*q, len);
194
                val[len] = 0;
195
        }
196
        *q += len;
197
        return val;
198
}
199
 
200
Boolean readfile_ffx(StandardFileReply* sfr, char** reason) {
201
        Handle h;
202
        Boolean res = false;
203
        FILEREF refnum;
204
        uint32_t len;
205
        char* val;
367 daniel-mar 206
        int format_version = -1;
366 daniel-mar 207
        int i;
208
 
433 daniel-mar 209
        UNREFERENCED_PARAMETER(reason);
210
 
366 daniel-mar 211
        if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
212
                if ((h = readfileintohandle(refnum))) {
213
                        char* q = (char*)PILOCKHANDLE(h, false);
214
 
215
                        len = *((uint32_t*)q);
216
                        if (len == 6) {
217
                                val = _ffx_read_str(&q);
367 daniel-mar 218
                                if (strcmp(val, "FFX1.0") == 0) format_version = 10;
219
                                else if (strcmp(val, "FFX1.1") == 0) format_version = 11;
220
                                else if (strcmp(val, "FFX1.2") == 0) format_version = 12;
366 daniel-mar 221
                                free(val);
367 daniel-mar 222
                                if (format_version > 0) {
444 daniel-mar 223
                                        simplewarning((TCHAR*)TEXT("Attention! You are loading a \"Filters Unlimited\" file. Please note that Filter Foundry only implements the basic Filter Factory functions. Therefore, most \"Filters Unlimited\" filters won't work with Filter Foundry."));
368 daniel-mar 224
 
366 daniel-mar 225
                                        val = _ffx_read_str(&q);
393 daniel-mar 226
                                        strcpy(gdata->parm.szTitle, val);
366 daniel-mar 227
                                        free(val);
228
 
229
                                        val = _ffx_read_str(&q);
393 daniel-mar 230
                                        strcpy(gdata->parm.szCategory, val);
366 daniel-mar 231
                                        free(val);
232
 
233
                                        val = _ffx_read_str(&q);
393 daniel-mar 234
                                        strcpy(gdata->parm.szAuthor, val);
366 daniel-mar 235
                                        free(val);
236
 
237
                                        val = _ffx_read_str(&q);
393 daniel-mar 238
                                        strcpy(gdata->parm.szCopyright, val);
366 daniel-mar 239
                                        free(val);
240
 
241
                                        // Channels I, R, G, B, A
242
                                        for (i = 0; i < 4; i++) {
243
                                                val = _ffx_read_str(&q);
244
                                                if (i == 0) {
370 daniel-mar 245
                                                        char* val2 = _ffx_read_str(&q);
366 daniel-mar 246
                                                        if (strcmp(val, "0") != 0) {
247
                                                                // "Intro channel" existing
370 daniel-mar 248
                                                                // C++ wrong warning: Using uninitialized memory "val2" (C6001)
249
                                                                #pragma warning(suppress : 6001)
250
                                                                char* combined = (char*)malloc(strlen(val) + strlen(",") + strlen(val2) + 1);
251
                                                                if (combined != NULL) {
252
                                                                        sprintf(combined, "%s,%s", val, val2);
253
                                                                        free(val);
254
                                                                        free(val2);
255
                                                                        val = combined;
256
                                                                }
366 daniel-mar 257
                                                        }
258
                                                        else {
259
                                                                free(val);
370 daniel-mar 260
                                                                val = val2;
366 daniel-mar 261
                                                        }
262
                                                }
393 daniel-mar 263
                                                if (strlen(val) >= sizeof(gdata->parm.szFormula[i])) {
368 daniel-mar 264
                                                        if (i == 0) {
444 daniel-mar 265
                                                                simplealert((TCHAR*)TEXT("Attention! The formula for channel I/R was too long (longer than 1023 characters) and was truncated."));
368 daniel-mar 266
                                                        }
267
                                                        else if (i == 1) {
444 daniel-mar 268
                                                                simplealert((TCHAR*)TEXT("Attention! The formula for channel G was too long (longer than 1023 characters) and was truncated."));
368 daniel-mar 269
                                                        }
270
                                                        else if (i == 2) {
444 daniel-mar 271
                                                                simplealert((TCHAR*)TEXT("Attention! The formula for channel B was too long (longer than 1023 characters) and was truncated."));
368 daniel-mar 272
                                                        }
273
                                                        else if (i == 3) {
444 daniel-mar 274
                                                                simplealert((TCHAR*)TEXT("Attention! The formula for channel A was too long (longer than 1023 characters) and was truncated."));
368 daniel-mar 275
                                                        }
370 daniel-mar 276
                                                        // C++ wrong warning: Buffer overflow (C6386)
277
                                                        #pragma warning(suppress : 6386)
393 daniel-mar 278
                                                        val[sizeof(gdata->parm.szFormula[i]) - 1] = '\0';
368 daniel-mar 279
                                                }
395 daniel-mar 280
                                                if (expr[i]) free(expr[i]);
366 daniel-mar 281
                                                expr[i] = my_strdup(val);
393 daniel-mar 282
                                                strcpy(gdata->parm.szFormula[i], val);
366 daniel-mar 283
                                                free(val);
284
                                        }
285
 
286
                                        // Sliders
287
                                        for (i = 0; i < 8; i++) {
367 daniel-mar 288
                                                char* sliderName;
453 daniel-mar 289
                                                int v;
366 daniel-mar 290
                                                val = _ffx_read_str(&q);
367 daniel-mar 291
                                                sliderName = val;
292
                                                if (format_version >= 12) {
293
                                                        // Format FFX1.2 has prefixes {S} = Slider, {C} = Checkbox, none = Slider
294
                                                        if ((sliderName[0] == '{') && (sliderName[1] == 'S') && (sliderName[2] == '}')) sliderName += 3;
295
                                                        else if ((sliderName[0] == '{') && (sliderName[1] == 'C') && (sliderName[2] == '}')) sliderName += 3;
296
                                                }
393 daniel-mar 297
                                                strcpy(gdata->parm.szCtl[i], sliderName);
366 daniel-mar 298
                                                free(val);
367 daniel-mar 299
                                                gdata->parm.ctl_used[i] = (bool32_t)*((byte*)q);
366 daniel-mar 300
                                                q += sizeof(byte);
301
                                                gdata->parm.val[i] = *((uint32_t*)q);
453 daniel-mar 302
                                                v = *((uint32_t*)q);
303
                                                if (v < 0) v = 0;
456 daniel-mar 304
                                                else if (v > 255) v = 255;
305
                                                slider[i] = (uint8_t)v;
366 daniel-mar 306
                                                q += sizeof(uint32_t);
307
                                        }
308
 
390 daniel-mar 309
                                        // Maps (are not part of the format!)
393 daniel-mar 310
                                        strcpy(gdata->parm.szMap[0], "Map 0:");
311
                                        strcpy(gdata->parm.szMap[1], "Map 1:");
312
                                        strcpy(gdata->parm.szMap[2], "Map 2:");
313
                                        strcpy(gdata->parm.szMap[3], "Map 3:");
390 daniel-mar 314
 
366 daniel-mar 315
                                        res = true;
316
                                }
317
                        }
318
                        PIDISPOSEHANDLE(h);
319
                }
320
                FSClose(refnum);
321
        }
322
 
323
        if (res) gdata->obfusc = false;
324
        return res;
325
}
326
 
373 daniel-mar 327
Boolean readfile_8bf(StandardFileReply *sfr,char **reason){
256 daniel-mar 328
        unsigned char magic[2];
329
        FILECOUNT count;
330
        Handle h;
331
        Boolean res = false;
332
        FILEREF refnum;
333
 
334
        if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&refnum) == noErr){
335
                // check DOS EXE magic number
336
                count = 2;
337
                if(FSRead(refnum,&count,magic) == noErr /*&& magic[0]=='M' && magic[1]=='Z'*/){
309 daniel-mar 338
                        if(GetEOF(refnum,(FILEPOS*)&count) == noErr && count < 4096L<<10){ // sanity check file size < 4MiB (note that "Debug" builds can have approx 700 KiB while "Release" builds have approx 300 KiB)
256 daniel-mar 339
                                if( (h = readfileintohandle(refnum)) ){
340
                                        long *q = (long*)PILOCKHANDLE(h,false);
341
 
342
                                        // look for signature at start of valid PARM resource
343
                                        // This signature is observed in Filter Factory standalones.
344
                                        for( count /= 4 ; count >= PARM_SIZE/4 ; --count, ++q )
345
                                        {
394 daniel-mar 346
                                                res = readPARM(&gdata->parm, (Ptr)q);
256 daniel-mar 347
                                                if (res) break;
348
                                        }
341 daniel-mar 349
 
256 daniel-mar 350
                                        PIDISPOSEHANDLE(h);
351
                                }
352
                        }
353
                } // else no point in proceeding
354
                FSClose(refnum);
355
        }else
356
                *reason = _strdup("Could not open file.");
357
 
358
        if (res) gdata->obfusc = false;
359
        return res;
360
}
361
 
394 daniel-mar 362
Boolean readPARM(PARM_T* pparm, Ptr p){
363
        Boolean towin, tomac, fromwin, frommac;
364
        unsigned int signature = *((unsigned int*)p);
365
        unsigned int standalone = *((unsigned int*)p+1);
256 daniel-mar 366
 
394 daniel-mar 367
        // Find out our OS ("reader") the OS of the plugin ("source")
368
        #ifdef MAC_ENV
369
        towin = false;
370
        tomac = true;
371
        fromwin = ((EndianS32_LtoN(signature) == PARM_SIZE) ||
372
                (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) ||
373
                (EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1;
374
        frommac = ((signature == PARM_SIZE) ||
375
                (signature == PARM_SIZE_PREMIERE) ||
376
                (signature == PARM_SIG_MAC)) && standalone == 1;
377
        #else
378
        towin = true;
379
        tomac = false;
380
        fromwin = ((signature == PARM_SIZE) ||
381
                (signature == PARM_SIZE_PREMIERE) ||
382
                (signature == PARM_SIG_MAC)) && standalone == 1;
383
        frommac = ((EndianS32_LtoN(signature) == PARM_SIZE) ||
384
                (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) ||
385
                (EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1;
386
        #endif
387
 
388
        // Is it a valid signature?
389
        if (!fromwin && !frommac) {
390
                // No valid signature found
391
                return false;
392
        }
393
 
394
        // Does it come from Premiere or Photoshop?
395
        // Initialize pparm
396
        if ((signature == PARM_SIZE_PREMIERE) || (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE)) {
397
                // It comes from Premiere. Swap R and B channel and convert to a Photoshop PARM_T
256 daniel-mar 398
                convert_premiere_to_photoshop(pparm, (PARM_T_PREMIERE*)p);
399
        } else {
394 daniel-mar 400
                // It is already Photoshop. Just copy to pparm.
401
                memcpy(pparm, p, sizeof(PARM_T));
256 daniel-mar 402
        }
403
 
394 daniel-mar 404
        // Do we need to do string conversion?
405
        if (frommac) {
456 daniel-mar 406
                int i;
407
 
394 daniel-mar 408
                /* Mac PARM resource stores Pascal strings - convert to C strings, since this is what we work internally with (regardles of OS) */
393 daniel-mar 409
                myp2cstr((unsigned char*)pparm->szCategory);
410
                myp2cstr((unsigned char*)pparm->szTitle);
411
                myp2cstr((unsigned char*)pparm->szCopyright);
412
                myp2cstr((unsigned char*)pparm->szAuthor);
394 daniel-mar 413
                for (i = 0; i < 4; ++i)
393 daniel-mar 414
                        myp2cstr((unsigned char*)pparm->szMap[i]);
394 daniel-mar 415
                for (i = 0; i < 8; ++i)
393 daniel-mar 416
                        myp2cstr((unsigned char*)pparm->szCtl[i]);
256 daniel-mar 417
        }
418
 
394 daniel-mar 419
        // Case #1: Mac is reading Windows (Win16/32/64) plugin
420
        if (fromwin && tomac) {
421
                size_t i;
422
 
423
                // Convert copyright CRLF to CR (actually, just removing LF)
454 daniel-mar 424
                char copyrightCRLF[256] = { 0 };
456 daniel-mar 425
                char* pCopyright = &copyrightCRLF[0];
394 daniel-mar 426
                for (i = 0; i < strlen(pparm->szCopyright); i++) {
427
                        if (pparm->szCopyright[i] != LF) {
456 daniel-mar 428
                                *pCopyright++ = pparm->szCopyright[i];
394 daniel-mar 429
                        }
430
                }
456 daniel-mar 431
                *pCopyright++ = '\0';
394 daniel-mar 432
                strcpy(pparm->szCopyright, copyrightCRLF);
433
 
434
                // these are the only numeric fields we *have* to swap
435
                // all the rest are bool_t flags which (if we're careful) will work in either ordering
436
                for (i = 0; i < 8; ++i)
437
                        pparm->val[i] = EndianS32_LtoN(pparm->val[i]);
438
        }
439
 
440
        // Case #2: Mac is reading Mac (in case the normal resource extraction didn't work)
441
        // Nothing to do
442
 
443
        // Case #3: Windows is reading a Windows plugin (if Resource API failed, e.g. Win64 tries to open Win16 NE file or Win32 tries to open Win64 file)
444
        // Nothing to do
445
 
446
        // Case #4: Windows is reading an old FilterFactory Mac file
447
        // Note: You must read the ".rsrc" resource fork, not the standalone binary!
448
        if (frommac && towin) {
449
                size_t i;
450
 
451
                // Convert CR in the copyright field to CRLF.
454 daniel-mar 452
                char copyrightCRLF[256] = { 0 };
456 daniel-mar 453
                char* pCopyright = &copyrightCRLF[0];
394 daniel-mar 454
                for (i = 0; i < strlen(pparm->szCopyright); i++) {
456 daniel-mar 455
                        *pCopyright++ = pparm->szCopyright[i];
394 daniel-mar 456
                        if (pparm->szCopyright[i] == CR) {
456 daniel-mar 457
                                *pCopyright++ = LF;
394 daniel-mar 458
                        }
459
                }
456 daniel-mar 460
                *pCopyright++ = '\0';
394 daniel-mar 461
                strcpy(pparm->szCopyright, copyrightCRLF);
462
 
463
                // these are the only numeric fields we *have* to swap
464
                // all the rest are bool_t flags which (if we're careful) will work in either ordering
465
                for (i = 0; i < 8; ++i)
466
                        pparm->val[i] = EndianS32_LtoN(pparm->val[i]);
467
        }
468
 
469
        // Now set the values in pparm into the working variables expr[] and slider[], so that they are visible in the GUI
456 daniel-mar 470
        {
471
                int i;
472
                for (i = 0; i < 4; ++i) {
473
                        if (expr[i]) free(expr[i]);
474
                        expr[i] = my_strdup(pparm->szFormula[i]);
475
                }
394 daniel-mar 476
 
456 daniel-mar 477
                for (i = 0; i < 8; ++i) {
478
                        slider[i] = (uint8_t)pparm->val[i];
479
                        /*
480
                        if (slider[i] > 0xFF) {
481
                                // Wrong endianess (e.g. reading a Mac rsrc on Windows)
482
                                // Should not happen since we did the stuff above
483
                                slider[i] = (uint8_t)EndianS32_LtoN(slider[i]);
484
                        }
485
                        */
339 daniel-mar 486
                }
487
        }
256 daniel-mar 488
 
489
        return true;
490
}
491
 
492
Handle readfileintohandle(FILEREF r){
493
        FILEPOS n;
494
        Handle h;
495
        Ptr p;
496
 
497
        if( GetEOF(r,&n) == noErr && (h = PINEWHANDLE(n)) ){
498
                p = PILOCKHANDLE(h,false);
499
                if(SetFPos(r,fsFromStart,0) == noErr && FSRead(r,(FILECOUNT*)&n,p) == noErr){
500
                        PIUNLOCKHANDLE(h);
501
                        return h;
502
                }
503
                PIDISPOSEHANDLE(h);
504
        }
505
        return NULL;
506
}
507
 
385 daniel-mar 508
Boolean _picoLineContainsKey(char* line, char** content, const char* searchkey/*=NULL*/) {
386 daniel-mar 509
        size_t i;
510
        for (i = 0; i < strlen(line); i++) {
388 daniel-mar 511
                if (line[i] == '?') break; // avoid that "a?b:c" is detected as key
385 daniel-mar 512
                if (line[i] == ':') {
386 daniel-mar 513
                        // Note: We are ignoring whitespaces, i.e. " A :" != "A:" (TODO: should we change this?)
514
                        if ((searchkey == NULL) || ((i == strlen(searchkey)) && (memcmp(line, searchkey, i) == 0))) {
515
                                i++; // jump over ':' char
387 daniel-mar 516
                                //while ((line[i] == ' ') || (line[i] == TAB)) i++; // Trim value left
385 daniel-mar 517
                                *content = line + i;
518
                                return true;
519
                        }
520
                }
521
        }
522
        *content = line;
523
        return false;
524
}
525
 
388 daniel-mar 526
void _ffdcomp_removebrackets(char* x, char* maxptr) {
527
        char* closingBracketPos = NULL;
528
        Boolean openingBracketFound = false;
529
        if (x[0] == '[') {
530
                openingBracketFound = true;
531
        }
532
        x[0] = ':';
533
        x++;
534
        while (x < maxptr) {
535
                if ((!openingBracketFound) && (x[0] == '[')) {
536
                        openingBracketFound = true;
537
                        x[0] = ' ';
538
                }
539
                else if (openingBracketFound) {
540
                        if (x[0] == ']') {
541
                                closingBracketPos = x;
542
                        }
543
                        else if ((x[0] == CR) || (x[0] == LF)) {
544
                                if (closingBracketPos) closingBracketPos[0] = ' '; // last closing pos before CR/LF
545
                                break;
546
                        }
547
                }
548
                x++;
549
        }
550
}
551
 
393 daniel-mar 552
// isFormula=false => outputFile is C string. TXT linebreaks become spaces.
386 daniel-mar 553
// isFormula=true  => outputFile is C string. TXT line breaks become CRLF line breaks
554
Boolean _picoReadProperty(char* inputFile, int maxInput, const char* property, char* outputFile, size_t maxOutput, Boolean isFormula) {
555
        int i;
556
        char* outputwork;
557
        char* sline;
558
        char* svalue;
559
        char* inputwork;
560
        char* inputworkinitial;
561
        outputwork = outputFile;
562
        sline = NULL;
563
        svalue = NULL;
385 daniel-mar 564
        // Check parameters
386 daniel-mar 565
        if (maxOutput == 0) return false;
385 daniel-mar 566
        if (inputFile == 0) return false;
567
        // Let input memory be read-only, +1 for terminal zero
568
        //char* inputwork = inputFile;
454 daniel-mar 569
        inputwork = (char*)malloc((size_t)maxInput + 1);
386 daniel-mar 570
        inputworkinitial = inputwork;
385 daniel-mar 571
        if (inputwork == 0) return false;
572
        memcpy(inputwork, inputFile, maxInput);
388 daniel-mar 573
        inputwork[maxInput] = 0; // otherwise strstr() will crash
574
 
575
        // Transform "FFDecomp" TXT file into the similar "PluginCommander" TXT file
576
        if (strstr(inputwork, "Filter Factory Plugin Information:")) {
577
                char* x;
578
                char* k1;
579
                char* k2;
580
                // Metadata:
581
                x = strstr(inputwork, "CATEGORY:");
389 daniel-mar 582
                if (x) memcpy(x, "Category:", strlen("Category:"));
388 daniel-mar 583
                x = strstr(inputwork, "TITLE:");
389 daniel-mar 584
                if (x) memcpy(x, "Title:", strlen("Title:"));
388 daniel-mar 585
                x = strstr(inputwork, "COPYRIGHT:");
389 daniel-mar 586
                if (x) memcpy(x, "Copyright:", strlen("Copyright:"));
388 daniel-mar 587
                x = strstr(inputwork, "AUTHOR:");
389 daniel-mar 588
                if (x) memcpy(x, "Author:", strlen("Author:"));
388 daniel-mar 589
                // Controls:
590
                for (i = 0; i < 8; i++) {
591
                        k1 = (char*)malloc(strlen("Control X:") + 1);
592
                        sprintf(k1, "Control %d:", i);
593
                        x = strstr(inputwork, k1);
594
                        if (x) {
595
                                k2 = (char*)malloc(strlen("ctl[X]:   ") + 1);
596
                                sprintf(k2, "ctl[%d]:   ", i);
389 daniel-mar 597
                                memcpy(x, k2, strlen(k2));
388 daniel-mar 598
                                x += strlen("ctl[X]");
599
                                _ffdcomp_removebrackets(x, inputwork + maxInput - 1);
600
                                free(k2);
601
                        }
602
                        free(k1);
603
                }
604
                // Maps:
605
                for (i = 0; i < 4; i++) {
606
                        k1 = (char*)malloc(strlen("Map X:") + 1);
607
                        sprintf(k1, "Map %d:", i);
608
                        x = strstr(inputwork, k1);
609
                        if (x) {
610
                                k2 = (char*)malloc(strlen("map[X]:") + 1);
611
                                sprintf(k2, "map[%d]:", i);
389 daniel-mar 612
                                memcpy(x, k2, strlen(k2));
388 daniel-mar 613
                                x += strlen("map[X]");
614
                                _ffdcomp_removebrackets(x, inputwork + maxInput - 1);
615
                                free(k2);
616
                        }
617
                        free(k1);
618
                }
619
                // Convert all '\r' to '\n' for the next step to be easier
620
                for (i = 0; i < maxInput; i++) {
621
                        if (inputworkinitial[i] == CR) inputworkinitial[i] = LF;
622
                }
623
                x = strstr(inputwork, "\nR=\n");
389 daniel-mar 624
                if (x) memcpy(x, "\nR:\n", strlen("\nR:\n"));
388 daniel-mar 625
                x = strstr(inputwork, "\nG=\n");
389 daniel-mar 626
                if (x) memcpy(x, "\nG:\n", strlen("\nG:\n"));
388 daniel-mar 627
                x = strstr(inputwork, "\nB=\n");
389 daniel-mar 628
                if (x) memcpy(x, "\nB:\n", strlen("\nB:\n"));
388 daniel-mar 629
                x = strstr(inputwork, "\nA=\n");
389 daniel-mar 630
                if (x) memcpy(x, "\nA:\n", strlen("\nA:\n"));
388 daniel-mar 631
        }
385 daniel-mar 632
        // Replace all \r and \n with \0, so that we can parse easier
386 daniel-mar 633
        for (i = 0; i < maxInput; i++) {
388 daniel-mar 634
                if (inputworkinitial[i] == CR) inputworkinitial[i] = 0;
393 daniel-mar 635
                else if (inputworkinitial[i] == LF) inputworkinitial[i] = 0;
385 daniel-mar 636
        }
388 daniel-mar 637
 
385 daniel-mar 638
        // Find line that contains out key
388 daniel-mar 639
        inputwork = inputworkinitial;
385 daniel-mar 640
        do {
641
                if (inputwork > inputworkinitial + maxInput) {
642
                        // Key not found. Set output to empty string
643
                        outputwork[0] = 0;
644
                        free(inputworkinitial);
645
                        return false;
646
                }
647
                sline = inputwork;
648
                inputwork += strlen(sline) + 1;
649
                if (inputwork - 1 > inputworkinitial + maxInput) {
650
                        // Key not found. Set output to empty string
651
                        // TODO: will that be ever called?
652
                        outputwork[0] = 0;
653
                        free(inputworkinitial);
654
                        return false;
655
                }
656
        } while (!_picoLineContainsKey(sline, &svalue, property));
388 daniel-mar 657
 
385 daniel-mar 658
        // Read line(s) until we find a line with another key, or the line end
659
        do {
387 daniel-mar 660
                while ((svalue[0] == ' ') || (svalue[0] == TAB)) svalue++; // Trim left
661
                while ((svalue[strlen(svalue) - 1] == ' ') || (svalue[strlen(svalue) - 1] == TAB)) svalue[strlen(svalue) - 1] = 0; // Trim right
386 daniel-mar 662
 
385 daniel-mar 663
                if (strlen(svalue) > 0) {
393 daniel-mar 664
                        if (outputwork + strlen(svalue) + (isFormula ? 3/*CRLF+NUL*/ : 2/*space+NUL*/) > outputFile + maxOutput) {
444 daniel-mar 665
                                size_t remaining = maxOutput - (outputwork - outputFile) - 1;
385 daniel-mar 666
                                //printf("BUFFER FULL (remaining = %d)\n", remaining);
667
                                memcpy(outputwork, svalue, remaining);
668
                                outputwork += remaining;
669
                                outputwork[0] = 0;
670
                                free(inputworkinitial);
671
                                return true;
672
                        }
673
                        else {
674
                                memcpy(outputwork, svalue, strlen(svalue));
675
                                outputwork += strlen(svalue);
386 daniel-mar 676
                                if (isFormula) {
677
                                        // Formulas: TXT line break stays line break (important if you have comments!)
388 daniel-mar 678
                                        outputwork[0] = CR;
679
                                        outputwork[1] = LF;
387 daniel-mar 680
                                        outputwork += 2;
386 daniel-mar 681
                                }
682
                                else {
683
                                        // Everything else: TXT line breaks becomes single whitespace
684
                                        outputwork[0] = ' ';
685
                                        outputwork++;
686
                                }
385 daniel-mar 687
                        }
688
                }
689
                outputwork[0] = 0;
690
 
691
                // Process next line
692
                if (inputwork > inputworkinitial + maxInput) break;
693
                sline = inputwork;
694
                inputwork += strlen(sline) + 1;
386 daniel-mar 695
                if (inputwork - 1 > inputworkinitial + maxInput) break; // TODO: will that be ever called?
385 daniel-mar 696
        } while (!_picoLineContainsKey(sline, &svalue, NULL));
388 daniel-mar 697
 
385 daniel-mar 698
        // Remove trailing whitespace
699
        if (outputwork > outputFile) {
700
                outputwork -= 1;
701
                outputwork[0] = 0;
702
        }
703
        free(inputworkinitial);
704
        return true;
705
}
706
 
707
Boolean readfile_picotxt(StandardFileReply* sfr, char** reason) {
389 daniel-mar 708
        extern int ctls[], maps[];
709
 
385 daniel-mar 710
        Handle h;
711
        Boolean res = false;
712
        FILEREF refnum;
713
 
433 daniel-mar 714
        UNREFERENCED_PARAMETER(reason);
715
 
444 daniel-mar 716
        if (!fileHasExtension(sfr, TEXT(".txt"))) return false;
385 daniel-mar 717
 
718
        if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
719
                if ((h = readfileintohandle(refnum))) {
720
                        FILECOUNT count = PIGETHANDLESIZE(h);
721
                        char* q = PILOCKHANDLE(h, false);
722
 
723
                        char out[256];
724
                        if (_picoReadProperty(q, count, "Title", out, sizeof(out), false)) {
386 daniel-mar 725
                                int i;
385 daniel-mar 726
 
727
                                // Plugin infos
393 daniel-mar 728
                                _picoReadProperty(q, count, "Title", gdata->parm.szTitle, sizeof(gdata->parm.szTitle), false);
729
                                _picoReadProperty(q, count, "Category", gdata->parm.szCategory, sizeof(gdata->parm.szCategory), false);
730
                                _picoReadProperty(q, count, "Author", gdata->parm.szAuthor, sizeof(gdata->parm.szAuthor), false);
731
                                _picoReadProperty(q, count, "Copyright", gdata->parm.szCopyright, sizeof(gdata->parm.szCopyright), false);
732
                                //_picoReadProperty(q, count, "Filename", gdata->parm.xxx, sizeof(gdata->parm.xxx), false);
385 daniel-mar 733
 
734
                                // Expressions
393 daniel-mar 735
                                if (!_picoReadProperty(q, count, "R", gdata->parm.szFormula[0], sizeof(gdata->parm.szFormula[0]), true))
736
                                        strcpy(gdata->parm.szFormula[0], "r");
737
                                if (!_picoReadProperty(q, count, "G", gdata->parm.szFormula[1], sizeof(gdata->parm.szFormula[1]), true))
738
                                        strcpy(gdata->parm.szFormula[1], "g");
739
                                if (!_picoReadProperty(q, count, "B", gdata->parm.szFormula[2], sizeof(gdata->parm.szFormula[2]), true))
740
                                        strcpy(gdata->parm.szFormula[2], "b");
741
                                if (!_picoReadProperty(q, count, "A", gdata->parm.szFormula[3], sizeof(gdata->parm.szFormula[3]), true))
742
                                        strcpy(gdata->parm.szFormula[3], "a");
386 daniel-mar 743
                                for (i = 0; i < 4; i++) {
385 daniel-mar 744
                                        if (expr[i]) free(expr[i]);
393 daniel-mar 745
                                        expr[i] = my_strdup(gdata->parm.szFormula[i]);
385 daniel-mar 746
                                }
747
 
748
                                // Slider names
386 daniel-mar 749
                                for (i = 0; i < 8; i++) {
385 daniel-mar 750
                                        char keyname[7];
751
                                        sprintf(keyname, "ctl[%d]", i);
393 daniel-mar 752
                                        _picoReadProperty(q, count, keyname, gdata->parm.szCtl[i], sizeof(gdata->parm.szCtl[i]), false);
385 daniel-mar 753
                                }
754
 
755
                                // Slider values
386 daniel-mar 756
                                for (i = 0; i < 8; i++) {
757
                                        char keyname[7], tmp[5];
453 daniel-mar 758
                                        int v;
385 daniel-mar 759
                                        sprintf(keyname, "val[%d]", i);
760
                                        if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) {
761
                                                sprintf(keyname, "def[%d]", i);
762
                                                if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) {
393 daniel-mar 763
                                                        strcpy(tmp,"0");
385 daniel-mar 764
                                                }
765
                                        }
453 daniel-mar 766
                                        v = atoi(tmp);
767
                                        if (v < 0) v = 0;
456 daniel-mar 768
                                        else if (v > 255) v = 255;
769
                                        gdata->parm.val[i] = slider[i] = (uint8_t)v;
385 daniel-mar 770
                                }
771
 
772
                                // Map names
386 daniel-mar 773
                                for (i = 0; i < 4; i++) {
385 daniel-mar 774
                                        char keyname[7];
775
                                        sprintf(keyname, "map[%d]", i);
393 daniel-mar 776
                                        _picoReadProperty(q, count, keyname, gdata->parm.szMap[i], sizeof(gdata->parm.szMap[i]), false);
385 daniel-mar 777
                                }
778
 
389 daniel-mar 779
                                //These will be set when the expressions are evaluated anyway. So this part is optional:
780
                                checksliders(4, ctls, maps);
781
                                for (i = 0; i < 8; i++) gdata->parm.ctl_used[i] = ctls[i];
782
                                for (i = 0; i < 4; i++) gdata->parm.map_used[i] = maps[i];
385 daniel-mar 783
 
784
                                res = true;
785
                        }
786
 
787
                        PIUNLOCKHANDLE(h);
788
                        PIDISPOSEHANDLE(h);
789
                }
790
                FSClose(refnum);
791
        }
792
 
793
        return res;
794
}
795
 
366 daniel-mar 796
Boolean readfile_afs_pff(StandardFileReply *sfr,char **reason){
256 daniel-mar 797
        FILEREF r;
798
        Handle h;
799
        Boolean res = false;
800
 
801
        if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&r) == noErr){
802
                if( (h = readfileintohandle(r)) ){
408 daniel-mar 803
                        if( (res = readparams_afs_pff(h,reason)) ) {
256 daniel-mar 804
                                gdata->standalone = false; // so metadata fields will default, if user chooses Make...
805
 
444 daniel-mar 806
                                if (fileHasExtension(sfr, TEXT(".pff"))) {
256 daniel-mar 807
                                        // If it is a Premiere settings file, we need to swap the channels red and blue
263 daniel-mar 808
                                        // We just swap the pointers!
256 daniel-mar 809
                                        char* tmp;
263 daniel-mar 810
                                        tmp = expr[0];
811
                                        expr[0] = expr[2];
812
                                        expr[2] = tmp;
256 daniel-mar 813
                                }
814
                        }
815
 
816
                        PIDISPOSEHANDLE(h);
817
                }
818
                FSClose(r);
819
        }else
820
                *reason = _strdup("Could not open the file.");
821
 
822
        return res;
823
}