index : gmss.git

ascending towards madness

use screenshots::image::{ImageBuffer, Rgba};

/// Represents the size of an image in pixels.
///
/// Printed as: `(width, height)`
#[derive(Default)]
pub struct ImageDimensions {
    pub width: u32,
    pub height: u32,
}

impl std::fmt::Display for ImageDimensions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("({}, {})", self.width, self.height))
    }
}

pub struct Renderer;

impl Renderer {
    /// Horizontally stitches together a given collection of images.
    ///
    /// The height of the output image is dependent on the height of the tallest image.
    pub fn stitch_horizontally(
        images: Vec<ImageBuffer<Rgba<u8>, Vec<u8>>>,
    ) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
        let dimensions = images
            .iter()
            .fold(ImageDimensions::default(), |dimensions, buf| {
                ImageDimensions {
                    width: dimensions.width + buf.width(),
                    height: dimensions.height.max(buf.height()),
                }
            });

        let mut img_buf = ImageBuffer::new(dimensions.width, dimensions.height);

        images
            .iter()
            .fold(ImageDimensions::default(), |offset, buf| {
                for (x, y, pixel) in buf.enumerate_pixels() {
                    img_buf.put_pixel(offset.width + x, y, *pixel);
                }
                ImageDimensions {
                    width: offset.width + buf.width(),
                    height: offset.height,
                }
            });

        img_buf
    }
}