factor out ega loader

This commit is contained in:
2016-09-19 03:00:54 -07:00
parent fd9d1cca13
commit 067e8906be
3 changed files with 75 additions and 35 deletions

61
src/ega.rs Normal file
View File

@@ -0,0 +1,61 @@
static EGA_PALETTE: [[u8; 4]; 16] = [[0x00, 0x00, 0x00, 0x00],
[0x00, 0x00, 0xAA, 0x00],
[0x00, 0xAA, 0x00, 0x00],
[0x00, 0xAA, 0xAA, 0x00],
[0xAA, 0x00, 0x00, 0x00],
[0xAA, 0x00, 0xAA, 0x00],
[0xAA, 0x55, 0x00, 0x00],
[0xAA, 0xAA, 0xAA, 0x00],
[0x55, 0x55, 0x55, 0x00],
[0x55, 0x55, 0xFF, 0x00],
[0x55, 0xFF, 0x55, 0x00],
[0x55, 0xFF, 0xFF, 0x00],
[0xFF, 0x55, 0x55, 0x00],
[0xFF, 0x55, 0xFF, 0x00],
[0xFF, 0xFF, 0x55, 0x00],
[0xFF, 0xFF, 0xFF, 0x00]];
pub enum Compression {
UNCOMPRESSED,
RLE,
LZW
}
pub enum Tiling {
UNTILED,
TILED(u16)
}
pub struct EgaPage {
pub data: Vec<u8>,
pub tilesize: u16,
}
impl EgaPage {
pub fn iter<'a>(&'a self) -> impl Iterator<Item=&'a [u8]> {
self.data.chunks(self.tilesize as usize)
}
}
pub fn decode<'a>(buf: &[u8], compression: Compression, tiling: Tiling)
-> EgaPage {
let out: Vec<u8>;
out = match compression {
Compression::UNCOMPRESSED => buf.iter()
.flat_map(|tile_byte| {
EGA_PALETTE[(tile_byte >> 4u8 & 0xF) as usize]
.into_iter()
.chain(EGA_PALETTE[(tile_byte & 0xF) as usize]
.into_iter())
})
.map(|x| *x)
.collect(),
_ => unimplemented!()
};
let tilesize = match tiling {
Tiling::TILED(tiledim) => 4 * tiledim * tiledim,
Tiling::UNTILED => out.len() as u16
};
EgaPage { data: out, tilesize: tilesize}
}