This commit is contained in:
2025-07-14 18:22:40 +02:00
parent 49f036e742
commit c418677e02

View File

@ -1,6 +1,9 @@
use clap::Parser; use clap::Parser;
use eyre::{Context, ContextCompat, bail, ensure, eyre}; use eyre::{Context, ContextCompat, bail, ensure, eyre};
use image::{DynamicImage, GrayAlphaImage, ImageReader, imageops::overlay}; use image::{
DynamicImage, GrayAlphaImage, ImageReader,
imageops::{flip_vertical_in_place, overlay},
};
use nix::ioctl_write_ptr; use nix::ioctl_write_ptr;
use std::{ use std::{
fs::File, fs::File,
@ -105,12 +108,15 @@ fn main() -> eyre::Result<()> {
Ok(()) Ok(())
} }
fn image_to_offimage_buffer(image: GrayAlphaImage) -> eyre::Result<Box<[u8; IOCTL_BUFFER_SIZE]>> { fn image_to_offimage_buffer(
mut image: GrayAlphaImage,
) -> eyre::Result<Box<[u8; IOCTL_BUFFER_SIZE]>> {
let mut buf: Box<[u8; IOCTL_BUFFER_SIZE]> = vec![0u8; IOCTL_BUFFER_SIZE] let mut buf: Box<[u8; IOCTL_BUFFER_SIZE]> = vec![0u8; IOCTL_BUFFER_SIZE]
.into_boxed_slice() .into_boxed_slice()
.try_into() .try_into()
.expect("Buffer is the correct size"); .expect("Buffer is the correct size");
flip_vertical_in_place(&mut image);
let image = image.into_vec(); let image = image.into_vec();
ensure!( ensure!(
@ -118,16 +124,8 @@ fn image_to_offimage_buffer(image: GrayAlphaImage) -> eyre::Result<Box<[u8; IOCT
"Invalid size of image backing buffer. Bug?", "Invalid size of image backing buffer. Bug?",
); );
// iterate over pixel coordinates
let xs = 0..SCREEN_W;
let ys = 0..SCREEN_H;
let ys = ys.rev(); // flip `y`, it's reversed in the ioctl buffer.
let coords = ys.flat_map(|y| xs.clone().map(move |x| (x, y)));
// coordinates to indices
let indices = coords.map(|(x, y)| y * SCREEN_W + x);
// Iterate over pixels and copy them into `buf`. // Iterate over pixels and copy them into `buf`.
for (i, pixel) in indices.zip(image.chunks_exact(2)) { for (i, pixel) in image.chunks_exact(2).enumerate() {
let &[gray, alpha] = pixel else { let &[gray, alpha] = pixel else {
unreachable!() unreachable!()
}; };