1 Commits
wip2 ... wip

Author SHA1 Message Date
3d834c1ff2 use image crate for tiles instead of custom type 2016-10-07 15:10:38 -07:00
3 changed files with 33 additions and 41 deletions

View File

@@ -22,11 +22,9 @@ fn main() {
let mut ega_vec = Vec::<u8>::new(); let mut ega_vec = Vec::<u8>::new();
file.read_to_end(&mut ega_vec).expect("Read EGA file"); file.read_to_end(&mut ega_vec).expect("Read EGA file");
let ega_page = ega::decode(&ega_vec, Compression::UNCOMPRESSED, Tiling::TILED(16)); let tiles = ega::decode(&ega_vec, Compression::UNCOMPRESSED, Tiling::TILED(16));
for (i, tilepixels) in ega_page.iter().enumerate() { for (i, tile) in tiles.iter().enumerate() {
let out_name = format!("out/{}.png", i); let out_name = format!("out/{}.png", i);
let out_file = std::fs::File::create(Path::new(&out_name)).expect("open out file"); tile.save(out_name).expect("save png");
let enc = image::png::PNGEncoder::new(out_file);
enc.encode(&tilepixels, 16, 16, image::ColorType::RGBA(8)).expect("write png");
} }
} }

View File

@@ -1,3 +1,7 @@
extern crate image;
use self::image::{ImageBuffer, Rgba};
static EGA_PALETTE: [[u8; 4]; 16] = [[0x00, 0x00, 0x00, 0x00], static EGA_PALETTE: [[u8; 4]; 16] = [[0x00, 0x00, 0x00, 0x00],
[0x00, 0x00, 0xAA, 0x00], [0x00, 0x00, 0xAA, 0x00],
[0x00, 0xAA, 0x00, 0x00], [0x00, 0xAA, 0x00, 0x00],
@@ -23,39 +27,30 @@ pub enum Compression {
pub enum Tiling { pub enum Tiling {
UNTILED, UNTILED,
TILED(u16) TILED(u32)
}
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) pub fn decode<'a>(buf: &[u8], compression: Compression, tiling: Tiling)
-> EgaPage { -> Vec<ImageBuffer<Rgba<u8>, Vec<u8>>> {
let out: Vec<u8>; let (tiledim, iter) = match tiling {
Tiling::TILED(tiledim) => (tiledim, buf.chunks((tiledim * tiledim / 2) as usize)),
out = match compression { Tiling::UNTILED => (buf.len() as u32, buf.chunks(buf.len()))
Compression::UNCOMPRESSED => buf.iter() };
.flat_map(|tile_byte| { match compression {
EGA_PALETTE[(tile_byte >> 4u8 & 0xF) as usize] Compression::UNCOMPRESSED =>
.into_iter() iter.map(|chunk| {
.chain(EGA_PALETTE[(tile_byte & 0xF) as usize] ImageBuffer::from_raw(tiledim,
.into_iter()) tiledim,
}) chunk.into_iter()
.map(|x| *x) .flat_map(|tile_byte| {
.collect(), 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!() _ => unimplemented!()
}; }
let tilesize = match tiling {
Tiling::TILED(tiledim) => 4 * tiledim * tiledim,
Tiling::UNTILED => out.len() as u16
};
EgaPage { data: out, tilesize: tilesize}
} }

View File

@@ -14,8 +14,7 @@ pub struct Tile {
pub val: u8, pub val: u8,
} }
pub fn get_tiles<R, F, T>(factory: &mut F) -> (//gfx::handle::Texture<R, T::Surface>, pub fn get_tiles<R, F, T>(factory: &mut F) -> gfx::handle::ShaderResourceView<R, T::View>
gfx::handle::ShaderResourceView<R, T::View>)
where R: gfx::Resources, where R: gfx::Resources,
F: gfx::Factory<R>, F: gfx::Factory<R>,
T: gfx::format::TextureFormat { T: gfx::format::TextureFormat {
@@ -24,11 +23,11 @@ pub fn get_tiles<R, F, T>(factory: &mut F) -> (//gfx::handle::Texture<R, T::Surf
.expect(&format!("failed opening tiles file: {}", filename)); .expect(&format!("failed opening tiles file: {}", filename));
let mut ega_bytes = Vec::new(); let mut ega_bytes = Vec::new();
file.read_to_end(&mut ega_bytes).expect("Read tiles file"); file.read_to_end(&mut ega_bytes).expect("Read tiles file");
let ega_page = ega::decode(&ega_bytes, ega::Compression::UNCOMPRESSED, ega::Tiling::TILED(16)); let tiles = ega::decode(&ega_bytes, ega::Compression::UNCOMPRESSED, ega::Tiling::TILED(16));
let tiles: Vec<&[u8]> = ega_page.iter().collect(); let tiles_raw: Vec<&[u8]> = tiles.iter().map(|t| &*t as &[u8]).collect();
let tex = factory.create_texture_const_u8::<T>(tex::Kind::D2Array(16, 16, 256, let tex = factory.create_texture_const_u8::<T>(tex::Kind::D2Array(16, 16, 256,
tex::AaMode::Single), tex::AaMode::Single),
&tiles) &tiles_raw)
.expect("create tile texture"); .expect("create tile texture");
tex.1 tex.1
} }