Verified Commit c5735232 authored by Jenny's avatar Jenny
Browse files

added rect slice feature

parent f514205e
......@@ -161,6 +161,17 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "gif"
version = "0.11.2"
......@@ -336,6 +347,7 @@ version = "1.1.0"
dependencies = [
"clap",
"image",
"rand",
]
[[package]]
......@@ -350,6 +362,12 @@ dependencies = [
"miniz_oxide 0.3.7",
]
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
......@@ -392,6 +410,46 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core",
]
[[package]]
name = "rayon"
version = "1.5.1"
......@@ -505,6 +563,12 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "weezl"
version = "0.1.5"
......
......@@ -11,4 +11,5 @@ license = "AGPL-3.0-or-later"
[dependencies]
image = "0.23.14"
clap = "3.0.0-beta.4"
\ No newline at end of file
clap = "3.0.0-beta.4"
rand = "0.8.4"
\ No newline at end of file
......@@ -33,7 +33,7 @@ fn draw_image_slice(image_path: &String, host: &String, offset_x: u32, offset_y:
//stream.shutdown(Shutdown::Both).expect("Failed to shutdown connection!");
}
pub fn draw_sliced_image(image_path: &str, host: &str, slices: u32, offset_x: u32, offset_y: u32) {
pub fn draw_image(image_path: &str, host: &str, slices: u32, offset_x: u32, offset_y: u32) {
let img = image::open(Path::new(image_path)).expect("Failed to open image!");
let x_max = img.dimensions().0 - 1;
......
......@@ -72,6 +72,14 @@ fn main() {
.takes_value(true)
.required(true)
)
.arg(Arg::new("slices")
.short('s')
.long("slices")
.value_name("SLICES")
.about("Number of parts in that each axis should be sliced")
.takes_value(true)
.required(true)
)
.arg(Arg::new("height")
.short('h')
.long("height")
......@@ -106,6 +114,11 @@ fn main() {
.required(false)
.default_value("0")
)
.arg(Arg::new("no-shuffle")
.long("no-shuffle")
.about("Disable the shuffling of the pixel draw order")
.required(false)
)
)
.get_matches();
......@@ -119,17 +132,19 @@ fn main() {
let offset_x: u32 = matches.value_of_t_or_exit("offset-x");
let offset_y: u32 = matches.value_of_t_or_exit("offset-y");
image::draw_sliced_image(image_path, host, slices, offset_x, offset_y);
image::draw_image(image_path, host, slices, offset_x, offset_y);
}
Some("rect") => {
let matches = matches.subcommand_matches("rect").unwrap();
let color = matches.value_of("color").unwrap();
let slices: u32 = matches.value_of_t_or_exit("slices");
let height: u32 = matches.value_of_t_or_exit("height");
let width: u32 = matches.value_of_t_or_exit("width");
let offset_x: u32 = matches.value_of_t_or_exit("offset-x");
let offset_y: u32 = matches.value_of_t_or_exit("offset-y");
let shuffle = !matches.is_present("no-shuffle");
rect::draw_rect(host, color, height, width, offset_x, offset_y);
rect::draw_rect(host, color, slices, height, width, offset_x, offset_y, shuffle);
}
None => {
println!("No subcommand specified!");
......
use std::io::prelude::Write;
use std::net::TcpStream;
pub fn draw_rect(host: &str, color: &str, height: u32, width: u32, offset_x: u32, offset_y: u32) {
let mut stream = TcpStream::connect(host).expect("Failed to connect!");
use std::thread;
use std::thread::JoinHandle;
use rand::thread_rng;
use rand::seq::SliceRandom;
fn draw_rect_slice(host: String, area: Vec<String>) {
let mut stream = TcpStream::connect(host).expect("Failed to connect!");
loop {
for x in 0..=width {
for y in 0..=height {
let target_x = x + offset_x;
let target_y = y + offset_y;
let msg = format!("PX {} {} {}\n", target_x, target_y, color);
for pos in area.iter() {
stream.write(pos.as_bytes()).expect("Failed to send message!");
}
}
}
pub fn draw_rect(host: &str, color: &str, slices: u32, height: u32, width: u32, offset_x: u32, offset_y: u32, shuffle: bool) {
let slice_width = width / slices;
let slice_height = height / slices;
let mut slice_width_rest = width % slices;
let mut slice_height_rest = height % slices;
let mut threads: Vec<JoinHandle<()>> = Vec::new();
for i in 1..=slices {
for j in 1..=slices {
let from_x = (i - 1) * slice_width;
let from_y = (j - 1) * slice_height;
stream.write(msg.as_bytes()).expect("Failed to send message!");
let to_x: u32;
let to_y: u32;
// add division rest to slices evenly
if slice_width_rest > 0 {
to_x = i * slice_width + 1;
slice_width_rest -= 1;
} else {
to_x = i * slice_width;
};
if slice_height_rest > 0 {
to_y = j * slice_height + 1;
slice_height_rest -= 1;
} else {
to_y = j * slice_height;
};
let mut slice_area: Vec<String> = Vec::new();
for x in from_x..=to_x {
for y in from_y..=to_y {
let target_x = x + offset_x;
let target_y = y + offset_y;
let msg = format!("PX {} {} {}\n", target_x, target_y, color);
slice_area.push(msg);
}
}
if shuffle {
slice_area.shuffle(&mut thread_rng());
}
let host_string = String::from(host);
let t = thread::spawn(move || {
draw_rect_slice(host_string, slice_area);
});
threads.push(t);
}
}
for t in threads {
t.join().unwrap();
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment