Enforce valid image brightness in daemon and asusctl

This commit is contained in:
I Al Istannen
2022-07-16 03:00:22 +00:00
committed by Luke Jones
parent ef04549c8e
commit c0e36295b7
6 changed files with 50 additions and 12 deletions

View File

@@ -16,6 +16,7 @@ pub enum AnimeError {
Udev(String, std::io::Error),
NoDevice,
UnsupportedDevice,
InvalidBrightness(f32),
}
impl fmt::Display for AnimeError {
@@ -36,6 +37,11 @@ impl fmt::Display for AnimeError {
AnimeError::Udev(deets, error) => write!(f, "udev {}: {}", deets, error),
AnimeError::NoDevice => write!(f, "No AniMe Matrix device found"),
AnimeError::UnsupportedDevice => write!(f, "Unsupported AniMe Matrix device found"),
AnimeError::InvalidBrightness(bright) => write!(
f,
"Image brightness must be between 0.0 and 1.0 (inclusive), was {}",
bright
),
}
}
}

View File

@@ -191,7 +191,7 @@ impl AnimeGif {
pixels,
decoder.width() as u32,
anime_type,
);
)?;
while let Some(frame) = decoder.read_next_frame()? {
let wait = frame.delay * 10;
@@ -206,7 +206,7 @@ impl AnimeGif {
pixels,
width as u32,
anime_type,
);
)?;
}
for (y, row) in frame.buffer.chunks(frame.width as usize * 4).enumerate() {
for (x, px) in row.chunks(4).enumerate() {

View File

@@ -83,8 +83,12 @@ impl AnimeImage {
pixels: Vec<Pixel>,
width: u32,
anime_type: AnimeType,
) -> Self {
Self {
) -> Result<Self, AnimeError> {
if bright < 0.0 || bright > 1.0 {
return Err(AnimeError::InvalidBrightness(bright));
}
Ok(Self {
scale,
angle,
translation,
@@ -93,7 +97,7 @@ impl AnimeImage {
img_pixels: pixels,
width,
anime_type,
}
})
}
// TODO: Convert functions back to const after todo completed
@@ -435,7 +439,7 @@ impl AnimeImage {
pixels,
width,
anime_type,
);
)?;
matrix.update();
Ok(matrix)