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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
//! FLIC implementation.

use std::cmp::min;
use std::fs::File;
use std::io::{Cursor,Read,Seek,SeekFrom,Write};
use std::path::{Path,PathBuf};
use byteorder::LittleEndian as LE;
use byteorder::{ReadBytesExt,WriteBytesExt};

use ::{FlicError,FlicResult,Raster,RasterMut};
use ::pstamp::{PostageStamp,write_pstamp_data};
use codec::*;

/// Magic for a FLI file - Original Animator FLI Files.
///
/// This animation file format is limited to 320x200 resolution.  It
/// is the main animation file format of the original Animator, and is
/// still used by Animator Pro for creating 320x200 animations.  The
/// file structure is very similar to that of a FLC file.  A FLI file
/// does not contain a prefix chunk, and does not use FLI_PSTAMP or
/// FLI_SS2 data encoding in the frame chunks.
///
/// The file header for a FLI file is a subset of the FLC file header.
/// It is defined as follows:
///
///   Offset | Length |   Name   | Description
///   ------:| ------:|:--------:| -----------------------------------
///        0 |      4 |   size   | The size of the entire animation file, including this file header.
///        4 |      2 |   magic  | File format identifier.  Always 0xAF11.
///        6 |      2 |  frames  | Number of frames in the FLIC.  This count does not include the ring frame.  FLI files have a maximum length of 4000 frames.
///        8 |      2 |   width  | Screen width in pixels.  This is always 320 in a FLI file.
///       10 |      2 |  height  | Screen height in pixels.  This is always 200 in a FLI file.
///       12 |      2 |   depth  | Bits per pixel (always 8).
///       14 |      2 |   flags  | Always zero in a FLI file.
///       16 |      2 |   speed  | Number of jiffies to delay between each frame during playback.  A jiffy is 1/70 of a second.
///       18 |    110 | reserved | Unused space, set to zeroes.
pub const FLIH_MAGIC: u16 = 0xAF11;

/// Magic for a FLC file - Animator Pro FLC Files.
///
/// This is the main animation file format created by Animator Pro.
/// The file contains a 128-byte header, followed by an optional
/// prefix chunk, followed by one or more frame chunks.
///
/// The prefix chunk, if present, contains Animator Pro settings
/// information, CEL placement information, and other auxiliary data.
///
/// A frame chunk exists for each frame in the animation.  In
/// addition, a ring frame follows all the animation frames.  Each
/// frame chunk contains color palette information and/or pixel data.
///
/// The ring frame contains delta-compressed information to loop from
/// the last frame of the FLIC back to the first.  It can be helpful
/// to think of the ring frame as a copy of the first frame,
/// compressed in a different way.  All FLIC files will contain a ring
/// frame, including a single-frame FLIC.
///
/// A FLC file begins with a 128-byte header, described below.  All
/// lengths and offsets are in bytes.  All values stored in the header
/// fields are unsigned.
///
///   Offset | Length |   Name   | Description
///   ------:| ------:|:--------:| -----------------------------------
///        0 |      4 |   size   | The size of the entire animation file, including this file header.
///        4 |      2 |   magic  | File format identifier.  Always 0xAF12.
///        6 |      2 |  frames  | Number of frames in the FLIC.  This count does not include the ring frame.  FLC files have a maximum length of 4000 frames.
///        8 |      2 |   width  | Screen width in pixels.
///       10 |      2 |   height | Screen height in pixels.
///       12 |      2 |   depth  | Bits per pixel (always 8).
///       14 |      2 |   flags  | Set to 0x0003 after ring frame is written and FLIC header is updated.  This indicates that the file was properly finished and closed.
///       16 |      4 |   speed  | Number of milliseconds to delay between each frame during playback.
///       20 |      2 | reserved | Unused word, set to 0.
///       22 |      4 |  created | The MSDOS-formatted date and time of the file's creation.
///       26 |      4 |  creator | The serial number of the Animator Pro program used to create the file.  If the file was created by some other program using the FlicLib development kit, this value is 0x464C4942 ("FLIB").
///       30 |      4 |  updated | The MSDOS-formatted date and time of the file's most recent update.
///       34 |      4 |  updater | Indicates who last updated the file.  See the description of creator.
///       38 |      2 |  aspectx | The x-axis aspect ratio at which the file was created.
///       40 |      2 |  aspecty | The y-axis aspect ratio at which the file was created.  Most often, the x:y aspect ratio will be 1:1.  A 320x200 FLIC has a ratio of 6:5.
///       42 |     38 | reserved | Unused space, set to zeroes.
///       80 |      4 |  oframe1 | Offset from the beginning of the file to the first animation frame chunk.
///       84 |      4 |  oframe2 | Offset from the beginning of the file to the second animation frame chunk.  This value is used when looping from the ring frame back to the second frame during playback.
///       88 |     40 | reserved | Unused space, set to zeroes.
pub const FLIHR_MAGIC: u16 = 0xAF12;

/// Default updater for files written by LibFLIC, "FLRS".
pub const LIBFLIC_UPDATER_ID: u32 = 0x464C5253;

/// FLIC animation, with a File handle.
///
/// Opens and holds onto the file handle until it is dropped.
#[allow(dead_code)]
pub struct FlicFile {
    hdr: FlicHeader,
    frame_hdr: Vec<FlicFrame>,
    frame: usize,

    filename: PathBuf,
    file: File,
}

/// FLIC animation writer, with a File handle.
///
/// Opens and holds onto the file handle until it is closed.
pub struct FlicFileWriter {
    hdr: FlicHeader,
    offset_frame1: u64,
    offset_frame2: u64,

    filename: PathBuf,
    file: Option<File>,
}

/// Size of a FLIC file header on disk.
///
/// A FLIC file begins with a 128-byte header, described below.  All
/// lengths and offsets are in bytes.  All values stored in the header
/// fields are unsigned.
pub const SIZE_OF_FLIC_HEADER: usize = 128;

/// FLIC header.
struct FlicHeader {
    magic: u16,
    size: u32,
    frame_count: u16,
    w: u16,
    h: u16,
    speed_msec: u32,
    speed_jiffies: u16,
    created: u32,
    creator: u32,
    updated: u32,
    updater: u32,
    aspect_x: u16,
    aspect_y: u16,
}


/// Magic for a FLIC pre-frame chunk - FLIC Prefix Chunk.
///
/// An optional prefix chunk may immediately follow the animation file
/// header.  This chunk is used to store auxiliary data which is not
/// directly involved in the animation playback.  The prefix chunk
/// starts with a 16-byte header (identical in structure to a frame
/// header), as follows:
///
///   Offset | Length |   Name   | Description
///   ------:| ------:|:--------:| -----------------------------------
///        0 |      4 |   size   | The size of the prefix chunk, including this header and all subordinate chunks that follow.
///        4 |      2 |   type   | Prefix chunk identifier.  Always 0xF100.
///        6 |      2 |  chunks  | Number of subordinate chunks in the prefix chunk.
///        8 |      8 | reserved | Unused space, set to zeroes.
///
/// To determine whether a prefix chunk is present, read the 16-byte
/// header following the file header.  If the type value is 0xF100,
/// it's a prefix chunk.  If the value is 0xF1FA it's the first frame
/// chunk, and no prefix chunk exists.
///
/// # Note
///
/// Programs other than Animator Pro should never need to create FLIC
/// files that contain a prefix chunk.  Programs reading a FLIC file
/// should skip the prefix chunk by using the size value in the prefix
/// header to read and discard the prefix, or by seeking directly to
/// the first frame using the oframe1 field from the file header.
pub const FCID_PREFIX: u16 = 0xF100;

/// Magic for a FLIC frame - FLIC Frame Chunks.
///
/// Frame chunks contain the pixel and color data for the animation.
/// A frame chunk may contain multiple subordinate chunks, each
/// containing a different type of data for the current frame.  Each
/// frame chunk starts with a 16-byte header that describes the
/// contents of the frame:
///
///   Offset | Length |   Name   | Description
///   ------:| ------:|:--------:| -----------------------------------
///        0 |      4 |   size   | The size of the frame chunk, including this header and all subordinate chunks that follow.
///        4 |      2 |   type   | Frame chunk identifier.
///        6 |      2 |  chunks  | Number of subordinate chunks in the frame chunk.
///        8 |      8 | reserved | Unused space, set to zeroes.
pub const FCID_FRAME: u16 = 0xF1FA; // also: FLIF_MAGIC.

/// Size of a FLIC frame header on disk.
pub const SIZE_OF_FLIC_FRAME: usize = 16;

/// FLIC frame header.
struct FlicFrame {
    chunks: Vec<ChunkId>,
}


/// Size of a chunk header on disk.
///
/// Immediately following the frame header are the frame's subordinate
/// data chunks.  When the chunks count in the frame header is zero,
/// it indicates that this frame is identical to the previous frame.
/// This implies that no change is made to the screen or color
/// palette, but the appropriate delay is still inserted during
/// playback.
///
/// Each data chunk within a frame chunk is formatted as follows:
///
///   Offset | Length | Name | Description
///   ------:| ------:|:----:| ---------------------------------------
///        0 |      4 | size | The size of the chunk, including this header.
///        4 |      2 | type | Data type identifier.
///        6 | size-6 | data | The color or pixel data.
///
/// The type values in the chunk headers indicate what type of
/// graphics data the chunk contains and which compression method was
/// used to encode the data.
pub const SIZE_OF_CHUNK: usize = 6;

/// Chunk header.
struct ChunkId {
    // Note: offset to the data.
    offset: u64,

    // Note: number of bytes in the data, excluding the chunk header.
    size: u32,

    magic: u16,
}


/// Record containing playback information.
pub struct FlicPlaybackResult {
    pub ended: bool,
    pub looped: bool,
    pub palette_updated: bool,
}

/*--------------------------------------------------------------*/

impl FlicFile {
    /// Open a FLIC file.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::path::Path;
    ///
    /// flic::FlicFile::open(Path::new("ex.fli"));
    /// ```
    pub fn open(filename: &Path)
            -> FlicResult<Self> {
        if !filename.exists() {
            return Err(FlicError::NoFile);
        } else if !filename.is_file() {
            return Err(FlicError::NotARegularFile);
        }

        let mut file = File::open(filename)?;
        let hdr = read_flic_header(&mut file)?;
        let frame_hdr = read_frame_headers(&mut file, &hdr)?;

        Ok(FlicFile {
            hdr: hdr,
            frame_hdr: frame_hdr,
            frame: 0,

            filename: filename.to_path_buf(),
            file: file,
        })
    }

    /// Get the next frame number.
    pub fn frame(&self) -> u16 {
        self.frame as u16
    }

    /// Get the frame count, not including the ring frame.
    pub fn frame_count(&self) -> u16 {
        self.hdr.frame_count
    }

    /// Get the FLIC width.
    pub fn width(&self) -> u16 {
        self.hdr.w
    }

    /// Get the FLIC height.
    pub fn height(&self) -> u16 {
        self.hdr.h
    }

    /// Number of milliseconds to delay between each frame during playback.
    pub fn speed_msec(&self) -> u32 {
        self.hdr.speed_msec
    }

    /// Number of jiffies to delay between each frame during playback.
    /// A jiffy is 1/70 of a second.
    pub fn speed_jiffies(&self) -> u16 {
        self.hdr.speed_jiffies
    }

    /// Get the FLIC creator.
    pub fn creator(&self) -> u32 {
        self.hdr.creator
    }

    /// Get the FLIC creation time.
    pub fn creation_time(&self) -> u32 {
        self.hdr.created
    }

    /// Get the most recent updater.
    pub fn updater(&self) -> u32 {
        self.hdr.updater
    }

    /// Get the most recent update time.
    pub fn update_time(&self) -> u32 {
        self.hdr.updated
    }

    /// Get the x-axis aspect ratio.
    pub fn aspect_x(&self) -> u16 {
        self.hdr.aspect_x
    }

    /// Get the y-axis aspect ratio.
    pub fn aspect_y(&self) -> u16 {
        self.hdr.aspect_y
    }

    /// Decode the postage stamp.
    pub fn read_postage_stamp<'a>(&mut self, dst: &'a mut RasterMut<'a>)
            -> FlicResult<()> {
        let mut pstamp = PostageStamp::new(
                self.hdr.w as usize, self.hdr.h as usize, dst);

        for chunk in self.frame_hdr[0].chunks.iter() {
            self.file.seek(SeekFrom::Start(chunk.offset))?;

            let mut buf = vec![0; chunk.size as usize];
            self.file.read_exact(&mut buf)?;

            let done = pstamp.feed(chunk.magic, &buf)?;
            if done {
                break;
            }
        }

        Ok(())
    }

    /// Decode the next frame in the FLIC.
    ///
    /// The raster buffer must contain the previous frame.
    /// The FLIC file will loop when it reaches the last frame.
    ///
    /// Returns a record indicating what was processed.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::path::Path;
    ///
    /// if let Ok(ref mut flic) = flic::FlicFile::open(Path::new("ex.fli")) {
    ///     const SCREEN_W: usize = 320;
    ///     const SCREEN_H: usize = 200;
    ///     const NUM_COLS: usize = 256;
    ///     let mut buf = [0; SCREEN_W * SCREEN_H];
    ///     let mut pal = [0; 3 * NUM_COLS];
    ///     let mut raster = flic::RasterMut::new(SCREEN_W, SCREEN_H, &mut buf, &mut pal);
    ///
    ///     let res = flic.read_next_frame(&mut raster);
    /// }
    /// ```
    pub fn read_next_frame(&mut self, dst: &mut RasterMut)
            -> FlicResult<FlicPlaybackResult> {
        let mut res = FlicPlaybackResult {
            ended: false,
            looped: false,
            palette_updated: false,
        };

        if (self.hdr.w as usize != dst.w) || (self.hdr.h as usize != dst.h) {
            return Err(FlicError::WrongResolution);
        }

        let frame = &self.frame_hdr[self.frame];
        for chunk in frame.chunks.iter() {
            self.file.seek(SeekFrom::Start(chunk.offset))?;

            let mut buf = vec![0; chunk.size as usize];
            self.file.read_exact(&mut buf)?;

            decode_chunk(chunk.magic, &buf, dst)?;

            res.palette_updated = res.palette_updated
                    || chunk_modifies_palette(chunk.magic);
        }

        if self.frame + 1 >= self.frame_hdr.len() {
            // Skip to second frame, since FLIC animations include a ring frame.
            self.frame = 1;
            res.looped = true;
        } else {
            self.frame = self.frame + 1;
        }

        if self.frame + 1 >= self.frame_hdr.len() {
            res.ended = true;
        }

        Ok(res)
    }
}

/*--------------------------------------------------------------*/

impl FlicFileWriter {
    /// Open a file for writing Animator Pro FLCs.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::path::Path;
    ///
    /// const SCREEN_W: u16 = 320;
    /// const SCREEN_H: u16 = 200;
    /// const speed_msec: u32 = 70;
    ///
    /// flic::FlicFileWriter::create(Path::new("ex.flc"), SCREEN_W, SCREEN_H, speed_msec);
    /// ```
    pub fn create(filename: &Path, w: u16, h: u16, speed_msec: u32)
            -> FlicResult<Self> {
        let mut file = File::create(filename)?;

        // Reserve space for header.
        file.write_all(&[0; SIZE_OF_FLIC_HEADER])?;

        let jiffy_speed = min((speed_msec as u64) * 70 / 1000, ::std::u16::MAX as u64) as u16;

        let hdr = FlicHeader {
            magic: FLIHR_MAGIC,
            size: 0,
            frame_count: 0,
            w: w,
            h: h,
            speed_msec: speed_msec,
            speed_jiffies: jiffy_speed,
            created: 0,
            creator: 0,
            updated: 0,
            updater: LIBFLIC_UPDATER_ID,
            aspect_x: 1,
            aspect_y: 1,
        };

        Ok(FlicFileWriter{
            hdr: hdr,
            offset_frame1: 0,
            offset_frame2: 0,
            filename: filename.to_path_buf(),
            file: Some(file),
        })
    }

    /// Open a file for writing Animator FLIs.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::path::Path;
    ///
    /// const speed_jiffies: u16 = 5;
    ///
    /// flic::FlicFileWriter::create_fli(Path::new("ex.fli"), speed_jiffies);
    /// ```
    pub fn create_fli(filename: &Path, speed_jiffies: u16)
            -> FlicResult<Self> {
        let mut file = File::create(filename)?;

        // Reserve space for header.
        file.write_all(&[0; SIZE_OF_FLIC_HEADER])?;

        let hdr = FlicHeader {
            magic: FLIH_MAGIC,
            size: 0,
            frame_count: 0,
            w: 320,
            h: 200,
            speed_msec: (speed_jiffies as u32) * 1000 / 70,
            speed_jiffies: speed_jiffies,
            created: 0,
            creator: 0,
            updated: 0,
            updater: LIBFLIC_UPDATER_ID,
            aspect_x: 6,
            aspect_y: 5,
        };

        Ok(FlicFileWriter{
            hdr: hdr,
            offset_frame1: 0,
            offset_frame2: 0,
            filename: filename.to_path_buf(),
            file: Some(file),
        })
    }

    /// Set the FLIC creator and creation time.
    pub fn set_creator(&mut self, creator: u32, created: u32) {
        self.hdr.creator = creator;
        self.hdr.created = created;
    }

    /// Set the most recent updater and update time.
    pub fn set_updater(&mut self, updater: u32, updated: u32) {
        self.hdr.updater = updater;
        self.hdr.updated = updated;
    }

    /// Set the aspect ratio, i.e. x by y is a square.
    ///
    /// Most often, the x:y aspect ratio will be 1:1.
    /// A 320x200 FLIC has a ratio of 6:5.
    pub fn set_aspect_ratio(&mut self, x: u16, y: u16) {
        if x > 0 && y > 0 {
            self.hdr.aspect_x = x;
            self.hdr.aspect_y = y;
        } else {
            self.hdr.aspect_x = 1;
            self.hdr.aspect_y = 1;
        }
    }

    /// Close the FLIC file.
    ///
    /// You must close the FLIC writer after you have supplied all the
    /// frames, including the ring frame, to write out the header.
    ///
    /// The FLIC writer is not usable after being closed.
    pub fn close(mut self)
            -> FlicResult<()> {
        if let Some(mut file) = self.file.take() {
            if self.hdr.frame_count == 0 {
                return Err(FlicError::Corrupted);
            } else if self.hdr.frame_count == 1 {
                self.offset_frame2 = file.seek(SeekFrom::Current(0))?;
                write_empty_frame(&mut file)?;
            } else {
                self.hdr.frame_count = self.hdr.frame_count - 1;
            }

            let size = file.seek(SeekFrom::Current(0))?;
            if size > ::std::u32::MAX as u64 {
                return Err(FlicError::ExceededLimit);
            }

            self.hdr.size = size as u32;
            file.seek(SeekFrom::Start(0))?;
            write_flic_header(
                    &self.hdr, self.offset_frame1, self.offset_frame2,
                    &mut file)?;

            Ok(())
        } else {
            Err(FlicError::NoFile)
        }
    }

    /// Encode the next frame in the FLIC.
    ///
    /// You must supply the previous frame buffer, or None if it is
    /// the first frame.  Upon reaching the last frame in the
    /// animation, you must also supply the first frame to create the
    /// ring frame.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::path::Path;
    ///
    /// const SCREEN_W: u16 = 320;
    /// const SCREEN_H: u16 = 200;
    /// const NUM_COLS: usize = 256;
    /// const speed_msec: u32 = 70;
    /// let buf = [0; (SCREEN_W * SCREEN_H) as usize];
    /// let pal = [0; 3 * NUM_COLS];
    ///
    /// if let Ok(mut flic) = flic::FlicFileWriter::create(
    ///         Path::new("ex.flc"), SCREEN_W, SCREEN_H, speed_msec) {
    ///     let raster1 = flic::Raster::new(SCREEN_W as usize, SCREEN_H as usize, &buf, &pal);
    ///     let raster2 = flic::Raster::new(SCREEN_W as usize, SCREEN_H as usize, &buf, &pal);
    ///     // Write first frame.
    ///     flic.write_next_frame(None, &raster1);
    ///     // Write subsequent frames.
    ///     flic.write_next_frame(Some(&raster1), &raster2);
    ///     // Write ring frame.
    ///     flic.write_next_frame(Some(&raster2), &raster1);
    ///     flic.close();
    /// }
    /// ```
    pub fn write_next_frame(&mut self, prev: Option<&Raster>, next: &Raster)
            -> FlicResult<()> {
        if let Some(mut file) = self.file.as_ref() {
            if (next.w != self.hdr.w as usize) || (next.h != self.hdr.h as usize) {
                return Err(FlicError::WrongResolution);
            }
            if self.hdr.frame_count == ::std::u16::MAX {
                return Err(FlicError::ExceededLimit);
            }

            if self.hdr.frame_count == 0 {
                self.offset_frame1 = file.seek(SeekFrom::Current(0))?;
            } else if self.hdr.frame_count == 1 {
                self.offset_frame2 = file.seek(SeekFrom::Current(0))?;
            }

            let prev = if self.hdr.frame_count == 0 {
                None
            } else {
                prev
            };

            write_next_frame(self.hdr.magic, self.hdr.frame_count,
                    prev, next, &mut file)?;
            self.hdr.frame_count = self.hdr.frame_count + 1;

            Ok(())
        } else {
            Err(FlicError::NoFile)
        }
    }
}

impl Drop for FlicFileWriter {
    /// A method called when the value goes out of scope.
    fn drop(&mut self) {
        if self.file.is_some() {
            println!("Warning: {} was not closed, may be corrupt.",
                    self.filename.to_string_lossy());
        }
    }
}

/*--------------------------------------------------------------*/

/// Read the FLIC's header.
fn read_flic_header(file: &mut File)
        -> FlicResult<FlicHeader> {
    let mut buf = [0; SIZE_OF_FLIC_HEADER];
    file.read_exact(&mut buf)?;

    let mut r = Cursor::new(&buf[..]);
    let size = r.read_u32::<LE>()?;
    let magic = r.read_u16::<LE>()?;

    match magic {
        FLIH_MAGIC => read_fli_header(&mut r, size, magic),
        FLIHR_MAGIC => read_flc_header(&mut r, size, magic),
        _ => Err(FlicError::BadMagic),
    }
}

/// Read the original Animator FLI header.
fn read_fli_header(
        r: &mut Cursor<&[u8]>, size: u32, magic: u16)
        -> FlicResult<FlicHeader> {
    assert_eq!(magic, FLIH_MAGIC);

    let frame_count = r.read_u16::<LE>()?;
    let width = r.read_u16::<LE>()?;
    let height = r.read_u16::<LE>()?;
    let _bpp = r.read_u16::<LE>()?;
    let _flags = r.read_u16::<LE>()?;
    let jiffy_speed = r.read_u16::<LE>()?;

    match r.seek(SeekFrom::Current(110)) {
        Ok(128) => (),
        _ => unreachable!(),
    };

    // Animator 1's FLIC files are always 320x200.
    if width != 320 || height != 200 {
        return Err(FlicError::WrongResolution);
    }
    if frame_count <= 0 {
        return Err(FlicError::Corrupted);
    }

    Ok(FlicHeader{
        magic: magic,
        size: size,
        frame_count: frame_count,
        w: width,
        h: height,
        speed_msec: (jiffy_speed as u32) * 1000 / 70,
        speed_jiffies: jiffy_speed,
        created: 0,
        creator: 0,
        updated: 0,
        updater: 0,
        aspect_x: 6,
        aspect_y: 5,
    })
}

/// Read the Animator Pro FLC header.
fn read_flc_header(
        r: &mut Cursor<&[u8]>, size: u32, magic: u16)
        -> FlicResult<FlicHeader> {
    assert_eq!(magic, FLIHR_MAGIC);

    let frame_count = r.read_u16::<LE>()?;
    let width = r.read_u16::<LE>()?;
    let height = r.read_u16::<LE>()?;
    let _bpp = r.read_u16::<LE>()?;
    let _flags = r.read_u16::<LE>()?;
    let speed = r.read_u32::<LE>()?;
    r.seek(SeekFrom::Current(2))?;
    let created = r.read_u32::<LE>()?;
    let creator = r.read_u32::<LE>()?;
    let updated = r.read_u32::<LE>()?;
    let updater = r.read_u32::<LE>()?;
    let mut aspect_x = r.read_u16::<LE>()?;
    let mut aspect_y = r.read_u16::<LE>()?;
    r.seek(SeekFrom::Current(38))?;
    let _oframe1 = r.read_u32::<LE>()?;
    let _oframe2 = r.read_u32::<LE>()?;

    match r.seek(SeekFrom::Current(40)) {
        Ok(128) => (),
        _ => unreachable!(),
    };

    if frame_count <= 0 || width <= 0 || height <= 0 {
        return Err(FlicError::Corrupted);
    }

    let jiffy_speed = min((speed as u64) * 70 / 1000, ::std::u16::MAX as u64) as u16;

    if aspect_x <= 0 || aspect_y <= 0 {
        aspect_x = 1;
        aspect_y = 1;
    }

    Ok(FlicHeader{
        magic: magic,
        size: size,
        frame_count: frame_count,
        w: width,
        h: height,
        speed_msec: speed,
        speed_jiffies: jiffy_speed,
        created: created,
        creator: creator,
        updated: updated,
        updater: updater,
        aspect_x: aspect_x,
        aspect_y: aspect_y,
    })
}

/// Read all of the FLIC's frame headers.
fn read_frame_headers(file: &mut File, hdr: &FlicHeader)
        -> FlicResult<Vec<FlicFrame>> {
    let mut frames = Vec::with_capacity(min(4096, 1 + hdr.frame_count as usize));
    let mut offset = SIZE_OF_FLIC_HEADER as u64;

    // Add 1 to frame count to account for the ring frame.
    for frame_num in 0..(hdr.frame_count + 1) {
        let mut buf = [0; SIZE_OF_FLIC_FRAME];
        let mut size;
        let mut magic;
        let mut num_chunks;

        file.seek(SeekFrom::Start(offset))?;
        file.read_exact(&mut buf)?;

        {
            let mut r = Cursor::new(&buf[..]);
            size = r.read_u32::<LE>()?;
            magic = r.read_u16::<LE>()?;
            num_chunks = r.read_u16::<LE>()? as usize;

            if size < (SIZE_OF_FLIC_FRAME as u32)
                    || offset + (size as u64) > (hdr.size as u64) {
                return Err(FlicError::Corrupted);
            }
        }

        if frame_num == 0 && magic == FCID_PREFIX {
            offset = offset + size as u64;

            file.seek(SeekFrom::Start(offset))?;
            file.read_exact(&mut buf)?;

            let mut r = Cursor::new(&buf[..]);
            size = r.read_u32::<LE>()?;
            magic = r.read_u16::<LE>()?;
            num_chunks = r.read_u16::<LE>()? as usize;

            if size < (SIZE_OF_FLIC_FRAME as u32)
                    || offset + (size as u64) > (hdr.size as u64) {
                return Err(FlicError::Corrupted);
            }
        }

        if magic != FCID_FRAME {
            return Err(FlicError::BadMagic);
        }

        let chunks = read_chunk_headers(file, hdr,
                frame_num, offset, size, num_chunks)?;
        assert_eq!(chunks.len(), num_chunks);

        // Note: Animator forces chunk sizes to be even.  However,
        // Animator 1 did not update the frame header size
        // accordingly.  This resulted in lost data.
        if num_chunks > 0 {
            let position = chunks[num_chunks - 1].offset + chunks[num_chunks - 1].size as u64;
            let expected = offset + size as u64;
            if position > expected {
                println!("Warning: frame {} reads too much - current offset={}, expected offset={}",
                         frame_num, position, expected);
            } else if position < expected {
                println!("Warning: frame {} reads too little - current offset={}, expected offset={}",
                         frame_num, position, expected);
            }
        }

        frames.push(FlicFrame{
            chunks: chunks,
        });

        offset = offset + size as u64;
    }

    Ok(frames)
}

/// Read all of the frame's chunk headers.
fn read_chunk_headers(file: &mut File, hdr: &FlicHeader,
        frame_num: u16, frame_offset: u64, frame_size: u32, num_chunks: usize)
        -> FlicResult<Vec<ChunkId>> {
    let mut chunks = Vec::with_capacity(min(4, num_chunks));
    let mut offset = frame_offset + SIZE_OF_FLIC_FRAME as u64;

    for _ in 0..num_chunks {
        file.seek(SeekFrom::Start(offset))?;

        let mut buf = [0; SIZE_OF_CHUNK];
        file.read_exact(&mut buf)?;

        let mut r = Cursor::new(&buf[..]);
        let size = r.read_u32::<LE>()?;
        let magic = r.read_u16::<LE>()?;

        if !(SIZE_OF_CHUNK as u32 <= size && size <= frame_size) {
            return Err(FlicError::Corrupted);
        }

        let mut size2 = size;

        match magic {
            // Warn about legacy chunk types.
            FLI_WRUN =>
                println!("Warning: frame {} - FLI_WRUN chunk type detected",
                        frame_num),
            FLI_SBSRSC =>
                println!("Warning: frame {} - FLI_SBSRSC chunk type detected",
                        frame_num),
            FLI_ICOLORS =>
                println!("Warning: frame {} - FLI_ICOLORS chunk type detected",
                        frame_num),

            // A bug in Animator and Animator Pro caused FLI_COPY
            // chunks have size = size of data + 4 (size of pointer)
            // instead of size of data + 6 (size of chunk header).
            // The data was still written to disk; only the chunk's
            // size is incorrect.
            FLI_COPY => {
                if size == hdr.w as u32 * hdr.h as u32 + 4 {
                    size2 = hdr.w as u32 * hdr.h as u32 + 6;
                    println!("Warning: frame {} - FLI_COPY has wrong size",
                            frame_num);
                }
            },

            FLI_COLOR256 | FLI_SS2 | FLI_COLOR64 | FLI_LC | FLI_BLACK | FLI_BRUN | FLI_PSTAMP => (),

            _ => println!("Warning: frame {} - unrecognised chunk type {}",
                    frame_num, magic),
        }

        chunks.push(ChunkId {
            offset: offset + SIZE_OF_CHUNK as u64,
            size: size2 - SIZE_OF_CHUNK as u32,
            magic: magic,
        });

        offset = offset + size as u64;
    }

    Ok(chunks)
}

/*--------------------------------------------------------------*/

/// Write the FLIC header.
fn write_flic_header<W: Write + Seek>(
        hdr: &FlicHeader, offset_frame1: u64, offset_frame2: u64, w: &mut W)
        -> FlicResult<()> {
    match hdr.magic {
        FLIH_MAGIC => write_fli_header(hdr, w),
        FLIHR_MAGIC => write_flc_header(hdr, offset_frame1, offset_frame2, w),
        _ => return Err(FlicError::BadMagic),
    }
}

fn write_fli_header<W: Write + Seek>(
        hdr: &FlicHeader, w: &mut W)
        -> FlicResult<()> {
    let depth = 8;
    let flags = 0;
    w.write_u32::<LE>(hdr.size)?;
    w.write_u16::<LE>(FLIH_MAGIC)?;
    w.write_u16::<LE>(hdr.frame_count)?;
    w.write_u16::<LE>(hdr.w)?;
    w.write_u16::<LE>(hdr.h)?;
    w.write_u16::<LE>(depth)?;
    w.write_u16::<LE>(flags)?;
    w.write_u16::<LE>(hdr.speed_jiffies)?;
    Ok(())
}

fn write_flc_header<W: Write + Seek>(
        hdr: &FlicHeader, offset_frame1: u64, offset_frame2: u64, w: &mut W)
        -> FlicResult<()> {
    let depth = 8;
    let flags = 3;

    w.write_u32::<LE>(hdr.size)?;
    w.write_u16::<LE>(FLIHR_MAGIC)?;
    w.write_u16::<LE>(hdr.frame_count)?;
    w.write_u16::<LE>(hdr.w)?;
    w.write_u16::<LE>(hdr.h)?;
    w.write_u16::<LE>(depth)?;
    w.write_u16::<LE>(flags)?;
    w.write_u32::<LE>(hdr.speed_msec)?;

    w.seek(SeekFrom::Current(2))?; // reserved
    w.write_u32::<LE>(hdr.created)?;
    w.write_u32::<LE>(hdr.creator)?;
    w.write_u32::<LE>(hdr.updated)?;
    w.write_u32::<LE>(hdr.updater)?;
    w.write_u16::<LE>(hdr.aspect_x)?;
    w.write_u16::<LE>(hdr.aspect_y)?;
    w.seek(SeekFrom::Current(38))?;

    // If the offsets are too big, then leave them as 0 and hope other
    // libraries will compute it themselves.
    if offset_frame1 < offset_frame2 && offset_frame2 <= ::std::u32::MAX as u64 {
        w.write_u32::<LE>(offset_frame1 as u32)?;
        w.write_u32::<LE>(offset_frame2 as u32)?;
    }

    Ok(())
}

/// Write an empty frame.
fn write_empty_frame<W: Write>(
        w: &mut W)
        -> FlicResult<()> {
    w.write_u32::<LE>(SIZE_OF_FLIC_FRAME as u32)?;
    w.write_u16::<LE>(FCID_FRAME)?;
    w.write_u16::<LE>(0)?; // chunks
    w.write_all(&[0; 8])?;
    Ok(())
}

/// Write the next frame.
fn write_next_frame<W: Write + Seek>(
        flic_magic: u16, frame_count: u16,
        prev: Option<&Raster>, next: &Raster, w: &mut W)
        -> FlicResult<usize> {
    let pos0 = w.seek(SeekFrom::Current(0))?;

    // Reserve space for chunk.
    w.write_all(&[0; SIZE_OF_FLIC_FRAME])?;

    let size_pstamp =
        if flic_magic != FLIH_MAGIC && frame_count == 0 {
            match write_pstamp_data(next, w) {
                Ok(size) => size,
                Err(_) => {
                    w.seek(SeekFrom::Start(pos0 + SIZE_OF_FLIC_FRAME as u64))?;
                    0
                },
            }
        } else {
            0
        };

    let size_col = write_color_data(flic_magic, prev, next, w)?;
    let size_pix = write_pixel_data(flic_magic, prev, next, w)?;
    let size = SIZE_OF_FLIC_FRAME + size_pstamp + size_col + size_pix;

    if size > ::std::u32::MAX as usize {
        return Err(FlicError::ExceededLimit);
    }

    let pos1 = w.seek(SeekFrom::Current(0))?;

    w.seek(SeekFrom::Start(pos0))?;
    if size > 0 {
        let num_chunks
            = if size_pstamp > 0 { 1 } else { 0 }
            + if size_col > 0 { 1 } else { 0 }
            + if size_pix > 0 { 1 } else { 0 };

        assert_eq!(size, (pos1 - pos0) as usize);
        w.write_u32::<LE>(size as u32)?;
        w.write_u16::<LE>(FCID_FRAME)?;
        w.write_u16::<LE>(num_chunks)?;
        w.seek(SeekFrom::Start(pos1))?;
        Ok(size)
    } else {
        Ok(0)
    }
}

/// Write the next frame's palette.
fn write_color_data<W: Write + Seek>(
        flic_magic: u16, prev: Option<&Raster>, next: &Raster, w: &mut W)
        -> FlicResult<usize> {
    let pos0 = w.seek(SeekFrom::Current(0))?;

    // Reserve space for chunk.
    w.write_all(&[0; SIZE_OF_CHUNK])?;

    let (chunk_size, chunk_magic) =
        if flic_magic == FLIH_MAGIC {
            let size = encode_fli_color64(prev, next, w)?;
            (size, FLI_COLOR64)
        } else {
            let size = encode_fli_color256(prev, next, w)?;
            (size, FLI_COLOR256)
        };

    if SIZE_OF_CHUNK + chunk_size > ::std::u32::MAX as usize {
        return Err(FlicError::ExceededLimit);
    }

    let pos1 = w.seek(SeekFrom::Current(0))?;

    w.seek(SeekFrom::Start(pos0))?;
    if chunk_size > 0 {
        w.write_u32::<LE>((SIZE_OF_CHUNK + chunk_size) as u32)?;
        w.write_u16::<LE>(chunk_magic)?;
        w.seek(SeekFrom::Start(pos1))?;
        Ok(SIZE_OF_CHUNK + chunk_size)
    } else {
        Ok(0)
    }
}

/// Write the next frame's pixels.
fn write_pixel_data<W: Write + Seek>(
        flic_magic: u16, prev: Option<&Raster>, next: &Raster, w: &mut W)
        -> FlicResult<usize> {
    let pos0 = w.seek(SeekFrom::Current(0))?;

    // Reserve space for chunk.
    w.write_all(&[0; SIZE_OF_CHUNK])?;

    let mut chunk_size = next.w * next.h;
    let mut chunk_magic = FLI_COPY;

    // Try FLI_BLACK for first frame only.
    if chunk_magic == FLI_COPY && prev.is_none() {
        if can_encode_fli_black(next) {
            chunk_size = 0;
            chunk_magic = FLI_BLACK;
        }
    }

    // Try FLI_LC.
    if chunk_magic == FLI_COPY && prev.is_some() {
        match encode_fli_lc(prev.unwrap(), next, w) {
            Ok(size) =>
                if size == 0 {
                    w.seek(SeekFrom::Start(pos0))?;
                    return Ok(0);
                } else if size < chunk_size {
                    chunk_size = size;
                    chunk_magic = FLI_LC;
                },

            Err(FlicError::ExceededLimit) => (),
            Err(e) => return Err(e),
        }

        if chunk_magic != FLI_LC {
            w.seek(SeekFrom::Start(pos0 + SIZE_OF_CHUNK as u64))?;
        }
    }

    // Try FLI_SS2, which has higher limits.
    if flic_magic == FLIHR_MAGIC && chunk_magic == FLI_COPY && prev.is_some() {
        match encode_fli_ss2(prev.unwrap(), next, w) {
            Ok(size) =>
                if size < chunk_size {
                    chunk_size = size;
                    chunk_magic = FLI_SS2;
                },

            Err(FlicError::ExceededLimit) => {},
            Err(e) => return Err(e),
        }

        if chunk_magic != FLI_SS2 {
            w.seek(SeekFrom::Start(pos0 + SIZE_OF_CHUNK as u64))?;
        }
    }

    // Try FLI_BRUN.
    if chunk_magic == FLI_COPY {
        match encode_fli_brun(next, w) {
            Ok(size) =>
                if size < chunk_size {
                    chunk_size = size;
                    chunk_magic = FLI_BRUN;
                },

            Err(FlicError::ExceededLimit) => (),
            Err(e) => return Err(e),
        }

        if chunk_magic != FLI_BRUN {
            w.seek(SeekFrom::Start(pos0 + SIZE_OF_CHUNK as u64))?;
        }
    }

    // Try FLI_COPY.
    if chunk_magic == FLI_COPY {
        chunk_size = encode_fli_copy(next, w)?;
        chunk_magic = FLI_COPY;
    }

    let pos1 = w.seek(SeekFrom::Current(0))?;
    assert_eq!(SIZE_OF_CHUNK + chunk_size, (pos1 - pos0) as usize);

    w.seek(SeekFrom::Start(pos0))?;
    if pos1 - pos0 > ::std::u32::MAX as u64 {
        return Err(FlicError::ExceededLimit);
    }

    w.write_u32::<LE>((pos1 - pos0) as u32)?;
    w.write_u16::<LE>(chunk_magic)?;
    w.seek(SeekFrom::Start(pos1))?;

    Ok((pos1 - pos0) as usize)
}

#[cfg(test)]
mod tests {
    use std::io::{Cursor,Seek,SeekFrom};
    use byteorder::LittleEndian as LE;
    use byteorder::ReadBytesExt;
    use ::Raster;
    use ::codec::FLI_COPY;
    use super::{FLIH_MAGIC,SIZE_OF_CHUNK,write_pixel_data};

    /// Test write_pixel_data output when reverting to FLI_COPY.
    #[test]
    fn test_write_pixel_data_fli_copy() {
        const SCREEN_W: usize = 1;
        const SCREEN_H: usize = 1;
        const NUM_COLS: usize = 256;
        let expected_size = SIZE_OF_CHUNK + SCREEN_W * SCREEN_H;

        let buf = [0xFF; SCREEN_W * SCREEN_H];
        let pal = [0; 3 * NUM_COLS];
        let next = Raster::new(SCREEN_W, SCREEN_H, &buf, &pal);
        let mut w = Cursor::new(Vec::new());

        let res = write_pixel_data(FLIH_MAGIC, None, &next, &mut w);
        assert_eq!(res.expect("size"), expected_size);

        w.seek(SeekFrom::Start(0)).expect("reset");
        assert_eq!(w.read_u32::<LE>().expect("size"), expected_size as u32);
        assert_eq!(w.read_u16::<LE>().expect("magic"), FLI_COPY);
    }
}