Verified Commit 8d1e79cf authored by Jenny's avatar Jenny
Browse files

added shuffle feature for image spammer module

parent c5735232
......@@ -5,35 +5,21 @@ use std::path::Path;
use std::thread;
use std::thread::JoinHandle;
use image::GenericImageView;
use rand::thread_rng;
use rand::seq::SliceRandom;
fn draw_image_slice(image_path: &String, host: &String, offset_x: u32, offset_y: u32, from_x: u32, from_y: u32, to_x: u32, to_y: u32) {
let img = image::open(Path::new(image_path)).expect("Failed to open image!");
use image::GenericImageView;
fn draw_image_slice(host: String, area: Vec<String>) {
let mut stream = TcpStream::connect(host).expect("Failed to connect!");
loop {
for x in from_x..=to_x {
for y in from_y..=to_y {
let px = img.get_pixel(x, y);
// skip alpha < 10
if px[3] < 10 {
continue;
}
let target_x = x + offset_x;
let target_y = y + offset_y;
let msg = format!("PX {} {} {:0>2X}{:0>2X}{:0>2X}{:0>2X}\n", target_x, target_y, px[0], px[1], px[2], px[3]);
stream.write(msg.as_bytes()).expect("Failed to send message!");
}
for pos in area.iter() {
stream.write(pos.as_bytes()).expect("Failed to send message!");
}
}
//TODO: shutdown on ctrl-c
//stream.shutdown(Shutdown::Both).expect("Failed to shutdown connection!");
}
pub fn draw_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, shuffle: bool, skip_alpha: u8) {
let img = image::open(Path::new(image_path)).expect("Failed to open image!");
let x_max = img.dimensions().0 - 1;
......@@ -70,11 +56,31 @@ pub fn draw_image(image_path: &str, host: &str, slices: u32, offset_x: u32, offs
to_y = j * slice_y;
};
let path_string = String::from(image_path);
let mut slice_area: Vec<String> = Vec::new();
for x in from_x..=to_x {
for y in from_y..=to_y {
let px = img.get_pixel(x, y);
// skip alpha
if px[3] <= skip_alpha {
continue;
}
let target_x = x + offset_x;
let target_y = y + offset_y;
let msg = format!("PX {} {} {:0>2X}{:0>2X}{:0>2X}{:0>2X}\n", target_x, target_y, px[0], px[1], px[2], px[3]);
slice_area.push(msg);
}
}
if shuffle {
slice_area.shuffle(&mut thread_rng());
}
let host_string = String::from(host);
let t = thread::spawn(move || {
draw_image_slice(&path_string, &host_string, offset_x, offset_y, from_x, from_y, to_x, to_y)
draw_image_slice(host_string, slice_area)
});
threads.push(t);
......
......@@ -58,6 +58,19 @@ 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)
)
.arg(Arg::new("skip-alpha")
.long("skip-alpha")
.value_name("ALPHA-VALUE")
.about("The alpha value at of which the pixel will be skipped if it is below or equal")
.takes_value(true)
.required(false)
.default_value("10")
)
)
.subcommand(
App::new("rect")
......@@ -131,8 +144,10 @@ fn main() {
let slices: u32 = matches.value_of_t_or_exit("slices");
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");
let skip_alpha: u8 = matches.value_of_t_or_exit("skip-alpha");
image::draw_image(image_path, host, slices, offset_x, offset_y);
image::draw_image(image_path, host, slices, offset_x, offset_y, shuffle, skip_alpha);
}
Some("rect") => {
let matches = matches.subcommand_matches("rect").unwrap();
......
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