Files
enkou/enkou-shaders/examples/gasket.rs
T
bspeice 6a7ce14137
CI / cargo fmt (push) Successful in 25s
CI / cargo test (push) Successful in 13m26s
CI / cargo test (GPU) (push) Successful in 12m50s
Start fixes for the gasket example
The image is inverted for reasons I don't entirely understand yet
2026-07-04 12:23:57 -04:00

111 lines
3.2 KiB
Rust

use anyhow::{Context, Result};
use enkou_shaders::Coefficients2;
use enkou_shaders::camera::Camera;
use enkou_shaders::camera::entry::main_image_render;
use enkou_shaders::chaos_game::entry::main_chaos_game;
use enkou_shaders::transform::Transform;
use enkou_shaders::variation::Variation;
use glam::{Affine2, UVec2, Vec2, Vec2Swizzles, Vec4, uvec2, vec2};
use image::{Rgba, RgbaImage};
use std::mem;
use std::process::Command;
use tempfile::NamedTempFile;
const ITERATIONS_DISCARD: u32 = 20;
const ITERATIONS: u32 = 50_000;
const IMAGE_DIMENSION: UVec2 = uvec2(600, 600);
pub fn main() -> Result<()> {
let transforms = [
// F_0: (x / 2, y / 2)
Transform::new(
Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.0),
uvec2(0, 1),
vec2(0.0, 0.0),
),
// F_1: ((x + 1) / 2, y / 2)
Transform::new(
Affine2::from_coefficients(0.5, 0.0, 0.5, 0.0, 0.5, 0.0),
uvec2(0, 1),
vec2(0.0, 0.0),
),
// F_2: (x / 2, (y + 1) / 2)
Transform::new(
Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.5),
uvec2(0, 1),
vec2(0.0, 0.0),
),
];
let weights = [1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0];
let variations = [Variation::IDENTITY];
let mut output_points_ifs = Vec::new();
output_points_ifs.resize(ITERATIONS as usize, Vec4::ZERO);
main_chaos_game(
ITERATIONS_DISCARD,
&[4u8; 32],
&transforms,
&weights,
&variations,
&mut output_points_ifs,
);
// The gasket is defined on the range [0, 1] for both X and Y
let camera = Camera::new(
IMAGE_DIMENSION,
Vec2::ONE * 0.5,
0.0,
Vec2::ZERO,
IMAGE_DIMENSION.as_vec2(),
);
let palette = &[Vec4::ONE; 2];
let mut output_points_pixel = Vec::new();
output_points_pixel.resize(IMAGE_DIMENSION.xy().element_product() as usize, Vec4::ZERO);
main_image_render(
&camera,
palette,
&output_points_ifs,
&mut output_points_pixel,
);
let mut image = RgbaImage::new(IMAGE_DIMENSION.x, IMAGE_DIMENSION.y);
for y in 0..image.dimensions().1 {
for x in 0..image.dimensions().0 {
let pixel_index = y * IMAGE_DIMENSION.x + x;
let pixel = output_points_pixel[pixel_index as usize];
let pixel = pixel.to_array().map(|channel| {
let channel = if channel.is_nan() { 0.0 } else { channel };
let channel = channel * u8::MAX as f32;
channel as u8
});
image.put_pixel(x, y, Rgba(pixel.into()));
}
}
let temp = NamedTempFile::with_suffix(".png").context("Unable to create file for image")?;
image.save(temp.path()).context("Unable to save image")?;
let open_program: &str = cfg_select! {
unix => Some("xdg-open"),
_ => None,
}
.expect("No available program to open images");
Command::new(open_program)
.arg(temp.path())
.spawn()?
.wait()?;
// In case the image viewer forks and gives control back prior to reading the file,
// drop it and don't run the destructor
mem::forget(temp);
Ok(())
}