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
extern crate inflate;
extern crate num_traits;

use components::header::{Header, SecondHeader, Channels};
use components::metadata::Metadata;

pub use decoder::Decoder;

mod decoder;
mod numbers;
mod maniac;

pub mod components;
pub mod error;

pub struct Flif {
    pub info: FlifInfo,
    image_data: DecodingImage,
}

impl Flif {
    pub fn get_raw_pixels(&self) -> Vec<u8> {
        let mut data = Vec::new();

        for y in 0..self.image_data.height {
            for x in 0..self.image_data.width {
                let vals = self.image_data.get_vals(y, x);
                for channel in 0..self.info.header.channels as usize {
                    data.push(vals[channel] as u8)
                }
            }
        }
        
        data
    }
}

pub struct FlifInfo {
    pub header: Header,
    pub metadata: Vec<Metadata>,
    pub second_header: SecondHeader,
}

pub type ColorValue = i16;

struct DecodingImage {
    pub height: usize,
    pub width: usize,
    pub channels: Channels,
    data: Vec<[ColorValue; 4]>,
}

impl DecodingImage {
    pub fn new(info: &FlifInfo) -> DecodingImage {
        DecodingImage {
            height: info.header.height,
            width: info.header.width,
            channels: info.header.channels,
            data: vec![[0, 0, 0, 0]; info.header.height * info.header.width],
        }
    }

    pub fn get_val(&self, row: usize, col: usize, channel: usize) -> ColorValue {
        self.data[(self.width * row) + col][channel]
    }

    pub fn set_val(&mut self, row: usize, col: usize, channel: usize, value: ColorValue) {
        self.data[(self.width * row) + col][channel] = value;
    }

    pub fn get_vals(&self, row: usize, col: usize) -> &[ColorValue; 4] {
        &self.data[(self.width * row) + col]
    }

    pub fn get_vals_mut(&mut self, row: usize, col: usize) -> &mut [ColorValue; 4] {
        &mut self.data[(self.width * row) + col]
    }
}