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
//! Methods for reading animated characters out of anim.mul/anim.idx
use byteorder::{LittleEndian, ReadBytesExt};
use color::{Color, Color16};
use mul_reader::MulReader;
use std::fs::{File};
use std::io::{Result, Cursor, SeekFrom, Seek, Read};
use std::path::Path;
use image::{Frames, Frame, Rgba, RgbaImage};
use num_rational::Ratio;

const PALETTE_SIZE: usize = 256;
const IMAGE_COMPLETE: u32 = 0x7FFF7FFF;

const OFFSET_MASK: i32 = (0x200 << 22) | (0x200 << 12);

pub struct Row {
    pub header: u32,
    pub image_data: Vec<u8>
}

impl Row {
    pub fn x_offset(&self, image_centre_x: i16) -> i32 {
        ((((self.header as i32 ^ OFFSET_MASK) >> 22) & 0x3FF) as i32 + image_centre_x as i32 - 0x200) as i32
    }

    pub fn y_offset(&self, image_centre_y: i16, height: u32) -> i32 {
        ((((self.header as i32 ^ OFFSET_MASK) >> 12) & 0x3FF) as i32 + image_centre_y as i32 + height as i32 - 0x200) as i32
    }
}

pub struct AnimFrame {
    pub image_centre_x: i16,
    pub image_centre_y: i16,
    pub width: u16,
    pub height: u16,
    pub data: Vec<Row>
}

pub struct AnimGroup {
    pub palette: [Color16; 256],
    pub frame_count: u32,
    pub frames: Vec<AnimFrame>
}

impl AnimGroup {
    pub fn to_frames(&self) -> Frames {
        Frames::new(self.frames.iter().map(|anim_frame| {
            // TODO: Figure out what to do with image_centre_x and y, and sort out offsets
            let mut buffer = RgbaImage::new(anim_frame.width as u32, anim_frame.height as u32);
            for row in &anim_frame.data {
                let x = row.x_offset(anim_frame.image_centre_x);
                let y = row.y_offset(anim_frame.image_centre_y, anim_frame.height as u32);
                for i in 0..row.image_data.len() {
                    let (r, g, b, a) = self.palette[row.image_data[i] as usize].to_rgba();
                    buffer.put_pixel(x as u32 + i as u32, y as u32, Rgba([r, g, b, a]));
                }
            }
            Frame::from_parts(buffer, 0, 0, Ratio::from_integer(0))
        }).collect())
    }
}

pub struct AnimReader<T: Read + Seek> {
    mul_reader: MulReader<T>
}

fn read_frame<T: Read + Seek>(reader: &mut T) -> Result<AnimFrame> {
    let image_centre_x = try!(reader.read_i16::<LittleEndian>());
    let image_centre_y = try!(reader.read_i16::<LittleEndian>());
    let width = try!(reader.read_u16::<LittleEndian>());
    let height = try!(reader.read_u16::<LittleEndian>());

    let mut data = vec![];
    loop {
        let header = try!(reader.read_u32::<LittleEndian>());
        if header == IMAGE_COMPLETE {
            break;
        }
        let run_length = header & 0xFFF;
        let mut image_data = vec![];
        for _i in 0..run_length {
            image_data.push(try!(reader.read_u8()));
        }
        data.push(Row {
            header,
            image_data
        });
    }

    // Read data
    Ok(AnimFrame {
        image_centre_x: image_centre_x,
        image_centre_y: image_centre_y,
        width: width,
        height: height,
        data: data
    })
}

impl AnimReader<File> {

    pub fn new(index_path: &Path, mul_path: &Path) -> Result<AnimReader<File>> {
        let mul_reader = try!(MulReader::new(index_path, mul_path));
        Ok(AnimReader {
            mul_reader: mul_reader
        })
    }
}

impl <T: Read + Seek> AnimReader<T> {

    pub fn from_mul(reader: MulReader<T>) -> AnimReader<T> {
        AnimReader {
            mul_reader: reader
        }
    }

    pub fn read(&mut self, id: u32) -> Result<AnimGroup> {

        let raw = try!(self.mul_reader.read(id));
        let mut reader = Cursor::new(raw.data);
        // Read the palette
        let mut palette = [0; PALETTE_SIZE];
        for i in 0..PALETTE_SIZE {
            palette[i] = try!(reader.read_u16::<LittleEndian>());
        }

        let frame_count = try!(reader.read_u32::<LittleEndian>());
        let mut frame_offsets = vec![];
        for _ in 0..frame_count {
            frame_offsets.push(try!(reader.read_u32::<LittleEndian>()));
        }

        let mut frames = vec![];
        for offset in frame_offsets {
            try!(reader.seek(SeekFrom::Start((PALETTE_SIZE as u32 * 2 + offset) as u64)));
            frames.push(try!(read_frame(&mut reader)));
        }

        Ok(AnimGroup {
            palette: palette,
            frame_count: frame_count,
            frames: frames
        })
    }
}