use crate::testing_prelude::*;
use image::{Rgb, RgbImage};
#[derive(Debug, Clone)]
pub struct ImageGenerator {
width: u32,
height: u32,
filename: String,
}
impl Default for ImageGenerator {
fn default() -> Self {
Self {
width: 100,
height: 100,
filename: "image.png".to_owned(),
}
}
}
impl ImageGenerator {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_size(mut self, width: u32, height: u32) -> Self {
self.width = width;
self.height = height;
self
}
#[must_use]
pub fn with_filename(mut self, filename: impl Into<String>) -> Self {
self.filename = filename.into();
self
}
pub fn generate(&self, output_dir: &Path) -> Result<PathBuf, Failure<SampleAction>> {
let path = output_dir.join(&self.filename);
let mut img = RgbImage::new(self.width, self.height);
#[allow(
clippy::as_conversions,
clippy::cast_possible_truncation,
clippy::integer_division
)]
for y in 0..self.height {
for x in 0..self.width {
let r = (255 - (x * 255 / self.width.max(1))) as u8;
let b = (y * 255 / self.height.max(1)) as u8;
img.put_pixel(x, y, Rgb([r, 0, b]));
}
}
img.save(&path)
.map_err(Failure::wrap(SampleAction::SaveImage))?;
Ok(path)
}
}