Skip to content

Commit

Permalink
add VeraImage flip functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yeastplume committed Jun 10, 2020
1 parent d574fdb commit b52c6d7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
34 changes: 33 additions & 1 deletion vera/src/imageset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ pub struct VeraImage {
pub foreground: u8,
/// Background colour on 1BPP mode
pub background: u8,
/// Also store hashes of each rotation
/// 90, 180, 270
pub rotation_hashes: [u64; 3],
}

impl Hash for VeraImage {
Expand Down Expand Up @@ -191,10 +194,11 @@ impl VeraImage {
depth: VeraPixelDepth::BPP8,
foreground: 0,
background: 0,
rotation_hashes: [0; 3],
}
}

/// push an pixel value
/// push a pixel value
pub fn push_pixel(&mut self, r: u8, g: u8, b: u8, pal_index: Option<u8>) {
self.data.push(VeraPixel {
r,
Expand Down Expand Up @@ -234,6 +238,34 @@ impl VeraImage {
self.hash(&mut hasher);
hasher.finish()
}

/// Return a new image from this one, horizontally flipped
pub fn h_flip(&self) -> VeraImage {
let mut ret = self.clone();
ret.data = vec![];
let width = self.width as usize;
let height = self.height as usize;
for j in 0..height {
for i in (0..width).rev() {
ret.data.push(self.data[j * width + i].clone());
}
}
ret
}

/// Return a new image from this one, vertically flipped
pub fn v_flip(&self) -> VeraImage {
let mut ret = self.clone();
ret.data = vec![];
let width = self.width as usize;
let height = self.height as usize;
for j in (0..height).rev() {
for i in 0..width {
ret.data.push(self.data[j * width + i].clone());
}
}
ret
}
}

#[derive(Clone, Copy, Debug)]
Expand Down
35 changes: 25 additions & 10 deletions vera/tests/imageset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,25 +344,40 @@ fn imageset_pal_64_4bpp() -> Result<(), Error> {
Ok(())
}

/*#[test]
fn imageset_crash() -> Result<(), Error> {
let test_png = include_bytes!("data/imageset/crash-test-pal.png");
#[test]
fn image_flip() -> Result<(), Error> {
let test_png = include_bytes!("data/imageset/indexed-4bpp-pal-64.png");
let pal_config = VeraPaletteLoadConfig {
direct_load: true,
include_defaults: false,
sort: false,
..VeraPaletteLoadConfig::default()
};
let palette = VeraPalette::derive_from_png("pal", test_png.to_vec(), &pal_config)?;
println!("{}", palette);

let imageset_png = include_bytes!("data/imageset/crash-test-imageset.png");
let mut set = VeraImageSet::new("imageset_1", 16, 16);
let mut set = VeraImageSet::new("imageset_1", 8, 8);
let config = VeraImageSetLoadConfig::default();
set.load_from_png(imageset_png.to_vec(), &config)?;

set.load_from_png(test_png.to_vec(), &config)?;
set.format_indices(&palette, VeraPixelDepth::BPP4)?;
println!("{}", set);

Ok(())
let orig = set.frame_data[2].clone();
println!("{}", orig);
let h_flipped = orig.h_flip();
println!("{}", h_flipped);
assert_eq!(h_flipped.data[7].pal_index, Some(7));
let v_flipped = h_flipped.v_flip();
println!("{}", v_flipped);
assert_eq!(v_flipped.data[63].pal_index, Some(7));

let orig = set.frame_data[3].clone();
println!("{}", orig);
let h_flipped = orig.h_flip();
println!("{}", h_flipped);
assert_eq!(h_flipped.data[7].pal_index, Some(2));
let v_flipped = h_flipped.v_flip();
println!("{}", v_flipped);
assert_eq!(v_flipped.data[63].pal_index, Some(2));

}*/
Ok(())
}

0 comments on commit b52c6d7

Please sign in to comment.