/
clip.c
278 lines (208 loc) · 6.49 KB
/
clip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
======================================================================
clip.c
Functions for LWO2 image references.
Ernie Wright 17 Sep 00
====================================================================== */
#include "../picointernal.h"
#include "lwo2.h"
/*
======================================================================
lwFreeClip()
Free memory used by an lwClip.
====================================================================== */
void lwFreeClip( lwClip *clip )
{
if ( clip ) {
lwListFree( clip->ifilter, (void *) lwFreePlugin );
lwListFree( clip->pfilter, (void *) lwFreePlugin );
switch ( clip->type ) {
case ID_STIL:
_pico_free( clip->source.still.name);
break;
case ID_ISEQ:
_pico_free( clip->source.seq.prefix );
_pico_free( clip->source.seq.suffix );
break;
case ID_ANIM:
_pico_free( clip->source.anim.name );
_pico_free( clip->source.anim.server );
_pico_free( clip->source.anim.data );
break;
case ID_XREF:
_pico_free( clip->source.xref.string );
break;
case ID_STCC:
_pico_free( clip->source.cycle.name );
break;
default:
break;
}
_pico_free( clip );
}
}
/*
======================================================================
lwGetClip()
Read image references from a CLIP chunk in an LWO2 file.
====================================================================== */
lwClip *lwGetClip( picoMemStream_t *fp, int cksize )
{
lwClip *clip;
lwPlugin *filt;
unsigned int id;
unsigned short sz;
int pos, rlen;
/* allocate the Clip structure */
clip = _pico_calloc( 1, sizeof( lwClip ));
if ( !clip ) goto Fail;
clip->contrast.val = 1.0f;
clip->brightness.val = 1.0f;
clip->saturation.val = 1.0f;
clip->gamma.val = 1.0f;
/* remember where we started */
set_flen( 0 );
pos = (int)_pico_memstream_tell( fp );
/* index */
clip->index = getI4( fp );
/* first subchunk header */
clip->type = getU4( fp );
sz = getU2( fp );
if ( 0 > get_flen() ) goto Fail;
sz += sz & 1;
set_flen( 0 );
switch ( clip->type ) {
case ID_STIL:
clip->source.still.name = getS0( fp );
break;
case ID_ISEQ:
clip->source.seq.digits = getU1( fp );
clip->source.seq.flags = getU1( fp );
clip->source.seq.offset = getI2( fp );
getU2( fp ); /* not sure what this is yet */
clip->source.seq.start = getI2( fp );
clip->source.seq.end = getI2( fp );
clip->source.seq.prefix = getS0( fp );
clip->source.seq.suffix = getS0( fp );
break;
case ID_ANIM:
clip->source.anim.name = getS0( fp );
clip->source.anim.server = getS0( fp );
rlen = get_flen();
clip->source.anim.data = getbytes( fp, sz - rlen );
break;
case ID_XREF:
clip->source.xref.index = getI4( fp );
clip->source.xref.string = getS0( fp );
break;
case ID_STCC:
clip->source.cycle.lo = getI2( fp );
clip->source.cycle.hi = getI2( fp );
clip->source.cycle.name = getS0( fp );
break;
default:
break;
}
/* error while reading current subchunk? */
rlen = get_flen();
if ( rlen < 0 || rlen > sz ) goto Fail;
/* skip unread parts of the current subchunk */
if ( rlen < sz )
_pico_memstream_seek( fp, sz - rlen, PICO_SEEK_CUR );
/* end of the CLIP chunk? */
rlen = (int)_pico_memstream_tell( fp ) - pos;
if ( cksize < rlen ) goto Fail;
if ( cksize == rlen )
return clip;
/* process subchunks as they're encountered */
id = getU4( fp );
sz = getU2( fp );
if ( 0 > get_flen() ) goto Fail;
while ( 1 ) {
sz += sz & 1;
set_flen( 0 );
switch ( id ) {
case ID_TIME:
clip->start_time = getF4( fp );
clip->duration = getF4( fp );
clip->frame_rate = getF4( fp );
break;
case ID_CONT:
clip->contrast.val = getF4( fp );
clip->contrast.eindex = getVX( fp );
break;
case ID_BRIT:
clip->brightness.val = getF4( fp );
clip->brightness.eindex = getVX( fp );
break;
case ID_SATR:
clip->saturation.val = getF4( fp );
clip->saturation.eindex = getVX( fp );
break;
case ID_HUE:
clip->hue.val = getF4( fp );
clip->hue.eindex = getVX( fp );
break;
case ID_GAMM:
clip->gamma.val = getF4( fp );
clip->gamma.eindex = getVX( fp );
break;
case ID_NEGA:
clip->negative = getU2( fp );
break;
case ID_IFLT:
case ID_PFLT:
filt = _pico_calloc( 1, sizeof( lwPlugin ));
if ( !filt ) goto Fail;
filt->name = getS0( fp );
filt->flags = getU2( fp );
rlen = get_flen();
filt->data = getbytes( fp, sz - rlen );
if ( id == ID_IFLT ) {
lwListAdd( (void *) &clip->ifilter, filt );
clip->nifilters++;
}
else {
lwListAdd( (void *) &clip->pfilter, filt );
clip->npfilters++;
}
break;
default:
break;
}
/* error while reading current subchunk? */
rlen = get_flen();
if ( rlen < 0 || rlen > sz ) goto Fail;
/* skip unread parts of the current subchunk */
if ( rlen < sz )
_pico_memstream_seek( fp, sz - rlen, PICO_SEEK_CUR );
/* end of the CLIP chunk? */
rlen = (int)_pico_memstream_tell( fp ) - pos;
if ( cksize < rlen ) goto Fail;
if ( cksize == rlen ) break;
/* get the next chunk header */
set_flen( 0 );
id = getU4( fp );
sz = getU2( fp );
if ( 6 != get_flen() ) goto Fail;
}
return clip;
Fail:
lwFreeClip( clip );
return NULL;
}
/*
======================================================================
lwFindClip()
Returns an lwClip pointer, given a clip index.
====================================================================== */
lwClip *lwFindClip( lwClip *list, int index )
{
lwClip *clip;
clip = list;
while ( clip ) {
if ( clip->index == index ) break;
clip = clip->next;
}
return clip;
}