diff --git a/src/bin/tileview.rs b/src/bin/tileview.rs index 8c21a10..f3c9ff8 100644 --- a/src/bin/tileview.rs +++ b/src/bin/tileview.rs @@ -22,11 +22,9 @@ fn main() { let mut ega_vec = Vec::::new(); file.read_to_end(&mut ega_vec).expect("Read EGA file"); - let ega_page = ega::decode(&ega_vec, Compression::UNCOMPRESSED, Tiling::TILED(16)); - for (i, tilepixels) in ega_page.iter().enumerate() { + let tiles = ega::decode(&ega_vec, Compression::UNCOMPRESSED, Tiling::TILED(16)); + for (i, tile) in tiles.iter().enumerate() { let out_name = format!("out/{}.png", i); - let out_file = std::fs::File::create(Path::new(&out_name)).expect("open out file"); - let enc = image::png::PNGEncoder::new(out_file); - enc.encode(&tilepixels, 16, 16, image::ColorType::RGBA(8)).expect("write png"); + tile.save(out_name).expect("save png"); } } diff --git a/src/ega.rs b/src/ega.rs index 54c9710..21d8148 100644 --- a/src/ega.rs +++ b/src/ega.rs @@ -1,3 +1,7 @@ +extern crate image; + +use self::image::{ImageBuffer, Rgba}; + static EGA_PALETTE: [[u8; 4]; 16] = [[0x00, 0x00, 0x00, 0x00], [0x00, 0x00, 0xAA, 0x00], [0x00, 0xAA, 0x00, 0x00], @@ -23,39 +27,30 @@ pub enum Compression { pub enum Tiling { UNTILED, - TILED(u16) -} - -pub struct EgaPage { - pub data: Vec, - pub tilesize: u16, -} - -impl EgaPage { - pub fn iter<'a>(&'a self) -> impl Iterator { - self.data.chunks(self.tilesize as usize) - } + TILED(u32) } pub fn decode<'a>(buf: &[u8], compression: Compression, tiling: Tiling) - -> EgaPage { - let out: Vec; - - 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(), + -> Vec, Vec>> { + let (tiledim, iter) = match tiling { + Tiling::TILED(tiledim) => (tiledim, buf.chunks((tiledim * tiledim / 2) as usize)), + Tiling::UNTILED => (buf.len() as u32, buf.chunks(buf.len())) + }; + match compression { + Compression::UNCOMPRESSED => + iter.map(|chunk| { + ImageBuffer::from_raw(tiledim, + tiledim, + chunk.into_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()) + .expect("decode ega") + }).collect(), _ => unimplemented!() - }; - let tilesize = match tiling { - Tiling::TILED(tiledim) => 4 * tiledim * tiledim, - Tiling::UNTILED => out.len() as u16 - }; - EgaPage { data: out, tilesize: tilesize} + } } diff --git a/src/tile.rs b/src/tile.rs index 882003c..170f019 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -14,8 +14,7 @@ pub struct Tile { pub val: u8, } -pub fn get_tiles(factory: &mut F) -> (//gfx::handle::Texture, - gfx::handle::ShaderResourceView) +pub fn get_tiles(factory: &mut F) -> gfx::handle::ShaderResourceView where R: gfx::Resources, F: gfx::Factory, T: gfx::format::TextureFormat { @@ -24,11 +23,11 @@ pub fn get_tiles(factory: &mut F) -> (//gfx::handle::Texture = ega_page.iter().collect(); + let tiles = ega::decode(&ega_bytes, ega::Compression::UNCOMPRESSED, ega::Tiling::TILED(16)); + let tiles_raw: Vec<&[u8]> = tiles.iter().map(|t| &*t as &[u8]).collect(); let tex = factory.create_texture_const_u8::(tex::Kind::D2Array(16, 16, 256, tex::AaMode::Single), - &tiles) + &tiles_raw) .expect("create tile texture"); tex.1 }