commit
0dbaf6b6e6
@ -0,0 +1,2 @@ |
|||||||
|
/target |
||||||
|
*~ |
||||||
@ -0,0 +1,186 @@ |
|||||||
|
# This file is automatically @generated by Cargo. |
||||||
|
# It is not intended for manual editing. |
||||||
|
version = 4 |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "anstream" |
||||||
|
version = "0.6.21" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" |
||||||
|
dependencies = [ |
||||||
|
"anstyle", |
||||||
|
"anstyle-parse", |
||||||
|
"anstyle-query", |
||||||
|
"anstyle-wincon", |
||||||
|
"colorchoice", |
||||||
|
"is_terminal_polyfill", |
||||||
|
"utf8parse", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "anstyle" |
||||||
|
version = "1.0.13" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "anstyle-parse" |
||||||
|
version = "0.2.7" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" |
||||||
|
dependencies = [ |
||||||
|
"utf8parse", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "anstyle-query" |
||||||
|
version = "1.1.5" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" |
||||||
|
dependencies = [ |
||||||
|
"windows-sys", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "anstyle-wincon" |
||||||
|
version = "3.0.11" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" |
||||||
|
dependencies = [ |
||||||
|
"anstyle", |
||||||
|
"once_cell_polyfill", |
||||||
|
"windows-sys", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "binvis" |
||||||
|
version = "0.1.0" |
||||||
|
dependencies = [ |
||||||
|
"clap", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "clap" |
||||||
|
version = "4.5.53" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8" |
||||||
|
dependencies = [ |
||||||
|
"clap_builder", |
||||||
|
"clap_derive", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "clap_builder" |
||||||
|
version = "4.5.53" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00" |
||||||
|
dependencies = [ |
||||||
|
"anstream", |
||||||
|
"anstyle", |
||||||
|
"clap_lex", |
||||||
|
"strsim", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "clap_derive" |
||||||
|
version = "4.5.49" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" |
||||||
|
dependencies = [ |
||||||
|
"heck", |
||||||
|
"proc-macro2", |
||||||
|
"quote", |
||||||
|
"syn", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "clap_lex" |
||||||
|
version = "0.7.6" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "colorchoice" |
||||||
|
version = "1.0.4" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "heck" |
||||||
|
version = "0.5.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "is_terminal_polyfill" |
||||||
|
version = "1.70.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "once_cell_polyfill" |
||||||
|
version = "1.70.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "proc-macro2" |
||||||
|
version = "1.0.104" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "9695f8df41bb4f3d222c95a67532365f569318332d03d5f3f67f37b20e6ebdf0" |
||||||
|
dependencies = [ |
||||||
|
"unicode-ident", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "quote" |
||||||
|
version = "1.0.42" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "strsim" |
||||||
|
version = "0.11.1" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "syn" |
||||||
|
version = "2.0.111" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
"quote", |
||||||
|
"unicode-ident", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "unicode-ident" |
||||||
|
version = "1.0.22" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "utf8parse" |
||||||
|
version = "0.2.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "windows-link" |
||||||
|
version = "0.2.1" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "windows-sys" |
||||||
|
version = "0.61.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" |
||||||
|
dependencies = [ |
||||||
|
"windows-link", |
||||||
|
] |
||||||
@ -0,0 +1,7 @@ |
|||||||
|
[package] |
||||||
|
name = "binvis" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2024" |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
clap = { version = "4.0", features = ["derive"] } |
||||||
@ -0,0 +1,629 @@ |
|||||||
|
#[allow(dead_code)] |
||||||
|
#[derive(Debug, PartialEq)] |
||||||
|
struct Range<T> { |
||||||
|
start: T, |
||||||
|
end: T, |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
fn min<T>(v1: T, v2: T) -> T where T: Ord { |
||||||
|
if v1 < v2 { |
||||||
|
return v1; |
||||||
|
} else { |
||||||
|
return v2; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
pub fn is_pixel_set_in_bitmap(x: u16, y: u16, memory: &[u8], width: u16, height: u16) -> bool { |
||||||
|
assert!(x < width); |
||||||
|
assert!(y < height); |
||||||
|
let index = y as usize * width as usize + x as usize; |
||||||
|
let byte_index = index / 8; |
||||||
|
let bit_index = (index % 8) as u8; |
||||||
|
assert!(byte_index < memory.len()); |
||||||
|
return (memory[byte_index] & (1 << bit_index)) == (1 << bit_index); |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
pub fn set_pixel_in_bitmap(x: u8, y: u8, memory: &mut [u8], width: u8, height: u8, value: bool) { |
||||||
|
assert!(x < width); |
||||||
|
assert!(y < height); |
||||||
|
let index = y as usize * width as usize + x as usize; |
||||||
|
let byte_index = index / 8; |
||||||
|
let bit_index = (index % 8) as u8; |
||||||
|
assert!(byte_index < memory.len()); |
||||||
|
if value { |
||||||
|
memory[byte_index] |= 1 << bit_index; |
||||||
|
} else { |
||||||
|
memory[byte_index] &= !(1 << bit_index); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
pub fn bitmap_size_bytes(width: u8, height: u8) -> usize { |
||||||
|
return ((width as usize) * (height as usize) + 7) / 8; |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
pub trait DrawObject { |
||||||
|
fn draw_into(self: &Self, tick: usize, memory_in: &[u8], target_width: u8, target_height: u8, target_memory: &mut [u8]); |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
#[derive(Debug, PartialEq)] |
||||||
|
pub struct DrawHeader { |
||||||
|
pos_x: u8, |
||||||
|
pos_y: u8, |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
#[derive(Debug, PartialEq)] |
||||||
|
pub struct DrawImage { |
||||||
|
header: DrawHeader, |
||||||
|
width: u8, |
||||||
|
height: u8, |
||||||
|
data: Range<usize>, |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
impl DrawImage { |
||||||
|
fn pixel_value(self: &Self, inner_x: u8, inner_y: u8, memory: &[u8]) -> bool { |
||||||
|
return is_pixel_set_in_bitmap(inner_x as u16, inner_y as u16, &memory[self.data.start..], self.width as u16, self.height as u16); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
impl DrawObject for DrawImage { |
||||||
|
fn draw_into(self: &Self, _tick: usize, memory_in: &[u8], target_width: u8, target_height: u8, target_memory: &mut [u8]) { |
||||||
|
let left = min(self.header.pos_x as usize, target_width as usize); |
||||||
|
let top = min(self.header.pos_y as usize, target_height as usize); |
||||||
|
let right = min(self.header.pos_x as usize + self.width as usize, target_width as usize); |
||||||
|
let bottom = min(self.header.pos_y as usize + self.height as usize, target_height as usize); |
||||||
|
for y in top..bottom { |
||||||
|
let inner_y = (y - self.header.pos_y as usize) as u8; |
||||||
|
for x in left..right { |
||||||
|
let inner_x = (x - self.header.pos_x as usize) as u8; |
||||||
|
set_pixel_in_bitmap(x as u8, y as u8, target_memory, target_width, target_height, self.pixel_value(inner_x, inner_y, memory_in)) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
#[derive(Debug, PartialEq)] |
||||||
|
pub struct DrawAnimation { |
||||||
|
header: DrawHeader, |
||||||
|
width: u8, |
||||||
|
height: u8, |
||||||
|
frame_count: u8, |
||||||
|
update_interval: usize, // starts at 1
|
||||||
|
data: Range<usize>, |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
impl DrawAnimation { |
||||||
|
fn pixel_value(self: &Self, frame_index: u8, inner_x: u8, inner_y: u8, memory: &[u8]) -> bool { |
||||||
|
let frame_size = ((self.width as usize) * (self.height as usize) + 7) / 8; |
||||||
|
let frame_start = self.data.start + frame_size * (frame_index as usize); |
||||||
|
return is_pixel_set_in_bitmap(inner_x as u16, inner_y as u16, &memory[frame_start..], self.width as u16, self.height as u16); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
impl DrawObject for DrawAnimation { |
||||||
|
fn draw_into(self: &Self, tick: usize, memory_in: &[u8], target_width: u8, target_height: u8, target_memory: &mut [u8]) { |
||||||
|
assert!(self.update_interval > 0); |
||||||
|
assert!(self.frame_count > 0); |
||||||
|
let animation_tick = tick / (self.update_interval as usize); |
||||||
|
let animation_index = animation_tick % (self.frame_count as usize); |
||||||
|
let left = min(self.header.pos_x as usize, target_width as usize); |
||||||
|
let top = min(self.header.pos_y as usize, target_height as usize); |
||||||
|
let right = min(self.header.pos_x as usize + self.width as usize, target_width as usize); |
||||||
|
let bottom = min(self.header.pos_y as usize + self.height as usize, target_height as usize); |
||||||
|
for y in top..bottom { |
||||||
|
let inner_y = (y - self.header.pos_y as usize) as u8; |
||||||
|
for x in left..right { |
||||||
|
let inner_x = (x - self.header.pos_x as usize) as u8; |
||||||
|
set_pixel_in_bitmap(x as u8, y as u8, target_memory, target_width, target_height, self.pixel_value(animation_index as u8, inner_x, inner_y, memory_in)) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
#[derive(Debug, PartialEq)] |
||||||
|
pub enum ScrollWrap { |
||||||
|
Restarting, |
||||||
|
Continuous, |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
#[derive(Debug, PartialEq)] |
||||||
|
pub enum HScrollDirection { |
||||||
|
Left, |
||||||
|
Right, |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
#[derive(Debug, PartialEq)] |
||||||
|
pub enum VScrollDirection { |
||||||
|
Up, |
||||||
|
Down, |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
#[derive(Debug, PartialEq)] |
||||||
|
pub struct DrawHScroll { |
||||||
|
header: DrawHeader, |
||||||
|
width: u8, |
||||||
|
height: u8, |
||||||
|
content_width: u16, |
||||||
|
scroll_speed: u8, |
||||||
|
wrap: ScrollWrap, |
||||||
|
direction: HScrollDirection, |
||||||
|
data: Range<usize>, |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
impl DrawHScroll { |
||||||
|
fn pixel_value(self: &Self, inner_x: u16, inner_y: u8, memory: &[u8]) -> bool { |
||||||
|
return is_pixel_set_in_bitmap(inner_x, inner_y as u16, &memory[self.data.start..], self.content_width, self.height as u16); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
impl DrawObject for DrawHScroll { |
||||||
|
fn draw_into(self: &Self, tick: usize, memory_in: &[u8], target_width: u8, target_height: u8, target_memory: &mut [u8]) { |
||||||
|
let left = min(self.header.pos_x as usize, target_width as usize); |
||||||
|
let top = min(self.header.pos_y as usize, target_height as usize); |
||||||
|
let right = min(self.header.pos_x as usize + self.width as usize, target_width as usize); |
||||||
|
let bottom = min(self.header.pos_y as usize + self.height as usize, target_height as usize); |
||||||
|
if self.width == 0 || self.content_width == 0 { |
||||||
|
return; |
||||||
|
} |
||||||
|
let mut offset_x = (tick * ((self.scroll_speed as usize + 1) / 8)) % (self.content_width as usize); |
||||||
|
if self.wrap == ScrollWrap::Restarting && (offset_x + self.width as usize) >= (self.content_width as usize) { |
||||||
|
offset_x = self.content_width as usize - self.width as usize; |
||||||
|
} |
||||||
|
if self.direction == HScrollDirection::Left { |
||||||
|
if self.width as u16 <= self.content_width { |
||||||
|
offset_x = self.content_width as usize - offset_x - 1; |
||||||
|
} else { |
||||||
|
offset_x = 0; |
||||||
|
} |
||||||
|
} else { |
||||||
|
if self.width as u16 > self.content_width { |
||||||
|
offset_x = 0; |
||||||
|
} |
||||||
|
} |
||||||
|
for y in top..bottom { |
||||||
|
let inner_y = (y - self.header.pos_y as usize) as u8; |
||||||
|
for x in left..right { |
||||||
|
let inner_x = (x + offset_x - self.header.pos_x as usize) as u16; |
||||||
|
if inner_x < self.content_width { |
||||||
|
set_pixel_in_bitmap(x as u8, y as u8, target_memory, target_width, target_height, self.pixel_value(inner_x, inner_y, memory_in)); |
||||||
|
} else { |
||||||
|
if self.wrap == ScrollWrap::Continuous { |
||||||
|
set_pixel_in_bitmap(x as u8, y as u8, target_memory, target_width, target_height, self.pixel_value(inner_x % self.content_width, inner_y, memory_in)); |
||||||
|
} else { |
||||||
|
set_pixel_in_bitmap(x as u8, y as u8, target_memory, target_width, target_height, false); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
#[derive(Debug, PartialEq)] |
||||||
|
pub struct DrawVScroll { |
||||||
|
header: DrawHeader, |
||||||
|
width: u8, |
||||||
|
height: u8, |
||||||
|
content_height: u16, |
||||||
|
scroll_speed: u8, |
||||||
|
wrap: ScrollWrap, |
||||||
|
direction: VScrollDirection, |
||||||
|
data: Range<usize>, |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
impl DrawVScroll { |
||||||
|
fn pixel_value(self: &Self, inner_x: u8, inner_y: u16, memory: &[u8]) -> bool { |
||||||
|
return is_pixel_set_in_bitmap(inner_x as u16, inner_y, &memory[self.data.start..], self.width as u16, self.content_height); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
impl DrawObject for DrawVScroll { |
||||||
|
fn draw_into(self: &Self, tick: usize, memory_in: &[u8], target_width: u8, target_height: u8, target_memory: &mut [u8]) { |
||||||
|
let left = min(self.header.pos_x as usize, target_width as usize); |
||||||
|
let top = min(self.header.pos_y as usize, target_height as usize); |
||||||
|
let right = min(self.header.pos_x as usize + self.width as usize, target_width as usize); |
||||||
|
let bottom = min(self.header.pos_y as usize + self.height as usize, target_height as usize); |
||||||
|
if self.height == 0 || self.content_height == 0 { |
||||||
|
return; |
||||||
|
} |
||||||
|
let mut offset_y = (tick * ((self.scroll_speed as usize + 1) / 8)) % (self.content_height as usize); |
||||||
|
if self.wrap == ScrollWrap::Restarting && (offset_y + self.height as usize) >= (self.content_height as usize) { |
||||||
|
offset_y = self.content_height as usize - self.height as usize; |
||||||
|
} |
||||||
|
if self.direction == VScrollDirection::Up { |
||||||
|
if self.height as u16 <= self.content_height { |
||||||
|
offset_y = self.content_height as usize - offset_y - 1; |
||||||
|
} else { |
||||||
|
offset_y = 0; |
||||||
|
} |
||||||
|
} else { |
||||||
|
if self.height as u16 > self.content_height { |
||||||
|
offset_y = 0; |
||||||
|
} |
||||||
|
} |
||||||
|
for y in top..bottom { |
||||||
|
let inner_y = (y + offset_y - self.header.pos_y as usize) as u16; |
||||||
|
if inner_y < self.content_height { |
||||||
|
for x in left..right { |
||||||
|
let inner_x = (x - self.header.pos_x as usize) as u8; |
||||||
|
set_pixel_in_bitmap(x as u8, y as u8, target_memory, target_width, target_height, self.pixel_value(inner_x, inner_y, memory_in)); |
||||||
|
} |
||||||
|
} else { |
||||||
|
if self.wrap == ScrollWrap::Continuous { |
||||||
|
for x in left..right { |
||||||
|
let inner_x = (x - self.header.pos_x as usize) as u8; |
||||||
|
set_pixel_in_bitmap(x as u8, y as u8, target_memory, target_width, target_height, self.pixel_value(inner_x, inner_y % self.content_height, memory_in)); |
||||||
|
} |
||||||
|
} else { |
||||||
|
for x in left..right { |
||||||
|
set_pixel_in_bitmap(x as u8, y as u8, target_memory, target_width, target_height, false); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
#[derive(Debug, PartialEq)] |
||||||
|
pub struct DrawLine { |
||||||
|
header: DrawHeader, |
||||||
|
end_x: u8, |
||||||
|
end_y: u8, |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
impl DrawObject for DrawLine { |
||||||
|
#[allow(unused_variables)] |
||||||
|
fn draw_into(self: &Self, tick: usize, memory_in: &[u8], target_width: u8, target_height: u8, target_memory: &mut [u8]) { |
||||||
|
unimplemented!(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
#[derive(Debug, PartialEq)] |
||||||
|
pub enum DrawEntry { |
||||||
|
Image(DrawImage), |
||||||
|
Animation(DrawAnimation), |
||||||
|
HScroll(DrawHScroll), |
||||||
|
VScroll(DrawVScroll), |
||||||
|
Line(DrawLine), |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
impl DrawObject for DrawEntry { |
||||||
|
fn draw_into(self: &Self, tick: usize, memory_in: &[u8], target_width: u8, target_height: u8, target_memory: &mut [u8]) { |
||||||
|
use DrawEntry::*; |
||||||
|
|
||||||
|
match self { |
||||||
|
Image(obj) => obj.draw_into(tick, &memory_in, target_width, target_height, target_memory), |
||||||
|
Animation(obj) => obj.draw_into(tick, &memory_in, target_width, target_height, target_memory), |
||||||
|
HScroll(obj) => obj.draw_into(tick, &memory_in, target_width, target_height, target_memory), |
||||||
|
VScroll(obj) => obj.draw_into(tick, &memory_in, target_width, target_height, target_memory), |
||||||
|
Line(obj) => obj.draw_into(tick, &memory_in, target_width, target_height, target_memory), |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
const MAGIC: [u8; 4] = [0x42, 0x4e, 0x17, 0xee]; |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
pub struct DrawEntryParser<'a> { |
||||||
|
memory: &'a [u8], |
||||||
|
index: usize, |
||||||
|
count: usize, |
||||||
|
pos: usize, |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
fn parse_hscroll_speed_and_flags(v: u8) -> (u8, ScrollWrap, HScrollDirection) { |
||||||
|
let speed = v & 0x3f; |
||||||
|
let wrap = if (v & 0x80) == 0x80 { ScrollWrap::Continuous } else { ScrollWrap::Restarting }; |
||||||
|
let direction = if (v & 0x40) == 0x40 { HScrollDirection::Right } else { HScrollDirection::Left }; |
||||||
|
return (speed, wrap, direction); |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
fn parse_vscroll_speed_and_flags(v: u8) -> (u8, ScrollWrap, VScrollDirection) { |
||||||
|
let speed = v & 0x3f; |
||||||
|
let wrap = if (v & 0x80) == 0x80 { ScrollWrap::Continuous } else { ScrollWrap::Restarting }; |
||||||
|
let direction = if (v & 0x40) == 0x40 { VScrollDirection::Down } else { VScrollDirection::Up }; |
||||||
|
return (speed, wrap, direction); |
||||||
|
} |
||||||
|
|
||||||
|
#[allow(dead_code)] |
||||||
|
impl<'a> DrawEntryParser<'a> { |
||||||
|
pub fn create(memory: &'a [u8]) -> Result<DrawEntryParser<'a>, u32> { |
||||||
|
if memory.len() < 5 { |
||||||
|
return Err(1); |
||||||
|
} |
||||||
|
if memory[0..4] != MAGIC { |
||||||
|
return Err(2); |
||||||
|
} |
||||||
|
let expected_count = memory[4] as usize; |
||||||
|
let mut parser = DrawEntryParser { |
||||||
|
memory: memory, |
||||||
|
index: 0, |
||||||
|
count: 0, |
||||||
|
pos: 5, |
||||||
|
}; |
||||||
|
let mut pos = 5; |
||||||
|
while pos < parser.memory.len() { |
||||||
|
if parser.count >= expected_count { |
||||||
|
break; |
||||||
|
} |
||||||
|
if pos + 3 > parser.memory.len() { |
||||||
|
break; |
||||||
|
} |
||||||
|
let t = parser.memory[pos + 0]; |
||||||
|
let _x = parser.memory[pos + 1] as usize; |
||||||
|
let _y = parser.memory[pos + 2] as usize; |
||||||
|
pos += 3; |
||||||
|
if t == 1 { // Image
|
||||||
|
if pos + 2 > parser.memory.len() { |
||||||
|
break; |
||||||
|
} |
||||||
|
let width = parser.memory[pos + 0] as usize; |
||||||
|
let height = parser.memory[pos + 1] as usize; |
||||||
|
let n = (width * height + 7) / 8; |
||||||
|
if pos + 2 + n > parser.memory.len() { |
||||||
|
break; |
||||||
|
} |
||||||
|
pos += n + 2; |
||||||
|
parser.count += 1; |
||||||
|
} else if t == 2 { // Animation
|
||||||
|
if pos + 4 > parser.memory.len() { |
||||||
|
break; |
||||||
|
} |
||||||
|
let width = parser.memory[pos + 0] as usize; |
||||||
|
let height = parser.memory[pos + 1] as usize; |
||||||
|
let frame_count = parser.memory[pos + 2] as usize; |
||||||
|
if frame_count == 0 { |
||||||
|
break; |
||||||
|
} |
||||||
|
let _update_interval = parser.memory[pos + 3] as usize; |
||||||
|
let n = ((width * height + 7) / 8) * frame_count; |
||||||
|
if pos + 4 + n > parser.memory.len() { |
||||||
|
break; |
||||||
|
} |
||||||
|
pos += n + 4; |
||||||
|
parser.count += 1; |
||||||
|
} else if t == 3 { // HScroll
|
||||||
|
if pos + 5 > parser.memory.len() { |
||||||
|
break; |
||||||
|
} |
||||||
|
let _width = parser.memory[pos + 0] as usize; |
||||||
|
let height = parser.memory[pos + 1] as usize; |
||||||
|
let content_width = (parser.memory[pos + 2] as u16) + ((parser.memory[pos + 3] as u16) << 8); |
||||||
|
let _speed_flags = parser.memory[4] as usize; |
||||||
|
let n = ((content_width as usize) * height + 7) / 8; |
||||||
|
if pos + 5 + n > parser.memory.len() { |
||||||
|
break; |
||||||
|
} |
||||||
|
pos += n + 5; |
||||||
|
} else if t == 4 { // VScroll
|
||||||
|
if pos + 5 > parser.memory.len() { |
||||||
|
break; |
||||||
|
} |
||||||
|
let width = parser.memory[pos + 0] as usize; |
||||||
|
let _height = parser.memory[pos + 1] as usize; |
||||||
|
let content_height = (parser.memory[pos + 2] as u16) + ((parser.memory[pos + 3] as u16) << 8); |
||||||
|
let _speed_flags = parser.memory[4] as usize; |
||||||
|
let n = (width * (content_height as usize) + 7) / 8; |
||||||
|
if pos + 5 + n > parser.memory.len() { |
||||||
|
break; |
||||||
|
} |
||||||
|
pos += n + 5; |
||||||
|
} else if t == 5 { // Line
|
||||||
|
if pos + 2 > parser.memory.len() { |
||||||
|
break; |
||||||
|
} |
||||||
|
pos += 2; |
||||||
|
} else { |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
return Ok(parser); |
||||||
|
} |
||||||
|
|
||||||
|
pub fn next(&mut self) -> Option<DrawEntry> { |
||||||
|
if self.index >= self.count { |
||||||
|
return None; |
||||||
|
} |
||||||
|
|
||||||
|
let t = self.memory[self.pos] as usize; |
||||||
|
let x = self.memory[self.pos + 1]; |
||||||
|
let y = self.memory[self.pos + 2]; |
||||||
|
let header = DrawHeader{pos_x: x, pos_y: y}; |
||||||
|
self.pos += 3; |
||||||
|
self.index += 1; |
||||||
|
|
||||||
|
if t == 1 { // Image
|
||||||
|
let width = self.memory[self.pos]; |
||||||
|
let height = self.memory[self.pos + 1]; |
||||||
|
let n = ((width as usize) * (height as usize) + 7) / 8; |
||||||
|
let range = Range{start: self.pos + 2, end: self.pos + 2 + n}; |
||||||
|
self.pos += n + 2; |
||||||
|
return Some(DrawEntry::Image(DrawImage{ |
||||||
|
header: header, |
||||||
|
width: width, |
||||||
|
height: height, |
||||||
|
data: range, |
||||||
|
})); |
||||||
|
} else if t == 2 { // Animation
|
||||||
|
let width = self.memory[self.pos]; |
||||||
|
let height = self.memory[self.pos + 1]; |
||||||
|
let frame_count = self.memory[self.pos + 2]; |
||||||
|
let update_interval = self.memory[self.pos + 3] as usize; |
||||||
|
let n = ((width as usize) * (height as usize) + 7) / 8 * (frame_count as usize); |
||||||
|
let range = Range{start: self.pos + 4, end: self.pos + 4 + n}; |
||||||
|
self.pos += n + 4; |
||||||
|
return Some(DrawEntry::Animation(DrawAnimation{ |
||||||
|
header: header, |
||||||
|
width: width, |
||||||
|
height: height, |
||||||
|
frame_count: frame_count, |
||||||
|
update_interval: update_interval, |
||||||
|
data: range, |
||||||
|
})); |
||||||
|
} else if t == 3 { // HScroll
|
||||||
|
let width = self.memory[self.pos + 0]; |
||||||
|
let height = self.memory[self.pos + 1]; |
||||||
|
let content_width = (self.memory[self.pos + 2] as u16) + ((self.memory[self.pos + 3] as u16) << 8); |
||||||
|
let (speed, wrap, direction) = parse_hscroll_speed_and_flags(self.memory[4]); |
||||||
|
let n = ((content_width as usize) * (height as usize) + 7) / 8; |
||||||
|
let range = Range{start: self.pos + 5, end: self.pos + 5 + n}; |
||||||
|
self.pos += n + 5; |
||||||
|
return Some(DrawEntry::HScroll(DrawHScroll{ |
||||||
|
header: header, |
||||||
|
width: width, |
||||||
|
height: height, |
||||||
|
content_width: content_width, |
||||||
|
scroll_speed: speed, |
||||||
|
wrap: wrap, |
||||||
|
direction: direction, |
||||||
|
data: range, |
||||||
|
})); |
||||||
|
} else if t == 4 { // VScroll
|
||||||
|
let width = self.memory[self.pos + 0]; |
||||||
|
let height = self.memory[self.pos + 1]; |
||||||
|
let content_height = (self.memory[self.pos + 2] as u16) + ((self.memory[self.pos + 3] as u16) << 8); |
||||||
|
let (speed, wrap, direction) = parse_vscroll_speed_and_flags(self.memory[4]); |
||||||
|
let n = ((width as usize) * (content_height as usize) + 7) / 8; |
||||||
|
let range = Range{start: self.pos + 5, end: self.pos + 5 + n}; |
||||||
|
self.pos += n + 5; |
||||||
|
return Some(DrawEntry::VScroll(DrawVScroll{ |
||||||
|
header: header, |
||||||
|
width: width, |
||||||
|
height: height, |
||||||
|
content_height: content_height, |
||||||
|
scroll_speed: speed, |
||||||
|
wrap: wrap, |
||||||
|
direction: direction, |
||||||
|
data: range, |
||||||
|
})); |
||||||
|
} else if t == 5 { // Line
|
||||||
|
let end_x = self.memory[self.pos + 0]; |
||||||
|
let end_y = self.memory[self.pos + 1]; |
||||||
|
self.pos += 2; |
||||||
|
return Some(DrawEntry::Line(DrawLine{ |
||||||
|
header: header, |
||||||
|
end_x: end_x, |
||||||
|
end_y: end_y, |
||||||
|
})); |
||||||
|
} else { |
||||||
|
assert!(t == 1 || t == 2 || t == 3 || t == 4 || t == 5, "Invalid type byte for a DrawEntry encountered."); |
||||||
|
unreachable!(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn reset(&mut self) { |
||||||
|
self.index = 0; |
||||||
|
self.pos = 5; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[cfg(test)] |
||||||
|
mod tests { |
||||||
|
use super::*; |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn empty() { |
||||||
|
let x : [u8; 5] = [0x42, 0x4e, 0x17, 0xee, 0]; |
||||||
|
let p = DrawEntryParser::create(&x).unwrap(); |
||||||
|
assert_eq!(p.count, 0); |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn simple_image_4x4() { |
||||||
|
let memory : [u8; 12] = [0x42, 0x4e, 0x17, 0xee, 1, 1, 10, 15, 4, 4, 1, 0]; |
||||||
|
let mut p = DrawEntryParser::create(&memory).unwrap(); |
||||||
|
assert_eq!(p.count, 1); |
||||||
|
let entry = p.next().unwrap(); |
||||||
|
if let DrawEntry::Image(img) = entry { |
||||||
|
assert_eq!(img.header.pos_x, 10); |
||||||
|
assert_eq!(img.header.pos_y, 15); |
||||||
|
assert_eq!(img.width, 4); |
||||||
|
assert_eq!(img.height, 4); |
||||||
|
assert_eq!(img.pixel_value(0, 0, &memory), true); |
||||||
|
assert_eq!(img.pixel_value(0, 1, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(0, 2, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(0, 3, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(1, 0, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(1, 1, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(1, 2, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(1, 3, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(2, 0, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(2, 1, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(2, 2, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(2, 3, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(3, 0, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(3, 1, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(3, 2, &memory), false); |
||||||
|
assert_eq!(img.pixel_value(3, 3, &memory), false); |
||||||
|
} else { |
||||||
|
assert!(false); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn draw_bin_sample_1() { |
||||||
|
let mut buffer = Vec::<u8>::new(); |
||||||
|
buffer.resize(120 * 60, 0); |
||||||
|
let input = include_bytes!("test1.bin"); |
||||||
|
let mut p = DrawEntryParser::create(input).unwrap(); |
||||||
|
assert!(p.count > 0); |
||||||
|
for frame_counter in 0..65535u32 { |
||||||
|
while let Some(entry) = p.next() { |
||||||
|
entry.draw_into(frame_counter as usize, input, 120, 60, &mut buffer); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn draw_bin_sample_2() { |
||||||
|
let mut buffer = Vec::<u8>::new(); |
||||||
|
buffer.resize(120 * 60, 0); |
||||||
|
let input = include_bytes!("test2.bin"); |
||||||
|
let mut p = DrawEntryParser::create(input).unwrap(); |
||||||
|
assert!(p.count > 0); |
||||||
|
for frame_counter in 0..65535u32 { |
||||||
|
while let Some(entry) = p.next() { |
||||||
|
entry.draw_into(frame_counter as usize, input, 120, 60, &mut buffer); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn draw_bin_sample_3() { |
||||||
|
let mut buffer = Vec::<u8>::new(); |
||||||
|
buffer.resize(120 * 60, 0); |
||||||
|
let input = include_bytes!("test3.bin"); |
||||||
|
let mut p = DrawEntryParser::create(input).unwrap(); |
||||||
|
assert!(p.count > 0); |
||||||
|
for frame_counter in 0..65535u32 { |
||||||
|
while let Some(entry) = p.next() { |
||||||
|
entry.draw_into(frame_counter as usize, input, 120, 60, &mut buffer); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,89 @@ |
|||||||
|
mod binfmt; |
||||||
|
|
||||||
|
use crate::binfmt::DrawObject; |
||||||
|
use crate::binfmt::DrawEntryParser; |
||||||
|
use clap::Parser; |
||||||
|
use std::fs::File; |
||||||
|
use std::io::Read; |
||||||
|
use std::io::Write; |
||||||
|
use std::{thread, time}; |
||||||
|
|
||||||
|
#[derive(Parser)] |
||||||
|
struct Cli { |
||||||
|
filename: std::path::PathBuf, |
||||||
|
} |
||||||
|
|
||||||
|
fn flush() { |
||||||
|
let _ = std::io::stdout().flush(); |
||||||
|
} |
||||||
|
|
||||||
|
fn cls() { |
||||||
|
print!("\x1b[1J\x1b[1;1H"); |
||||||
|
} |
||||||
|
|
||||||
|
const WIDTH : i32 = 120; |
||||||
|
const HEIGHT : i32 = 60; |
||||||
|
|
||||||
|
fn main() { |
||||||
|
let args = Cli::parse(); |
||||||
|
|
||||||
|
let mut f = File::open(args.filename).unwrap(); |
||||||
|
let mut buffer = Vec::new(); |
||||||
|
f.read_to_end(&mut buffer).unwrap(); |
||||||
|
|
||||||
|
let mut parser = DrawEntryParser::create(&buffer).unwrap(); |
||||||
|
|
||||||
|
let interval = time::Duration::from_millis(25); |
||||||
|
|
||||||
|
let mut frame_counter = 0; |
||||||
|
let mut memory = vec![0 as u8; (WIDTH * HEIGHT / 8) as usize]; |
||||||
|
loop { |
||||||
|
cls(); |
||||||
|
parser.reset(); |
||||||
|
memory.fill(0); |
||||||
|
while let Some(entry) = parser.next() { |
||||||
|
entry.draw_into(frame_counter, &buffer, WIDTH as u8, HEIGHT as u8, &mut memory); |
||||||
|
} |
||||||
|
print!("\u{250c}"); |
||||||
|
for _ in 0..WIDTH { |
||||||
|
print!("\u{2500}") |
||||||
|
} |
||||||
|
print!("\u{2510}\n"); |
||||||
|
for y in 0..((HEIGHT+1)/2) { |
||||||
|
print!("\u{2502}"); |
||||||
|
for x in 0..WIDTH { |
||||||
|
let pidx_upper = (2 * y + 0) * WIDTH + x; |
||||||
|
let pidx_lower = (2 * y + 1) * WIDTH + x; |
||||||
|
let pbyte_upper = (pidx_upper / 8) as usize; |
||||||
|
let pbyte_lower = (pidx_lower / 8) as usize; |
||||||
|
let pbit_upper = (pidx_upper % 8) as u8; |
||||||
|
let pbit_lower = (pidx_lower % 8) as u8; |
||||||
|
let pval_upper = (memory[pbyte_upper] & (1 << pbit_upper)) == (1 << pbit_upper); |
||||||
|
let pval_lower = if (2 * y + 1) < HEIGHT { |
||||||
|
(memory[pbyte_lower] & (1 << pbit_lower)) == (1 << pbit_lower) |
||||||
|
} else { |
||||||
|
false |
||||||
|
}; |
||||||
|
if pval_upper && pval_lower { |
||||||
|
print!("\u{2588}"); |
||||||
|
} else if pval_upper { |
||||||
|
print!("\u{1fb0e}"); |
||||||
|
} else if pval_lower { |
||||||
|
print!("\u{2584}"); |
||||||
|
} else { |
||||||
|
print!(" "); |
||||||
|
} |
||||||
|
} |
||||||
|
print!("\u{2502}\n"); |
||||||
|
} |
||||||
|
print!("\u{2514}"); |
||||||
|
for _ in 0..WIDTH { |
||||||
|
print!("\u{2500}") |
||||||
|
} |
||||||
|
print!("\u{2518}\n"); |
||||||
|
println!("Frame counter = {0}", frame_counter); |
||||||
|
flush(); |
||||||
|
thread::sleep(interval); |
||||||
|
frame_counter += 1; |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue