Rev 366 | Rev 368 | 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 | |||
41 | Boolean readparams(Handle h,Boolean alerts,char **reason){ |
||
42 | Boolean res = false; |
||
43 | char linebuf[MAXLINE+1],curexpr[MAXEXPR+1],*p,*dataend,*q; |
||
44 | char c; |
||
45 | int linecnt, lineptr, exprcnt; |
||
46 | |||
47 | if(!h){ |
||
48 | *reason = _strdup("readparams: Null parameter handle."); |
||
49 | return false; |
||
50 | } |
||
51 | |||
52 | p = PILOCKHANDLE(h,false); |
||
53 | dataend = p + PIGETHANDLESIZE(h); |
||
54 | |||
55 | q = curexpr; |
||
56 | linecnt = exprcnt = lineptr = 0; |
||
57 | |||
58 | *reason = _strdup("File was too short."); |
||
59 | while(p < dataend){ |
||
60 | |||
61 | c = *p++; |
||
62 | |||
63 | if(c==CR || c==LF){ /* detected end of line */ |
||
64 | |||
65 | /* look ahead to see if we need to skip a line feed (DOS EOL convention) */ |
||
66 | if(c == CR && *p == LF && p < dataend) |
||
67 | ++p; |
||
68 | |||
69 | linebuf[lineptr] = 0; /* add terminating NUL to line buffer */ |
||
70 | |||
71 | /* process complete line */ |
||
72 | if(linecnt==0){ |
||
73 | if(strcmp(linebuf,"%RGB-1.0")){ |
||
74 | if(alerts) |
||
75 | *reason = _strdup("This doesn't look like a Filter Factory file (first line is not \"%RGB-1.0\")."); |
||
76 | break; |
||
77 | } |
||
78 | }else if(linecnt<=8){ |
||
79 | slider[linecnt-1] = atoi(linebuf); |
||
80 | }else{ |
||
81 | if(lineptr){ |
||
82 | /* it's not an empty line; append it to current expr string */ |
||
83 | if( q+lineptr > curexpr+MAXEXPR ){ |
||
84 | *reason = _strdup("Found an expression longer than 1024 characters."); |
||
85 | break; |
||
86 | } |
||
87 | q = cat(q,linebuf); |
||
88 | }else{ |
||
89 | /* it's an empty line: we've completed the expr string */ |
||
90 | if(expr[exprcnt]) |
||
91 | free(expr[exprcnt]); |
||
92 | *q = 0; |
||
93 | if(!(expr[exprcnt] = my_strdup(curexpr))){ |
||
94 | *reason = _strdup("Could not get memory for expression."); |
||
95 | break; |
||
96 | } |
||
97 | |||
98 | if(++exprcnt == 4){ |
||
99 | res = true; |
||
100 | break; /* got everything we want */ |
||
101 | } |
||
102 | |||
103 | q = curexpr; /* empty current expr, ready for next one */ |
||
104 | } |
||
105 | } |
||
106 | |||
107 | ++linecnt; |
||
108 | lineptr = 0; |
||
109 | }else{ |
||
110 | /* store character */ |
||
111 | if(c=='\\'){ /* escape sequence */ |
||
112 | if(p < dataend){ |
||
113 | c = *p++; |
||
114 | switch(c){ |
||
115 | case 'r': |
||
268 | daniel-mar | 116 | #if WIN_ENV |
117 | c = CR; |
||
118 | if (lineptr < MAXLINE) |
||
274 | daniel-mar | 119 | linebuf[lineptr++] = c; |
268 | daniel-mar | 120 | c = LF; |
121 | #else |
||
122 | c = CR; |
||
123 | #endif |
||
124 | break; |
||
256 | daniel-mar | 125 | case '\\': break; |
126 | default: |
||
127 | if(alerts) alertuser(_strdup("Warning:"),_strdup("Unknown escape sequence in input.")); |
||
128 | } |
||
129 | }//else if(alerts) alertuser(_strdup("Warning:"),_strdup("truncated escape sequence ends input")); |
||
130 | } |
||
131 | |||
132 | if(lineptr < MAXLINE) |
||
133 | linebuf[lineptr++] = c; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | PIUNLOCKHANDLE(h); |
||
138 | |||
139 | if (res) *reason = NULL; // no error |
||
140 | return res; |
||
141 | } |
||
142 | |||
143 | void convert_premiere_to_photoshop(PARM_T* photoshop, PARM_T_PREMIERE* premiere) { |
||
144 | int i; |
||
145 | |||
146 | photoshop->cbSize = sizeof(PARM_T); |
||
147 | photoshop->standalone = premiere->standalone; |
||
148 | for (i=0;i<8;++i) |
||
272 | daniel-mar | 149 | photoshop->val[i] = premiere->val[i]; |
256 | daniel-mar | 150 | photoshop->popDialog = premiere->popDialog; |
151 | photoshop->unknown1 = premiere->unknown1; |
||
152 | photoshop->unknown2 = premiere->unknown2; |
||
153 | photoshop->unknown3 = premiere->unknown3; |
||
154 | for (i=0;i<4;++i) |
||
272 | daniel-mar | 155 | photoshop->map_used[i] = premiere->map_used[i]; |
256 | daniel-mar | 156 | for (i=0;i<8;++i) |
272 | daniel-mar | 157 | photoshop->ctl_used[i] = premiere->ctl_used[i]; |
256 | daniel-mar | 158 | sprintf((char*)photoshop->category, "Filter Factory"); // Premiere plugins do not have a category attribute |
159 | photoshop->iProtected = 0; // Premiere plugins do not have a protect flag |
||
160 | memcpy((void*)photoshop->title, (void*)premiere->title, sizeof(photoshop->title)); |
||
161 | memcpy((void*)photoshop->copyright, (void*)premiere->copyright, sizeof(photoshop->copyright)); |
||
162 | memcpy((void*)photoshop->author, (void*)premiere->author, sizeof(photoshop->author)); |
||
163 | for (i=0;i<4;++i) |
||
272 | daniel-mar | 164 | memcpy((void*)photoshop->map[i], (void*)premiere->map[i], sizeof(photoshop->map[i])); |
256 | daniel-mar | 165 | for (i=0;i<8;++i) |
272 | daniel-mar | 166 | memcpy((void*)photoshop->ctl[i], (void*)premiere->ctl[i], sizeof(photoshop->ctl[i])); |
256 | daniel-mar | 167 | |
168 | if (premiere->singleExpression) { |
||
169 | memcpy((void*)photoshop->formula[0], (void*)premiere->formula[3], sizeof(photoshop->formula[3])); |
||
170 | memcpy((void*)photoshop->formula[1], (void*)premiere->formula[3], sizeof(photoshop->formula[3])); |
||
171 | memcpy((void*)photoshop->formula[2], (void*)premiere->formula[3], sizeof(photoshop->formula[3])); |
||
172 | memcpy((void*)photoshop->formula[3], (void*)premiere->formula[3], sizeof(photoshop->formula[3])); |
||
173 | } else { |
||
174 | memcpy((void*)photoshop->formula[0], (void*)premiere->formula[2], sizeof(photoshop->formula[2])); |
||
175 | memcpy((void*)photoshop->formula[1], (void*)premiere->formula[1], sizeof(photoshop->formula[1])); |
||
176 | memcpy((void*)photoshop->formula[2], (void*)premiere->formula[0], sizeof(photoshop->formula[0])); |
||
177 | memcpy((void*)photoshop->formula[3], (void*)premiere->formula[3], sizeof(photoshop->formula[3])); |
||
178 | } |
||
179 | } |
||
180 | |||
366 | daniel-mar | 181 | char* _ffx_read_str(char** q) { |
182 | uint32_t len; |
||
183 | char* val; |
||
184 | |||
185 | len = *((uint32_t*)*q); |
||
186 | *q += sizeof(uint32_t); |
||
187 | val = (char*)malloc(len + 1); |
||
188 | if (val != NULL) { |
||
189 | memcpy(val, (char*)*q, len); |
||
190 | val[len] = 0; |
||
191 | } |
||
192 | *q += len; |
||
193 | return val; |
||
194 | } |
||
195 | |||
196 | Boolean readfile_ffx(StandardFileReply* sfr, char** reason) { |
||
197 | Handle h; |
||
198 | Boolean res = false; |
||
199 | FILEREF refnum; |
||
200 | uint32_t len; |
||
201 | char* val; |
||
367 | daniel-mar | 202 | int format_version = -1; |
366 | daniel-mar | 203 | int i; |
204 | |||
205 | if (FSpOpenDF(&sfr->sfFile, fsRdPerm, &refnum) == noErr) { |
||
206 | if ((h = readfileintohandle(refnum))) { |
||
207 | char* q = (char*)PILOCKHANDLE(h, false); |
||
208 | |||
209 | len = *((uint32_t*)q); |
||
210 | if (len == 6) { |
||
211 | val = _ffx_read_str(&q); |
||
367 | daniel-mar | 212 | if (strcmp(val, "FFX1.0") == 0) format_version = 10; |
213 | else if (strcmp(val, "FFX1.1") == 0) format_version = 11; |
||
214 | else if (strcmp(val, "FFX1.2") == 0) format_version = 12; |
||
366 | daniel-mar | 215 | free(val); |
367 | daniel-mar | 216 | if (format_version > 0) { |
366 | daniel-mar | 217 | val = _ffx_read_str(&q); |
218 | myc2pstrcpy(gdata->parm.title, val); |
||
219 | free(val); |
||
220 | |||
221 | val = _ffx_read_str(&q); |
||
222 | myc2pstrcpy(gdata->parm.category, val); |
||
223 | free(val); |
||
224 | |||
225 | val = _ffx_read_str(&q); |
||
226 | myc2pstrcpy(gdata->parm.author, val); |
||
227 | free(val); |
||
228 | |||
229 | val = _ffx_read_str(&q); |
||
230 | myc2pstrcpy(gdata->parm.copyright, val); |
||
231 | free(val); |
||
232 | |||
233 | // Channels I, R, G, B, A |
||
234 | for (i = 0; i < 4; i++) { |
||
235 | val = _ffx_read_str(&q); |
||
236 | if (i == 0) { |
||
237 | if (strcmp(val, "0") != 0) { |
||
238 | // "Intro channel" existing |
||
239 | char* val2 = _ffx_read_str(&q); |
||
240 | strcat(val, ","); |
||
241 | strcat(val, val2); |
||
242 | free(val2); |
||
243 | } |
||
244 | else { |
||
245 | free(val); |
||
246 | val = _ffx_read_str(&q); |
||
247 | } |
||
248 | } |
||
249 | expr[i] = my_strdup(val); |
||
250 | myc2pstrcpy(gdata->parm.formula[i], val); |
||
251 | free(val); |
||
252 | } |
||
253 | |||
254 | // Sliders |
||
255 | for (i = 0; i < 8; i++) { |
||
367 | daniel-mar | 256 | char* sliderName; |
366 | daniel-mar | 257 | val = _ffx_read_str(&q); |
367 | daniel-mar | 258 | sliderName = val; |
259 | if (format_version >= 12) { |
||
260 | // Format FFX1.2 has prefixes {S} = Slider, {C} = Checkbox, none = Slider |
||
261 | if ((sliderName[0] == '{') && (sliderName[1] == 'S') && (sliderName[2] == '}')) sliderName += 3; |
||
262 | else if ((sliderName[0] == '{') && (sliderName[1] == 'C') && (sliderName[2] == '}')) sliderName += 3; |
||
263 | } |
||
264 | myc2pstrcpy(gdata->parm.ctl[i], sliderName); |
||
366 | daniel-mar | 265 | free(val); |
367 | daniel-mar | 266 | gdata->parm.ctl_used[i] = (bool32_t)*((byte*)q); |
366 | daniel-mar | 267 | q += sizeof(byte); |
268 | gdata->parm.val[i] = *((uint32_t*)q); |
||
269 | slider[i] = *((uint32_t*)q); |
||
270 | q += sizeof(uint32_t); |
||
271 | } |
||
272 | |||
273 | simplewarning(_strdup("Attention! You loaded 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.")); |
||
274 | |||
275 | res = true; |
||
276 | } |
||
277 | } |
||
278 | PIDISPOSEHANDLE(h); |
||
279 | } |
||
280 | FSClose(refnum); |
||
281 | } |
||
282 | |||
283 | if (res) gdata->obfusc = false; |
||
284 | return res; |
||
285 | } |
||
286 | |||
256 | daniel-mar | 287 | Boolean read8bfplugin(StandardFileReply *sfr,char **reason){ |
288 | unsigned char magic[2]; |
||
289 | FILECOUNT count; |
||
290 | Handle h; |
||
291 | Boolean res = false; |
||
292 | FILEREF refnum; |
||
293 | |||
294 | if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&refnum) == noErr){ |
||
295 | // check DOS EXE magic number |
||
296 | count = 2; |
||
297 | if(FSRead(refnum,&count,magic) == noErr /*&& magic[0]=='M' && magic[1]=='Z'*/){ |
||
309 | daniel-mar | 298 | 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 | 299 | if( (h = readfileintohandle(refnum)) ){ |
300 | long *q = (long*)PILOCKHANDLE(h,false); |
||
301 | |||
302 | // look for signature at start of valid PARM resource |
||
303 | // This signature is observed in Filter Factory standalones. |
||
304 | for( count /= 4 ; count >= PARM_SIZE/4 ; --count, ++q ) |
||
305 | { |
||
306 | |||
268 | daniel-mar | 307 | #ifdef MAC_ENV |
256 | daniel-mar | 308 | // Case #1: Mac is reading Windows (Win16/32/64) plugin |
309 | if( ((EndianS32_LtoN(q[0]) == PARM_SIZE) || |
||
310 | (EndianS32_LtoN(q[0]) == PARM_SIZE_PREMIERE) || |
||
311 | (EndianS32_LtoN(q[0]) == PARM_SIG_MAC)) && EndianS32_LtoN(q[1]) == 1 |
||
312 | && (res = readPARM((char*)q, &gdata->parm, reason, 1 /*Windows format resource*/)) ) |
||
313 | { |
||
314 | // these are the only numeric fields we *have* to swap |
||
315 | // all the rest are flags which (if we're careful) will work in either ordering |
||
316 | for(i = 0; i < 8; ++i) |
||
317 | slider[i] = EndianS32_LtoN(slider[i]); |
||
318 | } |
||
268 | daniel-mar | 319 | #else |
256 | daniel-mar | 320 | // Case #2: Windows is reading a Windows plugin (if Resource API failed, e.g. Win64 tries to open NE file, Win32 tries to open 64bit file) |
321 | if( ((q[0] == PARM_SIZE) || |
||
322 | (q[0] == PARM_SIZE_PREMIERE) || |
||
323 | (q[0] == PARM_SIG_MAC)) && q[1] == 1 |
||
308 | daniel-mar | 324 | && (res = readPARM((char*)q, &gdata->parm, reason, 1/*fromwin*/))) |
256 | daniel-mar | 325 | { |
326 | } |
||
327 | |||
339 | daniel-mar | 328 | // Case #3: Windows is reading an old FilterFactory Mac file |
329 | // Note: You must read the ".rsrc" resource fork, not the standalone binary! |
||
256 | daniel-mar | 330 | else if( ((EndianS32_LtoN(q[0]) == PARM_SIZE) || |
331 | (EndianS32_LtoN(q[0]) == PARM_SIZE_PREMIERE) || |
||
332 | (EndianS32_LtoN(q[0]) == PARM_SIG_MAC)) && EndianS32_LtoN(q[1]) == 1 |
||
333 | && (res = readPARM((char*)q, &gdata->parm, reason, 0 /*Strings are already PStrings*/)) ) |
||
334 | { |
||
339 | daniel-mar | 335 | // Note: slider[i] = EndianS32_LtoN(slider[i]); will be done in readPARM() |
336 | // All the rest are flags which (if we're careful) will work in either ordering |
||
337 | |||
341 | daniel-mar | 338 | // Convert '\r' in the copyright field to '\r\n'. |
339 | int i, j; |
||
339 | daniel-mar | 340 | for (i = 1; i < gdata->parm.copyright[0]; i++) { |
341 | if (gdata->parm.copyright[i] == '\r') { |
||
341 | daniel-mar | 342 | for (j = gdata->parm.copyright[0]; j>i; j--) { |
343 | gdata->parm.copyright[j+1] = gdata->parm.copyright[j]; |
||
344 | } |
||
345 | gdata->parm.copyright[0]++; |
||
346 | gdata->parm.copyright[i+1] = '\n'; |
||
339 | daniel-mar | 347 | } |
348 | } |
||
256 | daniel-mar | 349 | } |
268 | daniel-mar | 350 | #endif |
256 | daniel-mar | 351 | |
352 | if (res) break; |
||
353 | } |
||
341 | daniel-mar | 354 | |
256 | daniel-mar | 355 | PIDISPOSEHANDLE(h); |
356 | } |
||
357 | } |
||
358 | } // else no point in proceeding |
||
359 | FSClose(refnum); |
||
360 | }else |
||
361 | *reason = _strdup("Could not open file."); |
||
362 | |||
363 | if (res) gdata->obfusc = false; |
||
364 | return res; |
||
365 | } |
||
366 | |||
367 | Boolean readPARM(Ptr p,PARM_T *pparm,char **reasonstr,int fromwin){ |
||
368 | int i; |
||
369 | |||
370 | if (*((unsigned int*)p) == PARM_SIZE_PREMIERE) { |
||
371 | convert_premiere_to_photoshop(pparm, (PARM_T_PREMIERE*)p); |
||
372 | } else { |
||
373 | // Assume it is Photoshop. Signature either PARM_SIZE (0x2068) or 0x1C68 |
||
374 | memcpy(pparm,p,sizeof(PARM_T)); |
||
375 | } |
||
376 | |||
377 | if(fromwin){ |
||
378 | /* Windows PARM resource stores C strings - convert to Pascal strings */ |
||
379 | myc2pstr((char*)pparm->category); |
||
380 | myc2pstr((char*)pparm->title); |
||
381 | myc2pstr((char*)pparm->copyright); |
||
382 | myc2pstr((char*)pparm->author); |
||
383 | for(i = 0; i < 4; ++i) |
||
384 | myc2pstr((char*)pparm->map[i]); |
||
385 | for(i = 0; i < 8; ++i) |
||
386 | myc2pstr((char*)pparm->ctl[i]); |
||
387 | } |
||
388 | |||
389 | for(i = 0; i < 4; ++i){ |
||
390 | if(expr[i]) free(expr[i]); |
||
366 | daniel-mar | 391 | expr[i] = my_strdup((char*)pparm->formula[i]); |
256 | daniel-mar | 392 | } |
393 | |||
339 | daniel-mar | 394 | for (i = 0; i < 8; ++i) { |
395 | if (pparm->val[i] > 0xFF) { |
||
396 | // Wrong endianess (e.g. reading a Mac rsrc on Windows) |
||
397 | slider[i] = (uint8_t)EndianS32_LtoN(pparm->val[i]); |
||
398 | } |
||
399 | else { |
||
400 | slider[i] = (uint8_t)pparm->val[i]; |
||
401 | } |
||
402 | } |
||
256 | daniel-mar | 403 | |
404 | return true; |
||
405 | } |
||
406 | |||
407 | Handle readfileintohandle(FILEREF r){ |
||
408 | FILEPOS n; |
||
409 | Handle h; |
||
410 | Ptr p; |
||
411 | |||
412 | if( GetEOF(r,&n) == noErr && (h = PINEWHANDLE(n)) ){ |
||
413 | p = PILOCKHANDLE(h,false); |
||
414 | if(SetFPos(r,fsFromStart,0) == noErr && FSRead(r,(FILECOUNT*)&n,p) == noErr){ |
||
415 | PIUNLOCKHANDLE(h); |
||
416 | return h; |
||
417 | } |
||
418 | PIDISPOSEHANDLE(h); |
||
419 | } |
||
420 | return NULL; |
||
421 | } |
||
422 | |||
423 | Boolean fileHasExtension(StandardFileReply *sfr, const char* extension) { |
||
268 | daniel-mar | 424 | #ifdef WIN_ENV |
425 | |||
256 | daniel-mar | 426 | char name[MAX_PATH+1]; |
263 | daniel-mar | 427 | return sfr->nFileExtension && !strcasecmp(myp2cstrcpy(name,sfr->sfFile.name) + sfr->nFileExtension - 1,extension); |
268 | daniel-mar | 428 | |
429 | #else |
||
430 | |||
256 | daniel-mar | 431 | char name[1025]; // https://stackoverflow.com/questions/1295135/longest-pathname-string-in-mac-os-x-hfs |
432 | char* s = myp2cstrcpy(name,sfr->sfFile.name); |
||
433 | return strcmp(s + strlen(s) - strlen(extension), extension) == 0; |
||
268 | daniel-mar | 434 | |
435 | #endif |
||
256 | daniel-mar | 436 | } |
437 | |||
366 | daniel-mar | 438 | Boolean readfile_afs_pff(StandardFileReply *sfr,char **reason){ |
256 | daniel-mar | 439 | FILEREF r; |
440 | Handle h; |
||
441 | Boolean res = false; |
||
442 | |||
443 | if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&r) == noErr){ |
||
444 | if( (h = readfileintohandle(r)) ){ |
||
445 | if( (res = readparams(h,true,reason)) ) { |
||
446 | gdata->standalone = false; // so metadata fields will default, if user chooses Make... |
||
447 | |||
448 | if (fileHasExtension(sfr, ".pff")) { |
||
449 | // If it is a Premiere settings file, we need to swap the channels red and blue |
||
263 | daniel-mar | 450 | // We just swap the pointers! |
256 | daniel-mar | 451 | char* tmp; |
263 | daniel-mar | 452 | tmp = expr[0]; |
453 | expr[0] = expr[2]; |
||
454 | expr[2] = tmp; |
||
256 | daniel-mar | 455 | } |
456 | } |
||
457 | |||
458 | PIDISPOSEHANDLE(h); |
||
459 | } |
||
460 | FSClose(r); |
||
461 | }else |
||
462 | *reason = _strdup("Could not open the file."); |
||
463 | |||
464 | return res; |
||
465 | } |