Subversion Repositories filter_foundry

Rev

Rev 505 | Rev 536 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 505 Rev 518
1
/*
1
/*
2
    This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
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
3
    Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
4
    Copyright (C) 2018-2022 Daniel Marschall, ViaThinkSoft
4
    Copyright (C) 2018-2022 Daniel Marschall, ViaThinkSoft
5
 
5
 
6
    This program is free software; you can redistribute it and/or modify
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
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
8
    the Free Software Foundation; either version 2 of the License, or
9
    (at your option) any later version.
9
    (at your option) any later version.
10
 
10
 
11
    This program is distributed in the hope that it will be useful,
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
14
    GNU General Public License for more details.
15
 
15
 
16
    You should have received a copy of the GNU General Public License
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
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
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
*/
19
*/
20
 
20
 
21
#include "ff.h"
21
#include "ff.h"
22
 
22
 
23
#include "file_compat.h"
23
#include "file_compat.h"
24
#include "sprintf_tiny.h"
24
#include "sprintf_tiny.h"
25
 
25
 
26
enum{ CHOPLINES = 63 };
26
#define CHOPLINES 63
27
 
27
 
28
OSErr putstr(Handle h,char *s);
28
OSErr putstr(Handle h,char *s);
29
 
29
 
30
OSErr putstr(Handle h,char *s){
30
OSErr putstr(Handle h,char *s){
31
        Ptr p;
31
        Ptr p;
32
        OSErr e;
32
        OSErr e;
33
        size_t size, n;
33
        size_t size, n;
34
 
34
 
35
        if (!h) return nilHandleErr;
35
        if (!h) return nilHandleErr;
36
 
36
 
37
        size = PIGETHANDLESIZE(h);
37
        size = PIGETHANDLESIZE(h);
38
        n = strlen(s);
38
        n = strlen(s);
39
 
39
 
40
        if(!(e = PISETHANDLESIZE(h,(int32)(size+n)))){
40
        if(!(e = PISETHANDLESIZE(h,(int32)(size+n)))){
41
                p = PILOCKHANDLE(h,false);
41
                p = PILOCKHANDLE(h,false);
42
                memcpy(p+size,s,n);
42
                memcpy(p+size,s,n);
43
                PIUNLOCKHANDLE(h);
43
                PIUNLOCKHANDLE(h);
44
        }
44
        }
45
        return e;
45
        return e;
46
}
46
}
47
 
47
 
48
OSErr saveparams_afs_pff(Handle h){
48
OSErr saveparams_afs_pff(Handle h){
49
        char outbuf[CHOPLINES * 2 + 2] = "";
49
        char outbuf[CHOPLINES * 2 + 2] = "";
50
        char *q, * p, * r, * start;
50
        char *q, * p, * r, * start;
51
        size_t n, chunk, j;
51
        size_t n, chunk, j;
52
        int i;
52
        int i;
53
        OSErr e;
53
        OSErr e;
54
        size_t est;
54
        size_t est;
55
        static char afs_sig[] = "%RGB-1.0\r";
55
        static char afs_sig[] = "%RGB-1.0\r";
56
 
56
 
57
        if (!h) return nilHandleErr;
57
        if (!h) return nilHandleErr;
58
 
58
 
59
        est = strlen(expr[0]) + strlen(expr[1]) + strlen(expr[2]) + strlen(expr[3]);
59
        est = strlen(expr[0]) + strlen(expr[1]) + strlen(expr[2]) + strlen(expr[3]);
60
        // do not be tempted to combine into one expression: 'est' is referenced below
60
        // do not be tempted to combine into one expression: 'est' is referenced below
61
        est += strlen(afs_sig) + est/CHOPLINES + 4 + 8*6 + 64 /*slop*/ ;
61
        est += strlen(afs_sig) + est/CHOPLINES + 4 + 8*6 + 64 /*slop*/ ;
62
 
62
 
63
        PIUNLOCKHANDLE(h); // should not be necessary
63
        PIUNLOCKHANDLE(h); // should not be necessary
64
        if( !(e = PISETHANDLESIZE(h,(int32)(est))) && (p = start = PILOCKHANDLE(h,false)) ){
64
        if( !(e = PISETHANDLESIZE(h,(int32)(est))) && (p = start = PILOCKHANDLE(h,false)) ){
65
                // build one long string in AFS format
65
                // build one long string in AFS format
66
                p = cat(p,afs_sig); // first the header signature
66
                p = cat(p,afs_sig); // first the header signature
67
 
67
 
68
                /* then slider values, one per line */
68
                /* then slider values, one per line */
69
                for( i=0 ; i<8 ; ++i )
69
                for( i=0 ; i<8 ; ++i )
70
                        p += sprintf(p, "%d\r", slider[i]);
70
                        p += sprintf(p, "%d\r", slider[i]);
71
 
71
 
72
                /* expressions, broken into lines no longer than CHOPLINES characters */
72
                /* expressions, broken into lines no longer than CHOPLINES characters */
73
                for( i=0 ; i<4 ; ++i ){
73
                for( i=0 ; i<4 ; ++i ){
74
                        if ((r = expr[i])) {
74
                        if ((r = expr[i])) {
75
                                chunk = 0; // to avoid that compiler complains
75
                                chunk = 0; // to avoid that compiler complains
76
                                for (n = strlen(r); n; n -= chunk) {
76
                                for (n = strlen(r); n; n -= chunk) {
77
                                        chunk = n > (int)CHOPLINES ? (int)CHOPLINES : n;
77
                                        chunk = n > (int)CHOPLINES ? (int)CHOPLINES : n;
78
                                        for (j = chunk, q = outbuf; j--; )
78
                                        for (j = chunk, q = outbuf; j--; )
79
                                                if (*r == CR) {
79
                                                if (*r == CR) {
80
                                                        *q++ = '\\';
80
                                                        *q++ = '\\';
81
                                                        *q++ = 'r';
81
                                                        *q++ = 'r';
82
                                                        ++r;
82
                                                        ++r;
83
                                                }
83
                                                }
84
                                                else if (*r == LF) {
84
                                                else if (*r == LF) {
85
 
85
 
86
                                                        // This can only happen with Windows or Linux.
86
                                                        // This can only happen with Windows or Linux.
87
                                                        // Native Linux is not supported, and Windows always combines LF with CR. So we can ignore LF.
87
                                                        // Native Linux is not supported, and Windows always combines LF with CR. So we can ignore LF.
88
                                                        ++r;
88
                                                        ++r;
89
                                                }
89
                                                }
90
                                                else
90
                                                else
91
                                                        *q++ = *r++;
91
                                                        *q++ = *r++;
92
                                        *q++ = CR;
92
                                        *q++ = CR;
93
                                        *q = 0;
93
                                        *q = 0;
94
                                        p = cat(p, outbuf);
94
                                        p = cat(p, outbuf);
95
                                }
95
                                }
96
                        }
96
                        }
97
                        else
97
                        else
98
                                p = cat(p,(char*)("(null expr)\r")); // this shouldn't happen
98
                                p = cat(p,(char*)("(null expr)\r")); // this shouldn't happen
99
                        *p++ = CR;
99
                        *p++ = CR;
100
                }
100
                }
101
 
101
 
102
//              *p = 0; dbg(start);
102
//              *p = 0; dbg(start);
103
 
103
 
104
                PIUNLOCKHANDLE(h);
104
                PIUNLOCKHANDLE(h);
105
                e = PISETHANDLESIZE(h,(int32)(p - start)); // could ignore this error, maybe
105
                e = PISETHANDLESIZE(h,(int32)(p - start)); // could ignore this error, maybe
106
        }
106
        }
107
 
107
 
108
        return e;
108
        return e;
109
}
109
}
110
 
110
 
111
OSErr saveparams_picotxt(Handle h, Boolean useparm) {
111
OSErr saveparams_picotxt(Handle h, Boolean useparm) {
112
        extern int ctls[], maps[];
112
        extern int ctls[], maps[];
113
 
113
 
114
        char * p, *start;
114
        char * p, *start;
115
        int i;
115
        int i;
116
        OSErr e;
116
        OSErr e;
117
        size_t est;
117
        size_t est;
118
 
118
 
119
        if (!h) return nilHandleErr;
119
        if (!h) return nilHandleErr;
120
 
120
 
121
        est = strlen(expr[0]) + strlen(expr[1]) + strlen(expr[2]) + strlen(expr[3]);
121
        est = strlen(expr[0]) + strlen(expr[1]) + strlen(expr[2]) + strlen(expr[3]);
122
        // do not be tempted to combine into one expression: 'est' is referenced below
122
        // do not be tempted to combine into one expression: 'est' is referenced below
123
        est += 16000;
123
        est += 16000;
124
 
124
 
125
        PIUNLOCKHANDLE(h); // should not be necessary
125
        PIUNLOCKHANDLE(h); // should not be necessary
126
        if (!(e = PISETHANDLESIZE(h, (int32)(est))) && (p = start = PILOCKHANDLE(h, false))) {
126
        if (!(e = PISETHANDLESIZE(h, (int32)(est))) && (p = start = PILOCKHANDLE(h, false))) {
127
                checksliders(4, ctls, maps);
127
                checksliders(4, ctls, maps);
128
 
128
 
129
                // Metadata
129
                // Metadata
130
                p += sprintf(p, "Category: %s\r\n", useparm ? gdata->parm.szCategory : "...");
130
                p += sprintf(p, "Category: %s\r\n", useparm ? gdata->parm.szCategory : "...");
131
                p += sprintf(p, "Title: %s\r\n", useparm ? gdata->parm.szTitle : "...");
131
                p += sprintf(p, "Title: %s\r\n", useparm ? gdata->parm.szTitle : "...");
132
                p += sprintf(p, "Copyright: %s\r\n", useparm ? gdata->parm.szCopyright : "...");
132
                p += sprintf(p, "Copyright: %s\r\n", useparm ? gdata->parm.szCopyright : "...");
133
                p += sprintf(p, "Author: %s\r\n", useparm ? gdata->parm.szAuthor : "...");
133
                p += sprintf(p, "Author: %s\r\n", useparm ? gdata->parm.szAuthor : "...");
134
                p += sprintf(p, "Filename: %s\r\n", useparm ? "Untitled.8bf" : "Untitled.8bf"); // TODO: get .txt filename and change .txt to .8bf
134
                p += sprintf(p, "Filename: %s\r\n", useparm ? "Untitled.8bf" : "Untitled.8bf"); // TODO: get .txt filename and change .txt to .8bf
135
                p += sprintf(p, "\r\n");
135
                p += sprintf(p, "\r\n");
136
                p += sprintf(p, "R: %s\r\n", useparm ? gdata->parm.szFormula[0] : expr[0]);
136
                p += sprintf(p, "R: %s\r\n", useparm ? gdata->parm.szFormula[0] : expr[0]);
137
                p += sprintf(p, "\r\n");
137
                p += sprintf(p, "\r\n");
138
                p += sprintf(p, "G: %s\r\n", useparm ? gdata->parm.szFormula[1] : expr[1]);
138
                p += sprintf(p, "G: %s\r\n", useparm ? gdata->parm.szFormula[1] : expr[1]);
139
                p += sprintf(p, "\r\n");
139
                p += sprintf(p, "\r\n");
140
                p += sprintf(p, "B: %s\r\n", useparm ? gdata->parm.szFormula[2] : expr[2]);
140
                p += sprintf(p, "B: %s\r\n", useparm ? gdata->parm.szFormula[2] : expr[2]);
141
                p += sprintf(p, "\r\n");
141
                p += sprintf(p, "\r\n");
142
                p += sprintf(p, "A: %s\r\n", useparm ? gdata->parm.szFormula[3] : expr[3]);
142
                p += sprintf(p, "A: %s\r\n", useparm ? gdata->parm.szFormula[3] : expr[3]);
143
                p += sprintf(p, "\r\n");
143
                p += sprintf(p, "\r\n");
144
                if (useparm) {
144
                if (useparm) {
145
                        for (i = 0; i < 8; i++) {
145
                        for (i = 0; i < 8; i++) {
146
                                if (gdata->parm.ctl_used[i]) {
146
                                if (gdata->parm.ctl_used[i]) {
147
                                        p += sprintf(p, "ctl[%d]: %s\r\n", i, gdata->parm.szCtl[i]);
147
                                        p += sprintf(p, "ctl[%d]: %s\r\n", i, gdata->parm.szCtl[i]);
148
                                }
148
                                }
149
                        }
149
                        }
150
                        for (i = 0; i < 4; i++) {
150
                        for (i = 0; i < 4; i++) {
151
                                if (gdata->parm.map_used[i]) {
151
                                if (gdata->parm.map_used[i]) {
152
                                        p += sprintf(p, "map[%d]: %s\r\n", i, gdata->parm.szMap[i]);
152
                                        p += sprintf(p, "map[%d]: %s\r\n", i, gdata->parm.szMap[i]);
153
                                }
153
                                }
154
                        }
154
                        }
155
                        p += sprintf(p, "\r\n");
155
                        p += sprintf(p, "\r\n");
156
                        for (i = 0; i < 8; i++) {
156
                        for (i = 0; i < 8; i++) {
157
                                if (gdata->parm.ctl_used[i]) {
157
                                if (gdata->parm.ctl_used[i]) {
158
                                        p += sprintf(p, "val[%d]: %d\r\n", i, gdata->parm.val[i]);
158
                                        p += sprintf(p, "val[%d]: %d\r\n", i, gdata->parm.val[i]);
159
                                }
159
                                }
160
                        }
160
                        }
161
                        /*
161
                        /*
162
                        p += sprintf(p, "\r\n");
162
                        p += sprintf(p, "\r\n");
163
                        for (i = 0; i < 8; i++) {
163
                        for (i = 0; i < 8; i++) {
164
                                if (gdata->parm.ctl_used[i]) {
164
                                if (gdata->parm.ctl_used[i]) {
165
                                        p += sprintf(p, "def[%d]: %d\r\n", i, gdata->parm.val[i]);
165
                                        p += sprintf(p, "def[%d]: %d\r\n", i, gdata->parm.val[i]);
166
                                }
166
                                }
167
                        }
167
                        }
168
                        */
168
                        */
169
                }
169
                }
170
                else {
170
                else {
171
                        for (i = 0; i < 8; i++) {
171
                        for (i = 0; i < 8; i++) {
172
                                if (ctls[i]) {
172
                                if (ctls[i]) {
173
                                        p += sprintf(p, "ctl[%d]: %s\r\n", i, "...");
173
                                        p += sprintf(p, "ctl[%d]: %s\r\n", i, "...");
174
                                }
174
                                }
175
                        }
175
                        }
176
                        for (i = 0; i < 4; i++) {
176
                        for (i = 0; i < 4; i++) {
177
                                if (maps[i]) {
177
                                if (maps[i]) {
178
                                        p += sprintf(p, "map[%d]: %s\r\n", i, "...");
178
                                        p += sprintf(p, "map[%d]: %s\r\n", i, "...");
179
                                }
179
                                }
180
                        }
180
                        }
181
                        p += sprintf(p, "\r\n");
181
                        p += sprintf(p, "\r\n");
182
                        for (i = 0; i < 8; i++) {
182
                        for (i = 0; i < 8; i++) {
183
                                if (ctls[i]) {
183
                                if (ctls[i]) {
184
                                        p += sprintf(p, "val[%d]: %d\r\n", i, slider[i]);
184
                                        p += sprintf(p, "val[%d]: %d\r\n", i, slider[i]);
185
                                }
185
                                }
186
                        }
186
                        }
187
                        /*
187
                        /*
188
                        p += sprintf(p, "\r\n");
188
                        p += sprintf(p, "\r\n");
189
                        for (i = 0; i < 8; i++) {
189
                        for (i = 0; i < 8; i++) {
190
                                if (ctls[i]) {
190
                                if (ctls[i]) {
191
                                        p += sprintf(p, "def[%d]: %s\r\n", i, "...");
191
                                        p += sprintf(p, "def[%d]: %s\r\n", i, "...");
192
                                }
192
                                }
193
                        }
193
                        }
194
                        */
194
                        */
195
                }
195
                }
196
 
196
 
197
                PIUNLOCKHANDLE(h);
197
                PIUNLOCKHANDLE(h);
198
                e = PISETHANDLESIZE(h, (int32)(p - start)); // could ignore this error, maybe
198
                e = PISETHANDLESIZE(h, (int32)(p - start)); // could ignore this error, maybe
199
        }
199
        }
200
 
200
 
201
        return e;
201
        return e;
202
}
202
}
203
 
203
 
204
OSErr savehandleintofile(Handle h,FILEREF r){
204
OSErr savehandleintofile(Handle h,FILEREF r){
205
        Ptr p;
205
        Ptr p;
206
        FILECOUNT n;
206
        FILECOUNT n;
207
        OSErr e;
207
        OSErr e;
208
 
208
 
209
        if (!h) return nilHandleErr;
209
        if (!h) return nilHandleErr;
210
        p = PILOCKHANDLE(h,false);
210
        p = PILOCKHANDLE(h,false);
211
        n = (FILECOUNT)PIGETHANDLESIZE(h);
211
        n = (FILECOUNT)PIGETHANDLESIZE(h);
212
        e = FSWrite(r,&n,p);
212
        e = FSWrite(r,&n,p);
213
        PIUNLOCKHANDLE(h);
213
        PIUNLOCKHANDLE(h);
214
        return e;
214
        return e;
215
}
215
}
216
 
216
 
217
Boolean savefile_afs_pff_picotxt(StandardFileReply *sfr){
217
Boolean savefile_afs_pff_picotxt(StandardFileReply *sfr){
218
        FILEREF r;
218
        FILEREF r;
219
        Handle h;
219
        Handle h;
220
        Boolean res = false;
220
        Boolean res = false;
221
        TCHAR* reasonstr = NULL;
221
        TCHAR* reasonstr = NULL;
222
 
222
 
223
        FSpDelete(&sfr->sfFile);
223
        FSpDelete(&sfr->sfFile);
224
        if(FSpCreate(&sfr->sfFile,SIG_SIMPLETEXT,TEXT_FILETYPE,sfr->sfScript) == noErr)
224
        if(FSpCreate(&sfr->sfFile,SIG_SIMPLETEXT,TEXT_FILETYPE,sfr->sfScript) == noErr)
225
                if(FSpOpenDF(&sfr->sfFile,fsWrPerm,&r) == noErr){
225
                if(FSpOpenDF(&sfr->sfFile,fsWrPerm,&r) == noErr){
226
 
226
 
227
                        if (fileHasExtension(sfr, TEXT(".txt"))) {
227
                        if (fileHasExtension(sfr, TEXT(".txt"))) {
228
                                // PluginCommander .txt
228
                                // PluginCommander .txt
229
                                if ((h = PINEWHANDLE(1))) { // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that.
229
                                if ((h = PINEWHANDLE(1))) { // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that.
230
                                        res = !(saveparams_picotxt(h,false) || savehandleintofile(h, r));
230
                                        res = !(saveparams_picotxt(h,false) || savehandleintofile(h, r));
231
                                        PIDISPOSEHANDLE(h);
231
                                        PIDISPOSEHANDLE(h);
232
                                }
232
                                }
233
                        }
233
                        }
234
 
234
 
235
                        if ((fileHasExtension(sfr, TEXT(".afs"))) || (fileHasExtension(sfr, TEXT(".pff")))) {
235
                        if ((fileHasExtension(sfr, TEXT(".afs"))) || (fileHasExtension(sfr, TEXT(".pff")))) {
236
                                if (fileHasExtension(sfr, TEXT(".pff"))) {
236
                                if (fileHasExtension(sfr, TEXT(".pff"))) {
237
                                        // If it is a Premiere settings file, we need to swap the channels red and blue
237
                                        // If it is a Premiere settings file, we need to swap the channels red and blue
238
                                        // We just swap the pointers!
238
                                        // We just swap the pointers!
239
                                        char* tmp;
239
                                        char* tmp;
240
                                        tmp = expr[0];
240
                                        tmp = expr[0];
241
                                        expr[0] = expr[2];
241
                                        expr[0] = expr[2];
242
                                        expr[2] = tmp;
242
                                        expr[2] = tmp;
243
                                }
243
                                }
244
 
244
 
245
                                if ((h = PINEWHANDLE(1))) { // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that.
245
                                if ((h = PINEWHANDLE(1))) { // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that.
246
                                        res = !(saveparams_afs_pff(h) || savehandleintofile(h, r));
246
                                        res = !(saveparams_afs_pff(h) || savehandleintofile(h, r));
247
                                        PIDISPOSEHANDLE(h);
247
                                        PIDISPOSEHANDLE(h);
248
                                }
248
                                }
249
 
249
 
250
                                if (fileHasExtension(sfr, TEXT(".pff"))) {
250
                                if (fileHasExtension(sfr, TEXT(".pff"))) {
251
                                        // Swap back so that the other program stuff will work normally again
251
                                        // Swap back so that the other program stuff will work normally again
252
                                        char* tmp;
252
                                        char* tmp;
253
                                        tmp = expr[0];
253
                                        tmp = expr[0];
254
                                        expr[0] = expr[2];
254
                                        expr[0] = expr[2];
255
                                        expr[2] = tmp;
255
                                        expr[2] = tmp;
256
                                }
256
                                }
257
                        }
257
                        }
258
 
258
 
259
                        FSClose(r);
259
                        FSClose(r);
260
                }else reasonstr = FF_GetMsg_Cpy(MSG_CANNOT_OPEN_FILE_ID);
260
                }else reasonstr = FF_GetMsg_Cpy(MSG_CANNOT_OPEN_FILE_ID);
261
        else reasonstr = FF_GetMsg_Cpy(MSG_CANNOT_CREATE_FILE_ID);
261
        else reasonstr = FF_GetMsg_Cpy(MSG_CANNOT_CREATE_FILE_ID);
262
 
262
 
263
        if (!res) {
263
        if (!res) {
264
                alertuser_id(MSG_CANNOT_SAVE_SETTINGS_ID, reasonstr);
264
                alertuser_id(MSG_CANNOT_SAVE_SETTINGS_ID, reasonstr);
265
        }
265
        }
266
 
266
 
267
        if (reasonstr) FF_GetMsg_Free(reasonstr);
267
        if (reasonstr) FF_GetMsg_Free(reasonstr);
268
 
268
 
269
        return res;
269
        return res;
270
}
270
}
271
 
271