Subversion Repositories filter_foundry

Rev

Rev 131 | Rev 146 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 toby 1
/*
18 toby 2
    This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
85 toby 3
    Copyright (C) 2003-7 Toby Thain, toby@telegraphics.com.au
2 toby 4
 
5
    This program is free software; you can redistribute it and/or modify
131 dmarschall 6
    it under the terms of the GNU General Public License as published by
2 toby 7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
9
 
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
 
131 dmarschall 15
    You should have received a copy of the GNU General Public License
2 toby 16
    along with this program; if not, write to the Free Software
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
*/
19
 
20
#include "ff.h"
21
 
22
#include "file_compat.h"
23
 
24
enum{
85 toby 25
        BUFSIZE = 4L<<10,
2 toby 26
        MAXLINE = 0x200,
27
};
28
 
29
Boolean readparams(Handle h,Boolean alerts,char **reason){
30
        Boolean res = false;
31
        char linebuf[MAXLINE+1],curexpr[MAXEXPR+1],*p,*dataend,*q;
32
        int c,linecnt,lineptr,exprcnt;
33
 
15 toby 34
        if(!h){
35
                *reason = "readparams: Null parameter handle.";
36
                return false;
37
        }
38
 
2 toby 39
        p = PILOCKHANDLE(h,false);
40
        dataend = p + PIGETHANDLESIZE(h);
131 dmarschall 41
 
2 toby 42
        q = curexpr;
43
        linecnt = exprcnt = lineptr = 0;
44
 
45
        *reason = "File was too short.";
85 toby 46
        while(p < dataend){
2 toby 47
 
48
                c = *p++;
49
 
50
                if(c==CR || c==LF){ /* detected end of line */
131 dmarschall 51
 
2 toby 52
                        /* look ahead to see if we need to skip a line feed (DOS EOL convention) */
131 dmarschall 53
                        if(c == CR && *p == LF && p < dataend)
2 toby 54
                                ++p;
131 dmarschall 55
 
2 toby 56
                        linebuf[lineptr] = 0; /* add terminating NUL to line buffer */
57
 
58
                        /* process complete line */
59
                        if(linecnt==0){
60
                                if(strcmp(linebuf,"%RGB-1.0")){
61
                                        if(alerts)
85 toby 62
                                                *reason = "This doesn't look like a Filter Factory file (first line is not \"%RGB-1.0\").";
2 toby 63
                                        break;
64
                                }
65
                        }else if(linecnt<=8){
66
                                slider[linecnt-1] = atoi(linebuf);
67
                        }else{
131 dmarschall 68
                                if(lineptr){
2 toby 69
                                        /* it's not an empty line; append it to current expr string */
70
                                        if( q+lineptr > curexpr+MAXEXPR ){
85 toby 71
                                                *reason = "Found an expression longer than 1024 characters.";
2 toby 72
                                                break;
73
                                        }
74
                                        q = cat(q,linebuf);
75
                                }else{
76
                                        /* it's an empty line: we've completed the expr string */
131 dmarschall 77
                                        if(expr[exprcnt])
2 toby 78
                                                free(expr[exprcnt]);
79
                                        *q = 0;
80
                                        if(!(expr[exprcnt] = my_strdup(curexpr))){
85 toby 81
                                                *reason = "Could not get memory for expression.";
2 toby 82
                                                break;
83
                                        }
84
 
85
                                        if(++exprcnt == 4){
86
                                                res = true;
87
                                                break; /* got everything we want */
88
                                        }
89
 
90
                                        q = curexpr; /* empty current expr, ready for next one */
91
                                }
92
                        }
131 dmarschall 93
 
2 toby 94
                        ++linecnt;
95
                        lineptr = 0;
96
                }else{
97
                        /* store character */
98
                        if(c=='\\'){ /* escape sequence */
99
                                if(p < dataend){
100
                                        c = *p++;
101
                                        switch(c){
102
                                        case 'r': c = CR;
103
                                        case '\\': break;
131 dmarschall 104
                                        default:
2 toby 105
                                                if(alerts) alertuser("Warning:","Unknown escape sequence in input.");
106
                                        }
131 dmarschall 107
                                }//else if(alerts) alertuser("Warning:","truncated escape sequence ends input");
2 toby 108
                        }
109
 
110
                        if(lineptr < MAXLINE)
111
                                linebuf[lineptr++] = c;
112
                }
113
        }
131 dmarschall 114
 
2 toby 115
        PIUNLOCKHANDLE(h);
116
 
117
        return res;
118
}
119
 
145 dmarschall 120
void convert_premiere_to_photoshop(PARM_T* photoshop, PARM_T_PREMIERE* premiere) {
121
        int i;
122
 
123
        photoshop->cbSize = sizeof(PARM_T);
124
        photoshop->standalone = premiere->standalone;
125
        for (i=0;i<8;++i)
126
          photoshop->val[i] = premiere->val[i];
127
        photoshop->popDialog = premiere->popDialog;
128
        photoshop->unknown1 = premiere->unknown1;
129
        photoshop->unknown2 = premiere->unknown2;
130
        photoshop->unknown3 = premiere->unknown3;
131
        for (i=0;i<4;++i)
132
          photoshop->map_used[i] = premiere->map_used[i];
133
        for (i=0;i<8;++i)
134
          photoshop->ctl_used[i] = premiere->ctl_used[i];
135
        memcpy((void*)photoshop->category, (void*)premiere->category, sizeof(photoshop->category));
136
        photoshop->iProtected = premiere->iProtected;
137
        memcpy((void*)photoshop->title, (void*)premiere->category, sizeof(photoshop->title));
138
        memcpy((void*)photoshop->copyright, (void*)premiere->category, sizeof(photoshop->copyright));
139
        memcpy((void*)photoshop->author, (void*)premiere->category, sizeof(photoshop->author));
140
        for (i=0;i<4;++i)
141
          memcpy((void*)photoshop->map[i], (void*)premiere->map[i], sizeof(photoshop->map[i]));
142
        for (i=0;i<8;++i)
143
          memcpy((void*)photoshop->ctl[i], (void*)premiere->ctl[i], sizeof(photoshop->ctl[i]));
144
 
145
        if (premiere->singleExpression) {
146
                memcpy((void*)photoshop->formula[0], (void*)premiere->formula[3], sizeof(photoshop->formula[3]));
147
                memcpy((void*)photoshop->formula[1], (void*)premiere->formula[3], sizeof(photoshop->formula[3]));
148
                memcpy((void*)photoshop->formula[2], (void*)premiere->formula[3], sizeof(photoshop->formula[3]));
149
                photoshop->formula[3][0] = '2';
150
                photoshop->formula[3][1] = '5';
151
                photoshop->formula[3][2] = '5';
152
                photoshop->formula[3][3] = '\0';
153
        } else {
154
                memcpy((void*)photoshop->formula[0], (void*)premiere->formula[2], sizeof(photoshop->formula[2]));
155
                memcpy((void*)photoshop->formula[1], (void*)premiere->formula[1], sizeof(photoshop->formula[1]));
156
                memcpy((void*)photoshop->formula[2], (void*)premiere->formula[0], sizeof(photoshop->formula[0]));
157
                memcpy((void*)photoshop->formula[3], (void*)premiere->formula[3], sizeof(photoshop->formula[3]));
158
        }
159
}
160
 
45 toby 161
Boolean readPARM(Ptr p,PARM_T *pparm,char **reasonstr,int fromwin){
2 toby 162
        int i;
163
 
145 dmarschall 164
        if (*((unsigned int*)p) == PARM_SIZE_PREMIERE) {
165
                convert_premiere_to_photoshop(pparm, (PARM_T_PREMIERE*)p);
166
        } else {
167
                // Assume it is Photoshop. Signature either PARM_SIZE (0x2068) or 0x1C68
168
                memcpy(pparm,p,sizeof(PARM_T));
169
        }
2 toby 170
 
45 toby 171
        if(fromwin){
172
                /* Windows PARM resource stores C strings - convert to Pascal strings  */
173
                myc2pstr((char*)pparm->category);
174
                myc2pstr((char*)pparm->title);
175
                myc2pstr((char*)pparm->copyright);
176
                myc2pstr((char*)pparm->author);
85 toby 177
                for(i = 0; i < 4; ++i)
45 toby 178
                        myc2pstr((char*)pparm->map[i]);
85 toby 179
                for(i = 0; i < 8; ++i)
45 toby 180
                        myc2pstr((char*)pparm->ctl[i]);
181
        }
182
 
85 toby 183
        for(i = 0; i < 4; ++i){
2 toby 184
                if(expr[i]) free(expr[i]);
185
                expr[i] = my_strdup(pparm->formula[i]);
186
        }
187
 
85 toby 188
        for(i = 0; i < 8; ++i)
2 toby 189
                slider[i] = pparm->val[i];
190
 
45 toby 191
        return true;
2 toby 192
}
193
 
194
Handle readfileintohandle(FILEREF r){
195
        long n;
196
        Handle h;
197
        Ptr p;
198
 
199
        if( !GetEOF(r,&n) && (h = PINEWHANDLE(n)) ){
200
                p = PILOCKHANDLE(h,false);
45 toby 201
                if(!SetFPos(r,fsFromStart,0) && !FSRead(r,&n,p)){
2 toby 202
                        PIUNLOCKHANDLE(h);
203
                        return h;
204
                }
205
                PIDISPOSEHANDLE(h);
206
        }
207
        return NULL;
208
}
209
 
210
Boolean readfile(StandardFileReply *sfr,char **reason){
211
        FILEREF r;
212
        Handle h;
213
        Boolean res = false;
214
 
215
        if(!FSpOpenDF(&sfr->sfFile,fsRdPerm,&r)){
23 toby 216
                if( (h = readfileintohandle(r)) ){
217
                        if( (res = readparams(h,true,reason)) )
2 toby 218
                                gdata->standalone = false; // so metadata fields will default, if user chooses Make...
145 dmarschall 219
 
220
                        if (!strcasecmp((char*)sfr->sfFile.name + 1 + sfr->nFileExtension,"pff")) {
221
                                char* tmp;
222
                                tmp = my_strdup(expr[0]);
223
                                memcpy((void*)expr[0], (void*)expr[2], sizeof(expr[0]));
224
                                memcpy((void*)expr[2], (void*)tmp, sizeof(expr[2]));
225
                                free(tmp);
226
                        }
227
 
2 toby 228
                        PIDISPOSEHANDLE(h);
229
                }
230
                FSClose(r);
231
        }else
85 toby 232
                *reason = "Could not open the file.";
2 toby 233
 
234
        return res;
235
}