flic 0.1.6

Autodesk Animator FLI and Autodesk Animator Pro FLC file encoder and decoder.
Documentation
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Codec for chunk type 12 = FLI_LC.

use std::io::{Cursor,Read,Seek,SeekFrom,Write};
use byteorder::LittleEndian as LE;
use byteorder::{ReadBytesExt,WriteBytesExt};

use ::{FlicError,FlicResult,Raster,RasterMut};
use super::{Group,GroupByLC};

/// Magic for a FLI_LC chunk - Byte Aligned Delta Compression.
///
/// This chunk contains the differences between the previous frame and
/// this frame.  This compression method was used by the original
/// Animator, but is not created by Animator Pro.  This type of chunk
/// can appear in an Animator Pro file, however, if the file was
/// originally created by Animator, then some (but not all) frames
/// were modified using Animator Pro.
///
/// The first 16-bit word following the chunk header contains the
/// position of the first line in the chunk.  This is a count of lines
/// (down from the top of the image) which are unchanged from the
/// prior frame.  The second 16-bit word contains the number of lines
/// in the chunk.  The data for the lines follows these two words.
///
/// Each line begins with a single byte that contains the number of
/// packets for the line.  Unlike BRUN compression, the packet count
/// is significant (because this compression method is only used on
/// 320x200 FLICs).
///
/// Each packet consists of a single byte column skip, followed by a
/// packet type/size byte.  If the packet type is positive it is a
/// count of pixels to be copied from the packet to the animation
/// image.  If the packet type is negative it contains a single pixel
/// which is to be replicated; the absolute value of the packet type
/// gives the number of times the pixel is to be replicated.
///
/// # Note
///
/// The negative/positive meaning of the packet type bytes in LC
/// compression is reversed from that used in BRUN compression.  This
/// gives better performance during playback.
pub const FLI_LC: u16 = 12;

#[derive(Clone,Copy,Debug)]
enum LcOp {
    Skip(usize),
    Memset(usize, usize),
    Memcpy(usize, usize),
}

/// Decode a FLI_LC chunk.
pub fn decode_fli_lc(src: &[u8], dst: &mut RasterMut)
        -> FlicResult<()> {
    let mut r = Cursor::new(src);
    let y0 = r.read_u16::<LE>()? as usize;
    let hh = r.read_u16::<LE>()? as usize;

    let start = dst.stride * (dst.y + y0);
    let end = dst.stride * (dst.y + y0 + hh);
    for row in dst.buf[start..end].chunks_mut(dst.stride) {
        let count = r.read_u8()?;
        let mut x0 = dst.x;

        for _ in 0..count {
            let nskip = r.read_u8()? as usize;
            let signed_length = r.read_i8()? as i32;

            if signed_length >= 0 {
                let start = x0 + nskip;
                let end = start + signed_length as usize;
                if end > row.len() {
                    return Err(FlicError::Corrupted);
                }

                r.read_exact(&mut row[start..end])?;

                x0 = end;
            } else {
                let start = x0 + nskip;
                let end = start + (-signed_length) as usize;
                if end > row.len() {
                    return Err(FlicError::Corrupted);
                }

                let c = r.read_u8()?;
                for e in &mut row[start..end] {
                    *e = c;
                }

                x0 = end;
            }
        }
    }

    Ok(())
}

/// Encode a FLI_LC chunk.
pub fn encode_fli_lc<W: Write + Seek>(
        prev: &Raster, next: &Raster, w: &mut W)
        -> FlicResult<usize> {
    if (prev.w != next.w) || (prev.h != next.h) {
        return Err(FlicError::WrongResolution);
    }

    let prev_start = prev.stride * prev.y;
    let prev_end = prev.stride * (prev.y + prev.h);
    let next_start = next.stride * next.y;
    let next_end = next.stride * (next.y + next.h);

    let y0 = prev.buf[prev_start..prev_end].chunks(prev.stride)
            .zip(next.buf[next_start..next_end].chunks(next.stride))
            .take_while(|&(p, n)| &p[prev.x..(prev.x + prev.w)] == &n[next.x..(next.x + next.w)])
            .count();

    if y0 >= next.h {
        return Ok(0);
    }

    let y1 = next.h - prev.buf[prev_start..prev_end].chunks(prev.stride)
            .zip(next.buf[next_start..next_end].chunks(next.stride))
            .rev()
            .take_while(|&(p, n)| &p[prev.x..(prev.x + prev.w)] == &n[next.x..(next.x + next.w)])
            .count();

    if y1 <= y0 {
        return Ok(0);
    }

    let hh = y1 - y0;
    if (y0 > ::std::u16::MAX as usize) || (hh > ::std::u16::MAX as usize) {
        return Err(FlicError::ExceededLimit);
    }

    // Reserve space for y0, hh.
    let max_size = (next.w * next.h) as u64;
    let pos0 = w.seek(SeekFrom::Current(0))?;
    w.write_u16::<LE>(y0 as u16)?;
    w.write_u16::<LE>(hh as u16)?;

    let prev_start = prev.stride * y0;
    let prev_end = prev.stride * y1;
    let next_start = next.stride * y0;
    let next_end = next.stride * y1;

    for (p, n) in prev.buf[prev_start..prev_end].chunks(prev.stride)
            .zip(next.buf[next_start..next_end].chunks(next.stride)) {
        let p = &p[prev.x..(prev.x + prev.w)];
        let n = &n[next.x..(next.x + next.w)];

        // Reserve space for count.
        let pos1 = w.seek(SeekFrom::Current(0))?;
        w.write_u8(0)?;

        let mut state = LcOp::Skip(0);
        let mut count = 0;

        for g in GroupByLC::new_lc(p, n)
                .set_prepend_same_run()
                .set_ignore_final_same_run() {
            if let Some(new_state) = combine_packets(state, g) {
                state = new_state;
            } else {
                let new_state = convert_packet(g);

                count = write_packet(state, count, n, w)?;

                // Insert Skip(0) between Memcpy and Memset operations.
                match (state, new_state) {
                    (LcOp::Skip(_), _) => {},
                    (_, LcOp::Skip(_)) => {},
                    _ => count = write_packet(LcOp::Skip(0), count, n, w)?,
                }

                if count > 2 * ::std::u8::MAX as usize {
                    return Err(FlicError::ExceededLimit);
                }

                state = new_state;
            }
        }

        if let LcOp::Skip(_) = state {
        } else {
            count = write_packet(state, count, n, w)?;
        }

        assert!(count % 2 == 0);
        if count > 2 * ::std::u8::MAX as usize {
            return Err(FlicError::ExceededLimit);
        }

        let pos2 = w.seek(SeekFrom::Current(0))?;
        if pos2 - pos0 > max_size {
            return Err(FlicError::ExceededLimit);
        }

        w.seek(SeekFrom::Start(pos1))?;
        w.write_u8((count / 2) as u8)?;
        w.seek(SeekFrom::Start(pos2))?;
    }

    // If odd number, pad it to be even.
    let mut pos1 = w.seek(SeekFrom::Current(0))?;
    if (pos1 - pos0) % 2 == 1 {
        w.write_u8(0)?;
        pos1 = pos1 + 1;
    }

    Ok((pos1 - pos0) as usize)
}

fn combine_packets(s0: LcOp, s1: Group)
        -> Option<LcOp> {
    match (s0, s1) {
        (LcOp::Skip(a), Group::Same(_, b)) => return Some(LcOp::Skip(a + b)),
        (LcOp::Skip(_), Group::Diff(..)) => return None,

        // 1. Memset: length + data (1)
        //    Skip:   length (1)
        //    Memcpy: length (1) + data
        //
        // 2. Memcpy: length + data (a)
        //    Memcpy: data (b)
        //    Memcpy: data
        (LcOp::Memset(idx, a), Group::Same(_, b)) =>
            if a + b < 3 {
                return Some(LcOp::Memcpy(idx, a + b));
            },

        // 1. Memset: length + data (1)
        //    Skip:   length (1)
        //    Memset: length (1) + data (1)
        //
        // 2. Memcpy: length + data (a)
        //    Memcpy: data (b)
        (LcOp::Memset(idx, a), Group::Diff(_, b)) =>
            if a + b <= 4 {
                return Some(LcOp::Memcpy(idx, a + b));
            },

        // 1. Memcpy: length + data (a)
        //    Skip:   length (1)
        //    Memcpy: length (1) + data
        //
        // 2. Memcpy: length + data (a)
        //    Memcpy: data (b)
        //    Memcpy: data
        (LcOp::Memcpy(idx, a), Group::Same(_, b)) =>
            if b < 2 {
                return Some(LcOp::Memcpy(idx, a + b));
            },

        // 1. Memcpy: length + data (a)
        //    Skip:   length (1)
        //    Memset: length (1) + data (1)
        //    Skip:   length (1)
        //    Memcpy: length (1) + data
        //
        // 2. Memcpy: length + data (a)
        //    Memcpy: data (b)
        //    Memcpy: data
        (LcOp::Memcpy(idx, a), Group::Diff(_, b)) =>
            if b < 5 {
                return Some(LcOp::Memcpy(idx, a + b));
            },
    }

    // Don't combine s0 and s1 into a single operation.
    None
}

fn convert_packet(g: Group)
        -> LcOp {
    match g {
        Group::Same(_, len) => LcOp::Skip(len),
        Group::Diff(start, len) => LcOp::Memset(start, len),
    }
}

fn write_packet<W: Write>(
        op: LcOp, count: usize, buf: &[u8], w: &mut W)
        -> FlicResult<usize> {
    let mut count = count;
    match op {
        LcOp::Skip(mut len) => {
            let max = ::std::u8::MAX as usize;
            while len > max {
                w.write_u8(max as u8)?;
                w.write_i8(0)?; // copy 0

                len = len - max;
                count = count + 2;
            }

            w.write_u8(len as u8)?;
            count = count + 1;
        },
        LcOp::Memset(idx, mut len) => {
            let max = (-(::std::i8::MIN as i32)) as usize;
            while len > max {
                w.write_i8(max as i8)?;
                w.write_u8(buf[idx])?;
                w.write_u8(0)?; // skip 0

                len = len - max;
                count = count + 2;
            }

            w.write_i8(-(len as i32) as i8)?;
            w.write_u8(buf[idx])?;
            count = count + 1;
        },
        LcOp::Memcpy(mut idx, mut len) => {
            let max = ::std::i8::MAX as usize;
            while len > max {
                w.write_i8(max as i8)?;
                w.write_all(&buf[idx..(idx + max)])?;
                w.write_u8(0)?; // skip 0

                idx = idx + max;
                len = len - max;
                count = count + 2;
            }

            w.write_u8(len as u8)?;
            w.write_all(&buf[idx..(idx + len)])?;
            count = count + 1;
        },
    }

    Ok(count)
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;
    use ::{Raster,RasterMut};
    use super::*;

    #[test]
    fn test_decode_fli_lc() {
        let src = [
            0x02, 0x00, // y0 2
            0x01, 0x00, // hh 1
            0x02,       // count 2
            3, 5,       // skip 3, length 5
            0x01, 0x23, 0x45, 0x67, 0x89,
            2, (-4i8) as u8,    // skip 2, length -4
            0xAB ];

        let expected = [
            0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89,
            0x00, 0x00, 0xAB, 0xAB, 0xAB, 0xAB,
            0x00, 0x00 ];

        const SCREEN_W: usize = 320;
        const SCREEN_H: usize = 200;
        let mut buf = [0; SCREEN_W * SCREEN_H];
        let mut pal = [0; 3 * 256];

        let res = decode_fli_lc(&src,
                &mut RasterMut::new(SCREEN_W, SCREEN_H, &mut buf, &mut pal));
        assert!(res.is_ok());
        assert_eq!(&buf[(SCREEN_W * 2)..(SCREEN_W * 2 + 16)], &expected[..]);
    }

    #[test]
    fn test_encode_fli_lc() {
        let src1 = [
            0x00, 0x00,
            0x01, 0x23, 0x45, 0x67, 0x89,
            0x00, 0x00, 0xAB, 0xAB, 0xAB, 0xAB,
            0x00, 0x00, 0x00 ];

        let src2 = [
            0x01, 0x00, 0x45, 0x00, 0x89,
            0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
            0x00, 0x00, 0x00 ];

        let expected = [
            0x02, 0x00, // y0 2
            0x03, 0x00, // hh 3
            2,          // count 2
            2, 5,       // skip 2, length 5
            0x01, 0x23, 0x45, 0x67, 0x89,
            2, (-4i8) as u8,    // skip 2, length -4
            0xAB,
            0,          // count 0
            2,          // count 2
            0, 5,       // skip 0, length 5
            0x01, 0x00, 0x45, 0x00, 0x89,
            0, (-8i8) as u8,    // skip 0, length -8
            0xAB,
            0x00 ];     // even

        const SCREEN_W: usize = 32;
        const SCREEN_H: usize = 8;
        let buf1 = [0; SCREEN_W * SCREEN_H];
        let mut buf2 = [0; SCREEN_W * SCREEN_H];
        let pal = [0; 3 * 256];
        buf2[(SCREEN_W * 2)..(SCREEN_W * 2 + 16)].copy_from_slice(&src1[..]);
        buf2[(SCREEN_W * 4)..(SCREEN_W * 4 + 16)].copy_from_slice(&src2[..]);

        let mut enc: Cursor<Vec<u8>> = Cursor::new(Vec::new());

        let prev = Raster::new(SCREEN_W, SCREEN_H, &buf1, &pal);
        let next = Raster::new(SCREEN_W, SCREEN_H, &buf2, &pal);
        let res = encode_fli_lc(&prev, &next, &mut enc);
        assert!(res.is_ok());
        assert_eq!(&enc.get_ref()[..], &expected[..]);
    }
}