Rev 444 | Rev 492 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
259 | 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 | #include "sprintf_tiny.h" |
||
25 | |||
26 | enum{ CHOPLINES = 63 }; |
||
27 | |||
28 | OSErr putstr(Handle h,char *s); |
||
29 | |||
30 | OSErr putstr(Handle h,char *s){ |
||
31 | Ptr p; |
||
32 | OSErr e; |
||
482 | daniel-mar | 33 | size_t size, n; |
259 | daniel-mar | 34 | |
482 | daniel-mar | 35 | if (!h) return nilHandleErr; |
36 | |||
37 | size = PIGETHANDLESIZE(h); |
||
38 | n = strlen(s); |
||
39 | |||
259 | daniel-mar | 40 | if(!(e = PISETHANDLESIZE(h,(int32)(size+n)))){ |
41 | p = PILOCKHANDLE(h,false); |
||
42 | memcpy(p+size,s,n); |
||
43 | PIUNLOCKHANDLE(h); |
||
44 | } |
||
45 | return e; |
||
46 | } |
||
47 | |||
389 | daniel-mar | 48 | OSErr saveparams_afs_pff(Handle h){ |
259 | daniel-mar | 49 | char outbuf[CHOPLINES*2+2],*q,*p,*r,*start; |
50 | size_t n, chunk, j; |
||
51 | int i; |
||
52 | OSErr e; |
||
53 | size_t est; |
||
54 | static char afs_sig[] = "%RGB-1.0\r"; |
||
55 | |||
482 | daniel-mar | 56 | if (!h) return nilHandleErr; |
259 | daniel-mar | 57 | |
58 | est = strlen(expr[0]) + strlen(expr[1]) + strlen(expr[2]) + strlen(expr[3]); |
||
59 | // do not be tempted to combine into one expression: 'est' is referenced below |
||
60 | est += strlen(afs_sig) + est/CHOPLINES + 4 + 8*6 + 64 /*slop*/ ; |
||
61 | |||
62 | PIUNLOCKHANDLE(h); // should not be necessary |
||
63 | if( !(e = PISETHANDLESIZE(h,(int32)(est))) && (p = start = PILOCKHANDLE(h,false)) ){ |
||
64 | // build one long string in AFS format |
||
65 | p = cat(p,afs_sig); // first the header signature |
||
66 | |||
67 | /* then slider values, one per line */ |
||
68 | for( i=0 ; i<8 ; ++i ) |
||
301 | daniel-mar | 69 | p += sprintf(p, "%d\r", slider[i]); |
259 | daniel-mar | 70 | |
71 | /* expressions, broken into lines no longer than CHOPLINES characters */ |
||
72 | for( i=0 ; i<4 ; ++i ){ |
||
73 | if( (r = expr[i]) ) |
||
74 | for( n = strlen(r) ; n ; n -= chunk ){ |
||
75 | chunk = n> (int)CHOPLINES ? (int)CHOPLINES : n; |
||
76 | for( j = chunk,q = outbuf ; j-- ; ) |
||
77 | if(*r == CR){ |
||
78 | *q++ = '\\'; |
||
79 | *q++ = 'r'; |
||
80 | ++r; |
||
81 | }else if (*r == LF) { |
||
393 | daniel-mar | 82 | |
259 | daniel-mar | 83 | // This can only happen with Windows or Linux. |
369 | daniel-mar | 84 | // Native Linux is not supported, and Windows always combines LF with CR. So we can ignore LF. |
259 | daniel-mar | 85 | ++r; |
86 | }else |
||
87 | *q++ = *r++; |
||
387 | daniel-mar | 88 | *q++ = CR; |
259 | daniel-mar | 89 | *q = 0; |
90 | p = cat(p,outbuf); |
||
91 | } |
||
92 | else |
||
93 | p = cat(p,_strdup("(null expr)\r")); // this shouldn't happen |
||
387 | daniel-mar | 94 | *p++ = CR; |
259 | daniel-mar | 95 | } |
96 | |||
97 | // *p = 0; dbg(start); |
||
98 | |||
99 | PIUNLOCKHANDLE(h); |
||
100 | e = PISETHANDLESIZE(h,(int32)(p - start)); // could ignore this error, maybe |
||
101 | } |
||
102 | |||
103 | return e; |
||
104 | } |
||
105 | |||
389 | daniel-mar | 106 | OSErr saveparams_picotxt(Handle h, Boolean useparm) { |
107 | extern int ctls[], maps[]; |
||
108 | |||
109 | char * p, *start; |
||
110 | int i; |
||
111 | OSErr e; |
||
112 | size_t est; |
||
113 | |||
482 | daniel-mar | 114 | if (!h) return nilHandleErr; |
389 | daniel-mar | 115 | |
116 | est = strlen(expr[0]) + strlen(expr[1]) + strlen(expr[2]) + strlen(expr[3]); |
||
117 | // do not be tempted to combine into one expression: 'est' is referenced below |
||
118 | est += 16000; |
||
119 | |||
120 | PIUNLOCKHANDLE(h); // should not be necessary |
||
121 | if (!(e = PISETHANDLESIZE(h, (int32)(est))) && (p = start = PILOCKHANDLE(h, false))) { |
||
122 | checksliders(4, ctls, maps); |
||
123 | |||
124 | // Metadata |
||
393 | daniel-mar | 125 | p += sprintf(p, "Category: %s\r\n", useparm ? gdata->parm.szCategory : "..."); |
126 | p += sprintf(p, "Title: %s\r\n", useparm ? gdata->parm.szTitle : "..."); |
||
127 | p += sprintf(p, "Copyright: %s\r\n", useparm ? gdata->parm.szCopyright : "..."); |
||
128 | p += sprintf(p, "Author: %s\r\n", useparm ? gdata->parm.szAuthor : "..."); |
||
389 | daniel-mar | 129 | p += sprintf(p, "Filename: %s\r\n", useparm ? "Untitled.8bf" : "Untitled.8bf"); // TODO: get .txt filename and change .txt to .8bf |
130 | p += sprintf(p, "\r\n"); |
||
393 | daniel-mar | 131 | p += sprintf(p, "R: %s\r\n", useparm ? gdata->parm.szFormula[0] : expr[0]); |
389 | daniel-mar | 132 | p += sprintf(p, "\r\n"); |
393 | daniel-mar | 133 | p += sprintf(p, "G: %s\r\n", useparm ? gdata->parm.szFormula[1] : expr[1]); |
389 | daniel-mar | 134 | p += sprintf(p, "\r\n"); |
393 | daniel-mar | 135 | p += sprintf(p, "B: %s\r\n", useparm ? gdata->parm.szFormula[2] : expr[2]); |
389 | daniel-mar | 136 | p += sprintf(p, "\r\n"); |
393 | daniel-mar | 137 | p += sprintf(p, "A: %s\r\n", useparm ? gdata->parm.szFormula[3] : expr[3]); |
389 | daniel-mar | 138 | p += sprintf(p, "\r\n"); |
139 | if (useparm) { |
||
140 | for (i = 0; i < 8; i++) { |
||
141 | if (gdata->parm.ctl_used[i]) { |
||
393 | daniel-mar | 142 | p += sprintf(p, "ctl[%d]: %s\r\n", i, gdata->parm.szCtl[i]); |
389 | daniel-mar | 143 | } |
144 | } |
||
145 | for (i = 0; i < 4; i++) { |
||
146 | if (gdata->parm.map_used[i]) { |
||
393 | daniel-mar | 147 | p += sprintf(p, "map[%d]: %s\r\n", i, gdata->parm.szMap[i]); |
389 | daniel-mar | 148 | } |
149 | } |
||
150 | p += sprintf(p, "\r\n"); |
||
151 | for (i = 0; i < 8; i++) { |
||
152 | if (gdata->parm.ctl_used[i]) { |
||
153 | p += sprintf(p, "val[%d]: %d\r\n", i, gdata->parm.val[i]); |
||
154 | } |
||
155 | } |
||
156 | /* |
||
157 | p += sprintf(p, "\r\n"); |
||
158 | for (i = 0; i < 8; i++) { |
||
159 | if (gdata->parm.ctl_used[i]) { |
||
160 | p += sprintf(p, "def[%d]: %d\r\n", i, gdata->parm.val[i]); |
||
161 | } |
||
162 | } |
||
163 | */ |
||
164 | } |
||
165 | else { |
||
166 | for (i = 0; i < 8; i++) { |
||
167 | if (ctls[i]) { |
||
168 | p += sprintf(p, "ctl[%d]: %s\r\n", i, "..."); |
||
169 | } |
||
170 | } |
||
171 | for (i = 0; i < 4; i++) { |
||
172 | if (maps[i]) { |
||
173 | p += sprintf(p, "map[%d]: %s\r\n", i, "..."); |
||
174 | } |
||
175 | } |
||
176 | p += sprintf(p, "\r\n"); |
||
177 | for (i = 0; i < 8; i++) { |
||
178 | if (ctls[i]) { |
||
179 | p += sprintf(p, "val[%d]: %d\r\n", i, slider[i]); |
||
180 | } |
||
181 | } |
||
182 | /* |
||
183 | p += sprintf(p, "\r\n"); |
||
184 | for (i = 0; i < 8; i++) { |
||
185 | if (ctls[i]) { |
||
186 | p += sprintf(p, "def[%d]: %s\r\n", i, "..."); |
||
187 | } |
||
188 | } |
||
189 | */ |
||
190 | } |
||
191 | |||
192 | PIUNLOCKHANDLE(h); |
||
193 | e = PISETHANDLESIZE(h, (int32)(p - start)); // could ignore this error, maybe |
||
194 | } |
||
195 | |||
196 | return e; |
||
197 | } |
||
198 | |||
259 | daniel-mar | 199 | OSErr savehandleintofile(Handle h,FILEREF r){ |
482 | daniel-mar | 200 | Ptr p; |
201 | long n; |
||
202 | OSErr e; |
||
203 | |||
204 | if (!h) return nilHandleErr; |
||
205 | p = PILOCKHANDLE(h,false); |
||
206 | n = PIGETHANDLESIZE(h); |
||
207 | e = FSWrite(r,&n,p); |
||
259 | daniel-mar | 208 | PIUNLOCKHANDLE(h); |
209 | return e; |
||
210 | } |
||
211 | |||
389 | daniel-mar | 212 | Boolean savefile_afs_pff_picotxt(StandardFileReply *sfr){ |
259 | daniel-mar | 213 | FILEREF r; |
214 | Handle h; |
||
215 | Boolean res = false; |
||
216 | char *reasonstr = _strdup(""); |
||
217 | |||
218 | FSpDelete(&sfr->sfFile); |
||
219 | if(FSpCreate(&sfr->sfFile,SIG_SIMPLETEXT,TEXT_FILETYPE,sfr->sfScript) == noErr) |
||
220 | if(FSpOpenDF(&sfr->sfFile,fsWrPerm,&r) == noErr){ |
||
221 | |||
444 | daniel-mar | 222 | if (fileHasExtension(sfr, TEXT(".txt"))) { |
389 | daniel-mar | 223 | // PluginCommander .txt |
224 | if ((h = PINEWHANDLE(1))) { // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that. |
||
225 | res = !(saveparams_picotxt(h,false) || savehandleintofile(h, r)); |
||
226 | PIDISPOSEHANDLE(h); |
||
227 | } |
||
369 | daniel-mar | 228 | } |
229 | |||
444 | daniel-mar | 230 | if ((fileHasExtension(sfr, TEXT(".afs"))) || (fileHasExtension(sfr, TEXT(".pff")))) { |
231 | if (fileHasExtension(sfr, TEXT(".pff"))) { |
||
389 | daniel-mar | 232 | // If it is a Premiere settings file, we need to swap the channels red and blue |
233 | // We just swap the pointers! |
||
234 | char* tmp; |
||
235 | tmp = expr[0]; |
||
236 | expr[0] = expr[2]; |
||
237 | expr[2] = tmp; |
||
238 | } |
||
259 | daniel-mar | 239 | |
389 | daniel-mar | 240 | if ((h = PINEWHANDLE(1))) { // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that. |
241 | res = !(saveparams_afs_pff(h) || savehandleintofile(h, r)); |
||
242 | PIDISPOSEHANDLE(h); |
||
243 | } |
||
244 | |||
444 | daniel-mar | 245 | if (fileHasExtension(sfr, TEXT(".pff"))) { |
389 | daniel-mar | 246 | // Swap back so that the other program stuff will work normally again |
247 | char* tmp; |
||
248 | tmp = expr[0]; |
||
249 | expr[0] = expr[2]; |
||
250 | expr[2] = tmp; |
||
251 | } |
||
369 | daniel-mar | 252 | } |
253 | |||
259 | daniel-mar | 254 | FSClose(r); |
444 | daniel-mar | 255 | }else reasonstr = _strdup("Could not open the file."); |
256 | else reasonstr = _strdup("Could not create the file."); |
||
259 | daniel-mar | 257 | |
444 | daniel-mar | 258 | if (!res) { |
259 | #ifdef UNICODE |
||
260 | TCHAR reasonstrW[0x300]; |
||
261 | mbstowcs(reasonstrW, reasonstr, 0x300); |
||
262 | alertuser((TCHAR*)TEXT("Could not save settings."), reasonstrW); |
||
263 | #else |
||
264 | alertuser((TCHAR*)TEXT("Could not save settings."), reasonstr); |
||
265 | #endif |
||
266 | } |
||
259 | daniel-mar | 267 | |
268 | return res; |
||
269 | } |