1 Commits
master ... 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();
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");
}
}

View File

@@ -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<u8>,
pub tilesize: u16,
}
impl EgaPage {
pub fn iter<'a>(&'a self) -> impl Iterator<Item=&'a [u8]> {
self.data.chunks(self.tilesize as usize)
}
TILED(u32)
}
pub fn decode<'a>(buf: &[u8], compression: Compression, tiling: Tiling)
-> EgaPage {
let out: Vec<u8>;
out = match compression {
Compression::UNCOMPRESSED => buf.iter()
-> Vec<ImageBuffer<Rgba<u8>, Vec<u8>>> {
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(),
}).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}
}
}

View File

@@ -14,8 +14,7 @@ pub struct Tile {
pub val: u8,
}
pub fn get_tiles<R, F, T>(factory: &mut F) -> (//gfx::handle::Texture<R, T::Surface>,
gfx::handle::ShaderResourceView<R, T::View>)
pub fn get_tiles<R, F, T>(factory: &mut F) -> gfx::handle::ShaderResourceView<R, T::View>
where R: gfx::Resources,
F: gfx::Factory<R>,
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));
let mut ega_bytes = Vec::new();
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: Vec<&[u8]> = 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::<T>(tex::Kind::D2Array(16, 16, 256,
tex::AaMode::Single),
&tiles)
&tiles_raw)
.expect("create tile texture");
tex.1
}