Subversion Repositories filter_foundry

Rev

Rev 453 | Rev 456 | 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;
83
                                if (v > 255) v = 255;
84
                                slider[linecnt-1] = 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;
304
                                                if (v > 255) v = 255;
305
                                                slider[i] = 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){
256 daniel-mar 363
        int i;
394 daniel-mar 364
        Boolean towin, tomac, fromwin, frommac;
365
        unsigned int signature = *((unsigned int*)p);
366
        unsigned int standalone = *((unsigned int*)p+1);
256 daniel-mar 367
 
394 daniel-mar 368
        // Find out our OS ("reader") the OS of the plugin ("source")
369
        #ifdef MAC_ENV
370
        towin = false;
371
        tomac = true;
372
        fromwin = ((EndianS32_LtoN(signature) == PARM_SIZE) ||
373
                (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) ||
374
                (EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1;
375
        frommac = ((signature == PARM_SIZE) ||
376
                (signature == PARM_SIZE_PREMIERE) ||
377
                (signature == PARM_SIG_MAC)) && standalone == 1;
378
        #else
379
        towin = true;
380
        tomac = false;
381
        fromwin = ((signature == PARM_SIZE) ||
382
                (signature == PARM_SIZE_PREMIERE) ||
383
                (signature == PARM_SIG_MAC)) && standalone == 1;
384
        frommac = ((EndianS32_LtoN(signature) == PARM_SIZE) ||
385
                (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE) ||
386
                (EndianS32_LtoN(signature) == PARM_SIG_MAC)) && EndianS32_LtoN(standalone) == 1;
387
        #endif
388
 
389
        // Is it a valid signature?
390
        if (!fromwin && !frommac) {
391
                // No valid signature found
392
                return false;
393
        }
394
 
395
        // Does it come from Premiere or Photoshop?
396
        // Initialize pparm
397
        if ((signature == PARM_SIZE_PREMIERE) || (EndianS32_LtoN(signature) == PARM_SIZE_PREMIERE)) {
398
                // It comes from Premiere. Swap R and B channel and convert to a Photoshop PARM_T
256 daniel-mar 399
                convert_premiere_to_photoshop(pparm, (PARM_T_PREMIERE*)p);
400
        } else {
394 daniel-mar 401
                // It is already Photoshop. Just copy to pparm.
402
                memcpy(pparm, p, sizeof(PARM_T));
256 daniel-mar 403
        }
404
 
394 daniel-mar 405
        // Do we need to do string conversion?
406
        if (frommac) {
407
                /* Mac PARM resource stores Pascal strings - convert to C strings, since this is what we work internally with (regardles of OS) */
393 daniel-mar 408
                myp2cstr((unsigned char*)pparm->szCategory);
409
                myp2cstr((unsigned char*)pparm->szTitle);
410
                myp2cstr((unsigned char*)pparm->szCopyright);
411
                myp2cstr((unsigned char*)pparm->szAuthor);
394 daniel-mar 412
                for (i = 0; i < 4; ++i)
393 daniel-mar 413
                        myp2cstr((unsigned char*)pparm->szMap[i]);
394 daniel-mar 414
                for (i = 0; i < 8; ++i)
393 daniel-mar 415
                        myp2cstr((unsigned char*)pparm->szCtl[i]);
256 daniel-mar 416
        }
417
 
394 daniel-mar 418
        // Case #1: Mac is reading Windows (Win16/32/64) plugin
419
        if (fromwin && tomac) {
420
                size_t i;
421
 
422
                // Convert copyright CRLF to CR (actually, just removing LF)
454 daniel-mar 423
                char copyrightCRLF[256] = { 0 };
394 daniel-mar 424
                char* p = &copyrightCRLF[0];
425
                for (i = 0; i < strlen(pparm->szCopyright); i++) {
426
                        if (pparm->szCopyright[i] != LF) {
427
                                *p++ = pparm->szCopyright[i];
428
                        }
429
                }
430
                *p++ = '\0';
431
                strcpy(pparm->szCopyright, copyrightCRLF);
432
 
433
                // these are the only numeric fields we *have* to swap
434
                // all the rest are bool_t flags which (if we're careful) will work in either ordering
435
                for (i = 0; i < 8; ++i)
436
                        pparm->val[i] = EndianS32_LtoN(pparm->val[i]);
437
        }
438
 
439
        // Case #2: Mac is reading Mac (in case the normal resource extraction didn't work)
440
        // Nothing to do
441
 
442
        // 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)
443
        // Nothing to do
444
 
445
        // Case #4: Windows is reading an old FilterFactory Mac file
446
        // Note: You must read the ".rsrc" resource fork, not the standalone binary!
447
        if (frommac && towin) {
448
                size_t i;
449
 
450
                // Convert CR in the copyright field to CRLF.
454 daniel-mar 451
                char copyrightCRLF[256] = { 0 };
394 daniel-mar 452
                char* p = &copyrightCRLF[0];
453
                for (i = 0; i < strlen(pparm->szCopyright); i++) {
454
                        *p++ = pparm->szCopyright[i];
455
                        if (pparm->szCopyright[i] == CR) {
456
                                *p++ = LF;
457
                        }
458
                }
459
                *p++ = '\0';
460
                strcpy(pparm->szCopyright, copyrightCRLF);
461
 
462
                // these are the only numeric fields we *have* to swap
463
                // all the rest are bool_t flags which (if we're careful) will work in either ordering
464
                for (i = 0; i < 8; ++i)
465
                        pparm->val[i] = EndianS32_LtoN(pparm->val[i]);
466
        }
467
 
468
        // Now set the values in pparm into the working variables expr[] and slider[], so that they are visible in the GUI
469
 
256 daniel-mar 470
        for(i = 0; i < 4; ++i){
471
                if(expr[i]) free(expr[i]);
393 daniel-mar 472
                expr[i] = my_strdup(pparm->szFormula[i]);
256 daniel-mar 473
        }
474
 
339 daniel-mar 475
        for (i = 0; i < 8; ++i) {
394 daniel-mar 476
                slider[i] = (uint8_t)pparm->val[i];
477
                /*
478
                if (slider[i] > 0xFF) {
339 daniel-mar 479
                        // Wrong endianess (e.g. reading a Mac rsrc on Windows)
394 daniel-mar 480
                        // Should not happen since we did the stuff above
481
                        slider[i] = (uint8_t)EndianS32_LtoN(slider[i]);
339 daniel-mar 482
                }
394 daniel-mar 483
                */
339 daniel-mar 484
        }
256 daniel-mar 485
 
486
        return true;
487
}
488
 
489
Handle readfileintohandle(FILEREF r){
490
        FILEPOS n;
491
        Handle h;
492
        Ptr p;
493
 
494
        if( GetEOF(r,&n) == noErr && (h = PINEWHANDLE(n)) ){
495
                p = PILOCKHANDLE(h,false);
496
                if(SetFPos(r,fsFromStart,0) == noErr && FSRead(r,(FILECOUNT*)&n,p) == noErr){
497
                        PIUNLOCKHANDLE(h);
498
                        return h;
499
                }
500
                PIDISPOSEHANDLE(h);
501
        }
502
        return NULL;
503
}
504
 
385 daniel-mar 505
Boolean _picoLineContainsKey(char* line, char** content, const char* searchkey/*=NULL*/) {
386 daniel-mar 506
        size_t i;
507
        for (i = 0; i < strlen(line); i++) {
388 daniel-mar 508
                if (line[i] == '?') break; // avoid that "a?b:c" is detected as key
385 daniel-mar 509
                if (line[i] == ':') {
386 daniel-mar 510
                        // Note: We are ignoring whitespaces, i.e. " A :" != "A:" (TODO: should we change this?)
511
                        if ((searchkey == NULL) || ((i == strlen(searchkey)) && (memcmp(line, searchkey, i) == 0))) {
512
                                i++; // jump over ':' char
387 daniel-mar 513
                                //while ((line[i] == ' ') || (line[i] == TAB)) i++; // Trim value left
385 daniel-mar 514
                                *content = line + i;
515
                                return true;
516
                        }
517
                }
518
        }
519
        *content = line;
520
        return false;
521
}
522
 
388 daniel-mar 523
void _ffdcomp_removebrackets(char* x, char* maxptr) {
524
        char* closingBracketPos = NULL;
525
        Boolean openingBracketFound = false;
526
        if (x[0] == '[') {
527
                openingBracketFound = true;
528
        }
529
        x[0] = ':';
530
        x++;
531
        while (x < maxptr) {
532
                if ((!openingBracketFound) && (x[0] == '[')) {
533
                        openingBracketFound = true;
534
                        x[0] = ' ';
535
                }
536
                else if (openingBracketFound) {
537
                        if (x[0] == ']') {
538
                                closingBracketPos = x;
539
                        }
540
                        else if ((x[0] == CR) || (x[0] == LF)) {
541
                                if (closingBracketPos) closingBracketPos[0] = ' '; // last closing pos before CR/LF
542
                                break;
543
                        }
544
                }
545
                x++;
546
        }
547
}
548
 
393 daniel-mar 549
// isFormula=false => outputFile is C string. TXT linebreaks become spaces.
386 daniel-mar 550
// isFormula=true  => outputFile is C string. TXT line breaks become CRLF line breaks
551
Boolean _picoReadProperty(char* inputFile, int maxInput, const char* property, char* outputFile, size_t maxOutput, Boolean isFormula) {
552
        int i;
553
        char* outputwork;
554
        char* sline;
555
        char* svalue;
556
        char* inputwork;
557
        char* inputworkinitial;
558
        outputwork = outputFile;
559
        sline = NULL;
560
        svalue = NULL;
385 daniel-mar 561
        // Check parameters
386 daniel-mar 562
        if (maxOutput == 0) return false;
385 daniel-mar 563
        if (inputFile == 0) return false;
564
        // Let input memory be read-only, +1 for terminal zero
565
        //char* inputwork = inputFile;
454 daniel-mar 566
        inputwork = (char*)malloc((size_t)maxInput + 1);
386 daniel-mar 567
        inputworkinitial = inputwork;
385 daniel-mar 568
        if (inputwork == 0) return false;
569
        memcpy(inputwork, inputFile, maxInput);
388 daniel-mar 570
        inputwork[maxInput] = 0; // otherwise strstr() will crash
571
 
572
        // Transform "FFDecomp" TXT file into the similar "PluginCommander" TXT file
573
        if (strstr(inputwork, "Filter Factory Plugin Information:")) {
574
                char* x;
575
                char* k1;
576
                char* k2;
577
                // Metadata:
578
                x = strstr(inputwork, "CATEGORY:");
389 daniel-mar 579
                if (x) memcpy(x, "Category:", strlen("Category:"));
388 daniel-mar 580
                x = strstr(inputwork, "TITLE:");
389 daniel-mar 581
                if (x) memcpy(x, "Title:", strlen("Title:"));
388 daniel-mar 582
                x = strstr(inputwork, "COPYRIGHT:");
389 daniel-mar 583
                if (x) memcpy(x, "Copyright:", strlen("Copyright:"));
388 daniel-mar 584
                x = strstr(inputwork, "AUTHOR:");
389 daniel-mar 585
                if (x) memcpy(x, "Author:", strlen("Author:"));
388 daniel-mar 586
                // Controls:
587
                for (i = 0; i < 8; i++) {
588
                        k1 = (char*)malloc(strlen("Control X:") + 1);
589
                        sprintf(k1, "Control %d:", i);
590
                        x = strstr(inputwork, k1);
591
                        if (x) {
592
                                k2 = (char*)malloc(strlen("ctl[X]:   ") + 1);
593
                                sprintf(k2, "ctl[%d]:   ", i);
389 daniel-mar 594
                                memcpy(x, k2, strlen(k2));
388 daniel-mar 595
                                x += strlen("ctl[X]");
596
                                _ffdcomp_removebrackets(x, inputwork + maxInput - 1);
597
                                free(k2);
598
                        }
599
                        free(k1);
600
                }
601
                // Maps:
602
                for (i = 0; i < 4; i++) {
603
                        k1 = (char*)malloc(strlen("Map X:") + 1);
604
                        sprintf(k1, "Map %d:", i);
605
                        x = strstr(inputwork, k1);
606
                        if (x) {
607
                                k2 = (char*)malloc(strlen("map[X]:") + 1);
608
                                sprintf(k2, "map[%d]:", i);
389 daniel-mar 609
                                memcpy(x, k2, strlen(k2));
388 daniel-mar 610
                                x += strlen("map[X]");
611
                                _ffdcomp_removebrackets(x, inputwork + maxInput - 1);
612
                                free(k2);
613
                        }
614
                        free(k1);
615
                }
616
                // Convert all '\r' to '\n' for the next step to be easier
617
                for (i = 0; i < maxInput; i++) {
618
                        if (inputworkinitial[i] == CR) inputworkinitial[i] = LF;
619
                }
620
                x = strstr(inputwork, "\nR=\n");
389 daniel-mar 621
                if (x) memcpy(x, "\nR:\n", strlen("\nR:\n"));
388 daniel-mar 622
                x = strstr(inputwork, "\nG=\n");
389 daniel-mar 623
                if (x) memcpy(x, "\nG:\n", strlen("\nG:\n"));
388 daniel-mar 624
                x = strstr(inputwork, "\nB=\n");
389 daniel-mar 625
                if (x) memcpy(x, "\nB:\n", strlen("\nB:\n"));
388 daniel-mar 626
                x = strstr(inputwork, "\nA=\n");
389 daniel-mar 627
                if (x) memcpy(x, "\nA:\n", strlen("\nA:\n"));
388 daniel-mar 628
        }
385 daniel-mar 629
        // Replace all \r and \n with \0, so that we can parse easier
386 daniel-mar 630
        for (i = 0; i < maxInput; i++) {
388 daniel-mar 631
                if (inputworkinitial[i] == CR) inputworkinitial[i] = 0;
393 daniel-mar 632
                else if (inputworkinitial[i] == LF) inputworkinitial[i] = 0;
385 daniel-mar 633
        }
388 daniel-mar 634
 
385 daniel-mar 635
        // Find line that contains out key
388 daniel-mar 636
        inputwork = inputworkinitial;
385 daniel-mar 637
        do {
638
                if (inputwork > inputworkinitial + maxInput) {
639
                        // Key not found. Set output to empty string
640
                        outputwork[0] = 0;
641
                        free(inputworkinitial);
642
                        return false;
643
                }
644
                sline = inputwork;
645
                inputwork += strlen(sline) + 1;
646
                if (inputwork - 1 > inputworkinitial + maxInput) {
647
                        // Key not found. Set output to empty string
648
                        // TODO: will that be ever called?
649
                        outputwork[0] = 0;
650
                        free(inputworkinitial);
651
                        return false;
652
                }
653
        } while (!_picoLineContainsKey(sline, &svalue, property));
388 daniel-mar 654
 
385 daniel-mar 655
        // Read line(s) until we find a line with another key, or the line end
656
        do {
387 daniel-mar 657
                while ((svalue[0] == ' ') || (svalue[0] == TAB)) svalue++; // Trim left
658
                while ((svalue[strlen(svalue) - 1] == ' ') || (svalue[strlen(svalue) - 1] == TAB)) svalue[strlen(svalue) - 1] = 0; // Trim right
386 daniel-mar 659
 
385 daniel-mar 660
                if (strlen(svalue) > 0) {
393 daniel-mar 661
                        if (outputwork + strlen(svalue) + (isFormula ? 3/*CRLF+NUL*/ : 2/*space+NUL*/) > outputFile + maxOutput) {
444 daniel-mar 662
                                size_t remaining = maxOutput - (outputwork - outputFile) - 1;
385 daniel-mar 663
                                //printf("BUFFER FULL (remaining = %d)\n", remaining);
664
                                memcpy(outputwork, svalue, remaining);
665
                                outputwork += remaining;
666
                                outputwork[0] = 0;
667
                                free(inputworkinitial);
668
                                return true;
669
                        }
670
                        else {
671
                                memcpy(outputwork, svalue, strlen(svalue));
672
                                outputwork += strlen(svalue);
386 daniel-mar 673
                                if (isFormula) {
674
                                        // Formulas: TXT line break stays line break (important if you have comments!)
388 daniel-mar 675
                                        outputwork[0] = CR;
676
                                        outputwork[1] = LF;
387 daniel-mar 677
                                        outputwork += 2;
386 daniel-mar 678
                                }
679
                                else {
680
                                        // Everything else: TXT line breaks becomes single whitespace
681
                                        outputwork[0] = ' ';
682
                                        outputwork++;
683
                                }
385 daniel-mar 684
                        }
685
                }
686
                outputwork[0] = 0;
687
 
688
                // Process next line
689
                if (inputwork > inputworkinitial + maxInput) break;
690
                sline = inputwork;
691
                inputwork += strlen(sline) + 1;
386 daniel-mar 692
                if (inputwork - 1 > inputworkinitial + maxInput) break; // TODO: will that be ever called?
385 daniel-mar 693
        } while (!_picoLineContainsKey(sline, &svalue, NULL));
388 daniel-mar 694
 
385 daniel-mar 695
        // Remove trailing whitespace
696
        if (outputwork > outputFile) {
697
                outputwork -= 1;
698
                outputwork[0] = 0;
699
        }
700
        free(inputworkinitial);
701
        return true;
702
}
703
 
704
Boolean readfile_picotxt(StandardFileReply* sfr, char** reason) {
389 daniel-mar 705
        extern int ctls[], maps[];
706
 
385 daniel-mar 707
        Handle h;
708
        Boolean res = false;
709
        FILEREF refnum;
710
 
433 daniel-mar 711
        UNREFERENCED_PARAMETER(reason);
712
 
444 daniel-mar 713
        if (!fileHasExtension(sfr, TEXT(".txt"))) return false;
385 daniel-mar 714
 
715
        if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) {
716
                if ((h = readfileintohandle(refnum))) {
717
                        FILECOUNT count = PIGETHANDLESIZE(h);
718
                        char* q = PILOCKHANDLE(h, false);
719
 
720
                        char out[256];
721
                        if (_picoReadProperty(q, count, "Title", out, sizeof(out), false)) {
386 daniel-mar 722
                                int i;
385 daniel-mar 723
 
724
                                // Plugin infos
393 daniel-mar 725
                                _picoReadProperty(q, count, "Title", gdata->parm.szTitle, sizeof(gdata->parm.szTitle), false);
726
                                _picoReadProperty(q, count, "Category", gdata->parm.szCategory, sizeof(gdata->parm.szCategory), false);
727
                                _picoReadProperty(q, count, "Author", gdata->parm.szAuthor, sizeof(gdata->parm.szAuthor), false);
728
                                _picoReadProperty(q, count, "Copyright", gdata->parm.szCopyright, sizeof(gdata->parm.szCopyright), false);
729
                                //_picoReadProperty(q, count, "Filename", gdata->parm.xxx, sizeof(gdata->parm.xxx), false);
385 daniel-mar 730
 
731
                                // Expressions
393 daniel-mar 732
                                if (!_picoReadProperty(q, count, "R", gdata->parm.szFormula[0], sizeof(gdata->parm.szFormula[0]), true))
733
                                        strcpy(gdata->parm.szFormula[0], "r");
734
                                if (!_picoReadProperty(q, count, "G", gdata->parm.szFormula[1], sizeof(gdata->parm.szFormula[1]), true))
735
                                        strcpy(gdata->parm.szFormula[1], "g");
736
                                if (!_picoReadProperty(q, count, "B", gdata->parm.szFormula[2], sizeof(gdata->parm.szFormula[2]), true))
737
                                        strcpy(gdata->parm.szFormula[2], "b");
738
                                if (!_picoReadProperty(q, count, "A", gdata->parm.szFormula[3], sizeof(gdata->parm.szFormula[3]), true))
739
                                        strcpy(gdata->parm.szFormula[3], "a");
386 daniel-mar 740
                                for (i = 0; i < 4; i++) {
385 daniel-mar 741
                                        if (expr[i]) free(expr[i]);
393 daniel-mar 742
                                        expr[i] = my_strdup(gdata->parm.szFormula[i]);
385 daniel-mar 743
                                }
744
 
745
                                // Slider names
386 daniel-mar 746
                                for (i = 0; i < 8; i++) {
385 daniel-mar 747
                                        char keyname[7];
748
                                        sprintf(keyname, "ctl[%d]", i);
393 daniel-mar 749
                                        _picoReadProperty(q, count, keyname, gdata->parm.szCtl[i], sizeof(gdata->parm.szCtl[i]), false);
385 daniel-mar 750
                                }
751
 
752
                                // Slider values
386 daniel-mar 753
                                for (i = 0; i < 8; i++) {
754
                                        char keyname[7], tmp[5];
453 daniel-mar 755
                                        int v;
385 daniel-mar 756
                                        sprintf(keyname, "val[%d]", i);
757
                                        if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) {
758
                                                sprintf(keyname, "def[%d]", i);
759
                                                if (!_picoReadProperty(q, count, keyname, tmp, sizeof(tmp), false)) {
393 daniel-mar 760
                                                        strcpy(tmp,"0");
385 daniel-mar 761
                                                }
762
                                        }
453 daniel-mar 763
                                        v = atoi(tmp);
764
                                        if (v < 0) v = 0;
765
                                        if (v > 255) v = 255;
766
                                        gdata->parm.val[i] = slider[i] = v;
385 daniel-mar 767
                                }
768
 
769
                                // Map names
386 daniel-mar 770
                                for (i = 0; i < 4; i++) {
385 daniel-mar 771
                                        char keyname[7];
772
                                        sprintf(keyname, "map[%d]", i);
393 daniel-mar 773
                                        _picoReadProperty(q, count, keyname, gdata->parm.szMap[i], sizeof(gdata->parm.szMap[i]), false);
385 daniel-mar 774
                                }
775
 
389 daniel-mar 776
                                //These will be set when the expressions are evaluated anyway. So this part is optional:
777
                                checksliders(4, ctls, maps);
778
                                for (i = 0; i < 8; i++) gdata->parm.ctl_used[i] = ctls[i];
779
                                for (i = 0; i < 4; i++) gdata->parm.map_used[i] = maps[i];
385 daniel-mar 780
 
781
                                res = true;
782
                        }
783
 
784
                        PIUNLOCKHANDLE(h);
785
                        PIDISPOSEHANDLE(h);
786
                }
787
                FSClose(refnum);
788
        }
789
 
790
        return res;
791
}
792
 
366 daniel-mar 793
Boolean readfile_afs_pff(StandardFileReply *sfr,char **reason){
256 daniel-mar 794
        FILEREF r;
795
        Handle h;
796
        Boolean res = false;
797
 
798
        if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&r) == noErr){
799
                if( (h = readfileintohandle(r)) ){
408 daniel-mar 800
                        if( (res = readparams_afs_pff(h,reason)) ) {
256 daniel-mar 801
                                gdata->standalone = false; // so metadata fields will default, if user chooses Make...
802
 
444 daniel-mar 803
                                if (fileHasExtension(sfr, TEXT(".pff"))) {
256 daniel-mar 804
                                        // If it is a Premiere settings file, we need to swap the channels red and blue
263 daniel-mar 805
                                        // We just swap the pointers!
256 daniel-mar 806
                                        char* tmp;
263 daniel-mar 807
                                        tmp = expr[0];
808
                                        expr[0] = expr[2];
809
                                        expr[2] = tmp;
256 daniel-mar 810
                                }
811
                        }
812
 
813
                        PIDISPOSEHANDLE(h);
814
                }
815
                FSClose(r);
816
        }else
817
                *reason = _strdup("Could not open the file.");
818
 
819
        return res;
820
}