switch size data types to u16

The binary protocol is limited to using u16's so limit everything to that.
This commit is contained in:
Johann150
2023-12-28 16:18:50 +01:00
parent f9c13ad78d
commit 04f54936b9
8 changed files with 32 additions and 27 deletions

View File

@@ -129,7 +129,7 @@ impl<'a: 'b, 'b> ArgHandler<'a> {
/// Get the image size.
/// Use the given default value if not set.
pub fn size(&self, def: Option<(u32, u32)>) -> (u32, u32) {
pub fn size(&self, def: Option<(u16, u16)>) -> (u16, u16) {
(
self.matches
.value_of("width")
@@ -143,15 +143,15 @@ impl<'a: 'b, 'b> ArgHandler<'a> {
}
/// Get the image offset.
pub fn offset(&self) -> (u32, u32) {
pub fn offset(&self) -> (u16, u16) {
(
self.matches
.value_of("x")
.map(|x| x.parse::<u32>().expect("Invalid X offset"))
.map(|x| x.parse::<u16>().expect("Invalid X offset"))
.unwrap_or(0),
self.matches
.value_of("y")
.map(|y| y.parse::<u32>().expect("Invalid Y offset"))
.map(|y| y.parse::<u16>().expect("Invalid Y offset"))
.unwrap_or(0),
)
}

View File

@@ -28,7 +28,7 @@ impl ImageManager {
}
/// Instantiate the image manager, and load the images from the given paths.
pub fn load(paths: &[&str], size: (u32, u32)) -> ImageManager {
pub fn load(paths: &[&str], size: (u16, u16)) -> ImageManager {
// Show a status message
println!("Load and process {} image(s)...", paths.len());
@@ -84,7 +84,7 @@ impl ImageManager {
}
/// Load the image at the given path, and size it correctly
fn load_image(path: &str, size: (u32, u32)) -> DynamicImage {
fn load_image(path: &str, size: (u16, u16)) -> DynamicImage {
// Create a path instance
let path = Path::new(&path);
@@ -97,5 +97,5 @@ fn load_image(path: &str, size: (u32, u32)) -> DynamicImage {
let image = image::open(&path).unwrap();
// Resize the image to fit the screen
image.resize_exact(size.0, size.1, FilterType::Gaussian)
image.resize_exact(size.0 as u32, size.1 as u32, FilterType::Gaussian)
}

View File

@@ -55,7 +55,7 @@ fn start<'a>(arg_handler: &ArgHandler<'a>) {
}
/// Gather important facts about the host.
fn gather_host_facts(arg_handler: &ArgHandler) -> Result<(u32, u32), Error> {
fn gather_host_facts(arg_handler: &ArgHandler) -> Result<(u16, u16), Error> {
// Set up a client, and get the screen size
let size = Client::connect(arg_handler.host().to_string(), false)?.read_screen_size()?;

View File

@@ -31,7 +31,12 @@ impl Handle {
/// Push an image update.
pub fn update_image(&self, full_image: &mut DynamicImage) {
// Crop the image to the area
let image = full_image.crop(self.area.x, self.area.y, self.area.w, self.area.h);
let image = full_image.crop(
self.area.x as u32,
self.area.y as u32,
self.area.w as u32,
self.area.h as u32,
);
// Push a new image to the thread
// TODO: return this result

View File

@@ -11,7 +11,7 @@ use rect::Rect;
pub struct Painter {
client: Option<Client>,
area: Rect,
offset: (u32, u32),
offset: (u16, u16),
image: Option<DynamicImage>,
}
@@ -20,7 +20,7 @@ impl Painter {
pub fn new(
client: Option<Client>,
area: Rect,
offset: (u32, u32),
offset: (u16, u16),
image: Option<DynamicImage>,
) -> Painter {
Painter {
@@ -62,7 +62,7 @@ impl Painter {
}
// Get the pixel at this location
let pixel = image.get_pixel(x, y);
let pixel = image.get_pixel(x as u32, y as u32);
// Get the channels
let channels = pixel.channels();

View File

@@ -15,8 +15,8 @@ pub struct Canvas {
host: String,
painter_count: usize,
painter_handles: Vec<Handle>,
size: (u32, u32),
offset: (u32, u32),
size: (u16, u16),
offset: (u16, u16),
}
impl Canvas {
@@ -24,8 +24,8 @@ impl Canvas {
pub fn new(
host: &str,
painter_count: usize,
size: (u32, u32),
offset: (u32, u32),
size: (u16, u16),
offset: (u16, u16),
binary: bool,
) -> Canvas {
// Initialize the object
@@ -52,10 +52,10 @@ impl Canvas {
// Spawn some painters
for i in 0..self.painter_count {
// Determine the slice width
let width = self.size.0 / (self.painter_count as u32);
let width = self.size.0 / (self.painter_count as u16);
// Define the area to paint per thread
let painter_area = Rect::from((i as u32) * width, 0, width, self.size.1);
let painter_area = Rect::from((i as u16) * width, 0, width, self.size.1);
// Spawn the painter
self.spawn_painter(painter_area, binary);

View File

@@ -48,7 +48,7 @@ impl Client {
}
/// Write a pixel to the given stream.
pub fn write_pixel(&mut self, x: u32, y: u32, color: Color) -> Result<(), Error> {
pub fn write_pixel(&mut self, x: u16, y: u16, color: Color) -> Result<(), Error> {
if self.binary {
self.write_command(
&[
@@ -74,7 +74,7 @@ impl Client {
}
/// Read the size of the screen.
pub fn read_screen_size(&mut self) -> Result<(u32, u32), Error> {
pub fn read_screen_size(&mut self) -> Result<(u16, u16), Error> {
// Read the screen size
let data = self
.write_read_command(b"SIZE")
@@ -87,10 +87,10 @@ impl Client {
match re.captures(&data) {
Some(matches) => Ok((
matches[1]
.parse::<u32>()
.parse::<u16>()
.expect("Failed to parse screen width, received malformed data"),
matches[2]
.parse::<u32>()
.parse::<u16>()
.expect("Failed to parse screen height, received malformed data"),
)),
None => Err(Error::new(

View File

@@ -2,14 +2,14 @@
#[derive(Copy, Clone)]
pub struct Rect {
// TODO: Make these properties private
pub x: u32,
pub y: u32,
pub w: u32,
pub h: u32,
pub x: u16,
pub y: u16,
pub w: u16,
pub h: u16,
}
impl Rect {
pub fn from(x: u32, y: u32, w: u32, h: u32) -> Rect {
pub fn from(x: u16, y: u16, w: u16, h: u16) -> Rect {
Rect { x, y, w, h }
}
}