Compare commits

...

4 commits

2 changed files with 51 additions and 28 deletions

View file

@ -12,7 +12,7 @@ use std::{
}; };
use bracket_color::prelude::{HSV, RGB}; use bracket_color::prelude::{HSV, RGB};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand, ValueEnum};
use image::{imageops::FilterType, io::Reader as ImageReader, Pixel, Rgb, RgbImage}; use image::{imageops::FilterType, io::Reader as ImageReader, Pixel, Rgb, RgbImage};
use rand::Rng; use rand::Rng;
@ -35,7 +35,7 @@ struct Args {
remote_addr: SocketAddr, remote_addr: SocketAddr,
/// The action to perform /// The action to perform
#[clap(subcommand, rename_all = "kebab-case")] #[clap(subcommand)]
action: Action, action: Action,
} }
@ -80,12 +80,25 @@ impl From<Color> for Rgb<u8> {
#[derive(Clone, Debug, PartialEq, Eq, Subcommand)] #[derive(Clone, Debug, PartialEq, Eq, Subcommand)]
enum Action { enum Action {
Rainbow, Animation {
Solid { color: Color }, #[clap(value_enum)]
Image { path: PathBuf }, animation: Animation,
},
Solid {
color: Color,
},
Image {
path: PathBuf,
},
Clear, Clear,
} }
#[derive(Clone, Debug, PartialEq, Eq, ValueEnum)]
enum Animation {
Rainbow,
Bling,
}
fn bling(layout: Layout, frame: u32) -> RgbImage { fn bling(layout: Layout, frame: u32) -> RgbImage {
#![allow( #![allow(
clippy::cast_precision_loss, clippy::cast_precision_loss,
@ -181,30 +194,27 @@ fn main() -> anyhow::Result<()> {
first_strand_index: 8, first_strand_index: 8,
}; };
match args.action { let image = match args.action {
Action::Solid { color } => { Action::Solid { color } => {
let image = RgbImage::from_pixel(layout.width_px(), layout.height_px(), color.into()); RgbImage::from_pixel(layout.width_px(), layout.height_px(), color.into())
let frame_num: u32 = rand::thread_rng().gen();
send_frame(&socket, layout, frame_num, &image)?;
} }
Action::Clear => { Action::Clear => RgbImage::new(layout.width_px(), layout.height_px()),
let image = RgbImage::new(layout.width_px(), layout.height_px()); Action::Image { path } => ImageReader::open(path)?
let frame_num: u32 = rand::thread_rng().gen();
send_frame(&socket, layout, frame_num, &image)?;
}
Action::Image { path } => {
let image = ImageReader::open(path)?
.decode()? .decode()?
.resize_to_fill(layout.width_px(), layout.height_px(), FilterType::Gaussian) .resize_to_fill(layout.width_px(), layout.height_px(), FilterType::Gaussian)
.into_rgb8(); .into_rgb8(),
let frame_num: u32 = rand::thread_rng().gen(); Action::Animation { animation } => {
send_frame(&socket, layout, frame_num, &image)?; let f = match animation {
} Animation::Rainbow => rainbow,
Action::Rainbow => { Animation::Bling => bling,
};
print!("{}", termion::clear::All); print!("{}", termion::clear::All);
for frame in 0.. { let mut frame = 0;
let image = rainbow(layout, frame); loop {
let start = Instant::now();
let image = f(layout, frame);
print_image(&image); print_image(&image);
@ -212,9 +222,13 @@ fn main() -> anyhow::Result<()> {
send_frame(&socket, layout, frame_num, &image)?; send_frame(&socket, layout, frame_num, &image)?;
sleep(Duration::from_millis(16)); sleep(Duration::from_millis(16));
frame += 1;
} }
} }
} };
let frame_num: u32 = rand::thread_rng().gen();
send_frame(&socket, layout, frame_num, &image)?;
Ok(()) Ok(())
} }

View file

@ -23,6 +23,9 @@ pub struct Strandifier<'a> {
pixels_remaining: u32, pixels_remaining: u32,
next_x: u32, next_x: u32,
next_y: u32, next_y: u32,
offset_x: u32,
offset_y: u32,
} }
impl<'a> Strandifier<'a> { impl<'a> Strandifier<'a> {
@ -41,7 +44,7 @@ impl<'a> Strandifier<'a> {
image: &'a RgbImage, image: &'a RgbImage,
strand_num: u32, strand_num: u32,
) -> Result<Self, StrandifierError> { ) -> Result<Self, StrandifierError> {
if layout.width_px() != image.width() || layout.height_px() != image.height() { if layout.width_px() < image.width() || layout.height_px() < image.height() {
return Err(StrandifierError::WrongDimensions); return Err(StrandifierError::WrongDimensions);
} }
@ -55,6 +58,9 @@ impl<'a> Strandifier<'a> {
let first_x = panel_x * layout.gang_len; let first_x = panel_x * layout.gang_len;
let first_y = panel_y * layout.num_gangs; let first_y = panel_y * layout.num_gangs;
let offset_x = (layout.width_px() - image.width()) / 2;
let offset_y = (layout.height_px() - image.height()) / 2;
Ok(Self { Ok(Self {
layout, layout,
image, image,
@ -62,6 +68,9 @@ impl<'a> Strandifier<'a> {
pixels_remaining: layout.strand_len(), pixels_remaining: layout.strand_len(),
next_x: first_x, next_x: first_x,
next_y: first_y, next_y: first_y,
offset_x,
offset_y,
}) })
} }
} }
@ -98,7 +107,7 @@ impl<'a> Iterator for Strandifier<'a> {
} }
self.pixels_remaining -= 1; self.pixels_remaining -= 1;
Some(*self.image.get_pixel(x, y)) Some(*self.image.get_pixel(x + self.offset_x, y + self.offset_y))
} }
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {