rename PixelGrid to Bitmap

This commit is contained in:
Vinzenz Schroeter 2024-10-14 22:42:45 +02:00
parent f64ce6e57e
commit bd0ecd77d2
23 changed files with 1037 additions and 1042 deletions

View file

@ -15,7 +15,7 @@ fn main() {
let connection = Connection::open(cli.destination)
.expect("could not connect to display");
let mut pixels = PixelGrid::max_sized();
let mut pixels = Bitmap::max_sized();
pixels.fill(true);
let command = Command::BitmapLinearWin(

View file

@ -34,7 +34,7 @@ fn main() {
}
}
fn iteration(field: PixelGrid) -> PixelGrid {
fn iteration(field: Bitmap) -> Bitmap {
let mut next = field.clone();
for x in 0..field.width() {
for y in 0..field.height() {
@ -51,7 +51,7 @@ fn iteration(field: PixelGrid) -> PixelGrid {
next
}
fn count_neighbors(field: &PixelGrid, x: i32, y: i32) -> i32 {
fn count_neighbors(field: &Bitmap, x: i32, y: i32) -> i32 {
let mut count = 0;
for nx in x - 1..=x + 1 {
for ny in y - 1..=y + 1 {
@ -78,8 +78,8 @@ fn count_neighbors(field: &PixelGrid, x: i32, y: i32) -> i32 {
count
}
fn make_random_field(probability: f64) -> PixelGrid {
let mut field = PixelGrid::max_sized();
fn make_random_field(probability: f64) -> Bitmap {
let mut field = Bitmap::max_sized();
let mut rng = rand::thread_rng();
let d = distributions::Bernoulli::new(probability).unwrap();
for x in 0..field.width() {

View file

@ -16,7 +16,7 @@ fn main() {
let connection = Connection::open(Cli::parse().destination)
.expect("could not connect to display");
let mut pixels = PixelGrid::max_sized();
let mut pixels = Bitmap::max_sized();
for x_offset in 0..usize::MAX {
pixels.fill(false);

View file

@ -28,7 +28,7 @@ fn main() {
// put all pixels in on state
if cli.enable_all {
let mut filled_grid = PixelGrid::max_sized();
let mut filled_grid = Bitmap::max_sized();
filled_grid.fill(true);
let command = BitmapLinearWin(

View file

@ -1,7 +1,7 @@
//! Example for how to use the WebSocket connection
use servicepoint::{
Command, CompressionCode, Connection, Grid, Origin, PixelGrid,
Command, CompressionCode, Connection, Grid, Origin, Bitmap,
};
fn main() {
@ -13,7 +13,7 @@ fn main() {
// use send_mut instead of send
connection.send_mut(Command::Clear).unwrap();
let mut pixels = PixelGrid::max_sized();
let mut pixels = Bitmap::max_sized();
pixels.fill(true);
// use send_mut instead of send

View file

@ -25,7 +25,7 @@ fn main() {
let connection = Connection::open(cli.destination)
.expect("could not connect to display");
let mut enabled_pixels = PixelGrid::new(PIXEL_WIDTH, PIXEL_HEIGHT);
let mut enabled_pixels = Bitmap::new(PIXEL_WIDTH, PIXEL_HEIGHT);
enabled_pixels.fill(true);
for x_offset in 0..PIXEL_WIDTH {

View file

@ -6,21 +6,21 @@ use crate::{BitVec, DataRef, Grid, SpBitVec, PIXEL_HEIGHT, PIXEL_WIDTH};
/// A grid of pixels stored in packed bytes.
#[derive(Debug, Clone, PartialEq)]
pub struct PixelGrid {
pub struct Bitmap {
width: usize,
height: usize,
bit_vec: SpBitVec,
}
impl PixelGrid {
/// Creates a new [PixelGrid] with the specified dimensions.
impl Bitmap {
/// Creates a new [Bitmap] with the specified dimensions.
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
///
/// returns: [PixelGrid] initialized to all pixels off
/// returns: [Bitmap] initialized to all pixels off
///
/// # Panics
///
@ -40,14 +40,14 @@ impl PixelGrid {
Self::new(PIXEL_WIDTH, PIXEL_HEIGHT)
}
/// Loads a [PixelGrid] with the specified dimensions from the provided data.
/// Loads a [Bitmap] with the specified dimensions from the provided data.
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
///
/// returns: [PixelGrid] that contains a copy of the provided data
/// returns: [Bitmap] that contains a copy of the provided data
///
/// # Panics
///
@ -64,12 +64,12 @@ impl PixelGrid {
}
}
/// Iterate over all cells in [PixelGrid].
/// Iterate over all cells in [Bitmap].
///
/// Order is equivalent to the following loop:
/// ```
/// # use servicepoint::{PixelGrid, Grid};
/// # let grid = PixelGrid::new(8,2);
/// # use servicepoint::{Bitmap, Grid};
/// # let grid = Bitmap::new(8,2);
/// for y in 0..grid.height() {
/// for x in 0..grid.width() {
/// grid.get(x, y);
@ -80,12 +80,12 @@ impl PixelGrid {
self.bit_vec.iter().by_refs()
}
/// Iterate over all cells in [PixelGrid] mutably.
/// Iterate over all cells in [Bitmap] mutably.
///
/// Order is equivalent to the following loop:
/// ```
/// # use servicepoint::{PixelGrid, Grid};
/// # let mut grid = PixelGrid::new(8,2);
/// # use servicepoint::{Bitmap, Grid};
/// # let mut grid = Bitmap::new(8,2);
/// # let value = false;
/// for y in 0..grid.height() {
/// for x in 0..grid.width() {
@ -96,8 +96,8 @@ impl PixelGrid {
///
/// # Example
/// ```
/// # use servicepoint::{PixelGrid, Grid};
/// # let mut grid = PixelGrid::new(8,2);
/// # use servicepoint::{Bitmap, Grid};
/// # let mut grid = Bitmap::new(8,2);
/// # let value = false;
/// for (index, mut pixel) in grid.iter_mut().enumerate() {
/// pixel.set(index % 2 == 0)
@ -107,17 +107,17 @@ impl PixelGrid {
self.bit_vec.iter_mut()
}
/// Iterate over all rows in [PixelGrid] top to bottom.
/// Iterate over all rows in [Bitmap] top to bottom.
pub fn iter_rows(&self) -> IterRows {
IterRows {
pixel_grid: self,
bitmap: self,
row: 0,
}
}
}
impl Grid<bool> for PixelGrid {
/// Sets the value of the specified position in the [PixelGrid].
impl Grid<bool> for Bitmap {
/// Sets the value of the specified position in the [Bitmap].
///
/// # Arguments
///
@ -139,7 +139,7 @@ impl Grid<bool> for PixelGrid {
self.bit_vec[x + y * self.width]
}
/// Sets the state of all pixels in the [PixelGrid].
/// Sets the state of all pixels in the [Bitmap].
///
/// # Arguments
///
@ -158,7 +158,7 @@ impl Grid<bool> for PixelGrid {
}
}
impl DataRef<u8> for PixelGrid {
impl DataRef<u8> for Bitmap {
fn data_ref_mut(&mut self) -> &mut [u8] {
self.bit_vec.as_raw_mut_slice()
}
@ -168,15 +168,15 @@ impl DataRef<u8> for PixelGrid {
}
}
impl From<PixelGrid> for Vec<u8> {
/// Turns a [PixelGrid] into the underlying [`Vec<u8>`].
fn from(value: PixelGrid) -> Self {
impl From<Bitmap> for Vec<u8> {
/// Turns a [Bitmap] into the underlying [`Vec<u8>`].
fn from(value: Bitmap) -> Self {
value.bit_vec.into()
}
}
pub struct IterRows<'t> {
pixel_grid: &'t PixelGrid,
bitmap: &'t Bitmap,
row: usize,
}
@ -184,24 +184,24 @@ impl<'t> Iterator for IterRows<'t> {
type Item = &'t BitSlice<u8, Msb0>;
fn next(&mut self) -> Option<Self::Item> {
if self.row >= self.pixel_grid.height {
if self.row >= self.bitmap.height {
return None;
}
let start = self.row * self.pixel_grid.width;
let end = start + self.pixel_grid.width;
let start = self.row * self.bitmap.width;
let end = start + self.bitmap.width;
self.row += 1;
Some(&self.pixel_grid.bit_vec[start..end])
Some(&self.bitmap.bit_vec[start..end])
}
}
#[cfg(test)]
mod tests {
use crate::{DataRef, Grid, PixelGrid};
use crate::{DataRef, Grid, Bitmap};
#[test]
fn fill() {
let mut grid = PixelGrid::new(8, 2);
let mut grid = Bitmap::new(8, 2);
assert_eq!(grid.data_ref(), [0x00, 0x00]);
grid.fill(true);
@ -213,7 +213,7 @@ mod tests {
#[test]
fn get_set() {
let mut grid = PixelGrid::new(8, 2);
let mut grid = Bitmap::new(8, 2);
assert!(!grid.get(0, 0));
assert!(!grid.get(1, 1));
@ -228,7 +228,7 @@ mod tests {
#[test]
fn load() {
let mut grid = PixelGrid::new(8, 3);
let mut grid = Bitmap::new(8, 3);
for x in 0..grid.width {
for y in 0..grid.height {
grid.set(x, y, (x + y) % 2 == 0);
@ -239,33 +239,33 @@ mod tests {
let data: Vec<u8> = grid.into();
let grid = PixelGrid::load(8, 3, &data);
let grid = Bitmap::load(8, 3, &data);
assert_eq!(grid.data_ref(), [0xAA, 0x55, 0xAA]);
}
#[test]
#[should_panic]
fn out_of_bounds_x() {
let vec = PixelGrid::new(8, 2);
let vec = Bitmap::new(8, 2);
vec.get(8, 1);
}
#[test]
#[should_panic]
fn out_of_bounds_y() {
let mut vec = PixelGrid::new(8, 2);
let mut vec = Bitmap::new(8, 2);
vec.set(1, 2, false);
}
#[test]
fn iter() {
let grid = PixelGrid::new(8, 2);
let grid = Bitmap::new(8, 2);
assert_eq!(16, grid.iter().count())
}
#[test]
fn iter_rows() {
let grid = PixelGrid::load(8, 2, &[0x04, 0x40]);
let grid = Bitmap::load(8, 2, &[0x04, 0x40]);
let mut iter = grid.iter_rows();
assert_eq!(iter.next().unwrap().count_ones(), 1);
@ -275,7 +275,7 @@ mod tests {
#[test]
fn iter_mut() {
let mut grid = PixelGrid::new(8, 2);
let mut grid = Bitmap::new(8, 2);
for (index, mut pixel) in grid.iter_mut().enumerate() {
pixel.set(index % 2 == 0);
}
@ -284,7 +284,7 @@ mod tests {
#[test]
fn data_ref_mut() {
let mut grid = PixelGrid::new(8, 2);
let mut grid = Bitmap::new(8, 2);
let data = grid.data_ref_mut();
data[1] = 0x0F;
assert!(grid.get(7, 1));

View file

@ -4,7 +4,7 @@ use crate::{
command_code::CommandCode,
compression::into_decompressed,
packet::{Header, Packet},
Brightness, BrightnessGrid, CompressionCode, Cp437Grid, Origin, PixelGrid,
Brightness, BrightnessGrid, CompressionCode, Cp437Grid, Origin, Bitmap,
Pixels, PrimitiveGrid, SpBitVec, Tiles, TILE_SIZE,
};
@ -105,10 +105,10 @@ pub enum Command {
/// # Examples
///
/// ```rust
/// # use servicepoint::{Command, CompressionCode, Grid, PixelGrid};
/// # use servicepoint::{Command, CompressionCode, Grid, Bitmap};
/// # let connection = servicepoint::Connection::Fake;
/// #
/// let mut pixels = PixelGrid::max_sized();
/// let mut pixels = Bitmap::max_sized();
/// // draw something to the pixels here
/// # pixels.set(2, 5, true);
///
@ -121,7 +121,7 @@ pub enum Command {
///
/// connection.send(command).expect("send failed");
/// ```
BitmapLinearWin(Origin<Pixels>, PixelGrid, CompressionCode),
BitmapLinearWin(Origin<Pixels>, Bitmap, CompressionCode),
/// Set the brightness of all tiles to the same value.
///
@ -337,7 +337,7 @@ impl Command {
Ok(Command::BitmapLinearWin(
Origin::new(tiles_x as usize * TILE_SIZE, pixels_y as usize),
PixelGrid::load(
Bitmap::load(
tile_w as usize * TILE_SIZE,
pixel_h as usize,
&payload,
@ -371,7 +371,7 @@ impl Command {
}
}
/// Helper method for Packets into `BitMapLinear*`-Commands
/// Helper method for Packets into `BitmapLinear*`-Commands
fn packet_into_linear_bitmap(
packet: Packet,
) -> Result<(SpBitVec, CompressionCode), TryFromPacketError> {
@ -496,7 +496,7 @@ mod tests {
origin::Pixels,
packet::{Header, Packet},
Brightness, BrightnessGrid, Command, CompressionCode, Origin,
PixelGrid, PrimitiveGrid,
Bitmap, PrimitiveGrid,
};
fn round_trip(original: Command) {
@ -589,7 +589,7 @@ mod tests {
));
round_trip(Command::BitmapLinearWin(
Origin::new(0, 0),
PixelGrid::max_sized(),
Bitmap::max_sized(),
compression,
));
}
@ -714,7 +714,7 @@ mod tests {
for compression in all_compressions().to_owned() {
let p: Packet = Command::BitmapLinearWin(
Origin::new(16, 8),
PixelGrid::new(8, 8),
Bitmap::new(8, 8),
compression,
)
.into();

View file

@ -3,13 +3,13 @@
/// # Examples
///
/// ```rust
/// # use servicepoint::{Command, CompressionCode, Origin, PixelGrid};
/// # use servicepoint::{Command, CompressionCode, Origin, Bitmap};
/// // create command without payload compression
/// # let pixels = PixelGrid::max_sized();
/// # let pixels = Bitmap::max_sized();
/// _ = Command::BitmapLinearWin(Origin::new(0, 0), pixels, CompressionCode::Uncompressed);
///
/// // create command with payload compressed with lzma and appropriate header flags
/// # let pixels = PixelGrid::max_sized();
/// # let pixels = Bitmap::max_sized();
/// _ = Command::BitmapLinearWin(Origin::new(0, 0), pixels, CompressionCode::Lzma);
/// ```
#[repr(u16)]

View file

@ -7,7 +7,7 @@
//! # Examples
//!
//! ```rust
//! use servicepoint::{Command, CompressionCode, Grid, PixelGrid};
//! use servicepoint::{Command, CompressionCode, Grid, Bitmap};
//!
//! let connection = servicepoint::Connection::open("127.0.0.1:2342")
//! .expect("connection failed");
@ -18,10 +18,10 @@
//! ```
//!
//! ```rust
//! # use servicepoint::{Command, CompressionCode, Grid, PixelGrid};
//! # use servicepoint::{Command, CompressionCode, Grid, Bitmap};
//! # let connection = servicepoint::Connection::open("127.0.0.1:2342").expect("connection failed");
//! // turn on all pixels in a grid
//! let mut pixels = PixelGrid::max_sized();
//! let mut pixels = Bitmap::max_sized();
//! pixels.fill(true);
//!
//! // create command to send pixels
@ -48,7 +48,7 @@ pub use crate::cp437::{CharGrid, Cp437Grid};
pub use crate::data_ref::DataRef;
pub use crate::grid::Grid;
pub use crate::origin::{Origin, Pixels, Tiles};
pub use crate::pixel_grid::PixelGrid;
pub use crate::bitmap::Bitmap;
pub use crate::primitive_grid::PrimitiveGrid;
type SpBitVec = BitVec<u8, Msb0>;
@ -64,7 +64,7 @@ mod data_ref;
mod grid;
mod origin;
pub mod packet;
mod pixel_grid;
mod bitmap;
mod primitive_grid;
/// size of a single tile in one dimension
@ -95,8 +95,8 @@ pub const TILE_HEIGHT: usize = 20;
/// # Examples
///
/// ```rust
/// # use servicepoint::{PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid};
/// let grid = PixelGrid::new(PIXEL_WIDTH, PIXEL_HEIGHT);
/// # use servicepoint::{PIXEL_HEIGHT, PIXEL_WIDTH, Bitmap};
/// let grid = Bitmap::new(PIXEL_WIDTH, PIXEL_HEIGHT);
/// ```
pub const PIXEL_WIDTH: usize = TILE_WIDTH * TILE_SIZE;
@ -105,8 +105,8 @@ pub const PIXEL_WIDTH: usize = TILE_WIDTH * TILE_SIZE;
/// # Examples
///
/// ```rust
/// # use servicepoint::{PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid};
/// let grid = PixelGrid::new(PIXEL_WIDTH, PIXEL_HEIGHT);
/// # use servicepoint::{PIXEL_HEIGHT, PIXEL_WIDTH, Bitmap};
/// let grid = Bitmap::new(PIXEL_WIDTH, PIXEL_HEIGHT);
/// ```
pub const PIXEL_HEIGHT: usize = TILE_HEIGHT * TILE_SIZE;
@ -119,10 +119,10 @@ pub const PIXEL_COUNT: usize = PIXEL_WIDTH * PIXEL_HEIGHT;
///
/// ```rust
/// # use std::time::Instant;
/// # use servicepoint::{Command, CompressionCode, FRAME_PACING, Origin, PixelGrid};
/// # use servicepoint::{Command, CompressionCode, FRAME_PACING, Origin, Bitmap};
/// # let connection = servicepoint::Connection::open("172.23.42.29:2342")
/// # .expect("connection failed");
/// # let pixels = PixelGrid::max_sized();
/// # let pixels = Bitmap::max_sized();
/// loop {
/// let start = Instant::now();
///

View file

@ -28,7 +28,7 @@ use std::mem::size_of;
use crate::compression::into_compressed;
use crate::{
command_code::CommandCode, Command, CompressionCode, Grid, Offset, Origin,
PixelGrid, Pixels, Tiles, TILE_SIZE,
Bitmap, Pixels, Tiles, TILE_SIZE,
};
/// A raw header.
@ -214,7 +214,7 @@ impl From<Command> for Packet {
}
impl Packet {
/// Helper method for `BitMapLinear*`-Commands into [Packet]
/// Helper method for `BitmapLinear*`-Commands into [Packet]
#[allow(clippy::cast_possible_truncation)]
fn bitmap_linear_into_packet(
command: CommandCode,
@ -239,7 +239,7 @@ impl Packet {
#[allow(clippy::cast_possible_truncation)]
fn bitmap_win_into_packet(
origin: Origin<Pixels>,
pixels: PixelGrid,
pixels: Bitmap,
compression: CompressionCode,
) -> Packet {
debug_assert_eq!(origin.x % 8, 0);