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