Add a color coordinate to the chaos game

Also refactors the gasket example to avoid using shader entry points; the entry point signatures are not stable, and I don't want to keep refactoring the gasket while adding more advanced color/image features.
This commit is contained in:
2026-07-12 21:12:01 -04:00
parent 7ff19631ba
commit fd49ae7256
4 changed files with 75 additions and 81 deletions
+19 -33
View File
@@ -1,36 +1,39 @@
use anyhow::{Context, Result};
use enkou_shaders::Coefficients2;
use enkou_shaders::camera::Camera;
use enkou_shaders::camera::entry::main_camera;
use enkou_shaders::chaos_game::entry::main_chaos_game;
use enkou_shaders::chaos_game::ChaosGame;
use enkou_shaders::transform::Transform;
use enkou_shaders::variation::Variation;
use glam::{Affine2, IVec2, UVec2, Vec2, uvec2};
use glam::{Affine2, UVec2, Vec2, uvec2, vec2};
use image::{GrayImage, Luma};
use rand::SeedableRng;
use rand_xoshiro::Xoshiro256StarStar;
use std::mem;
use std::process::Command;
use tempfile::NamedTempFile;
const ITERATIONS_DISCARD: u32 = 20;
const ITERATIONS: u32 = 50_000;
const ITERATIONS_DISCARD: usize = 20;
const ITERATIONS: usize = 50_000;
const IMAGE_DIMENSION: UVec2 = uvec2(600, 600);
pub fn main() -> Result<()> {
let mut rng = Xoshiro256StarStar::from_seed([4u8; 32]);
let transforms = [
{
// F_0: (x / 2, y / 2)
let coefficients = Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.0);
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1))
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1), vec2(0.0, 1.0))
},
{
// F_1: ((x + 1) / 2, y / 2)
let coefficients = Affine2::from_coefficients(0.5, 0.0, 0.5, 0.0, 0.5, 0.0);
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1))
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1), vec2(0.0, 1.0))
},
{
// F_2: (x / 2, (y + 1) / 2)
let coefficients = Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.5);
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1))
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1), vec2(0.0, 1.0))
},
];
@@ -38,18 +41,6 @@ pub fn main() -> Result<()> {
let variations = [Variation::IDENTITY];
let mut output_points_ifs = Vec::new();
output_points_ifs.resize(ITERATIONS as usize, Vec2::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,
@@ -59,20 +50,15 @@ pub fn main() -> Result<()> {
IMAGE_DIMENSION.as_vec2(),
);
let mut output_points_pixel = Vec::new();
output_points_pixel.resize(ITERATIONS as usize, IVec2::ZERO);
main_camera(&camera, &output_points_ifs, &mut output_points_pixel);
let mut image = GrayImage::new(IMAGE_DIMENSION.x, IMAGE_DIMENSION.y);
let dimensions = image.dimensions();
output_points_pixel
.iter()
.skip_while(|p| {
p.x < 0 || (p.x as u32) > dimensions.0 || p.y < 0 || (p.y as u32) > dimensions.1
})
.map(|p| (p.x as u32, p.y as u32))
.for_each(|(x, y)| image.put_pixel(x, y, Luma([255u8])));
let chaos_game = ChaosGame::new(&mut rng, &transforms, &weights, &variations);
chaos_game
.skip(ITERATIONS_DISCARD)
.take(ITERATIONS)
.filter_map(|(point_ifs, _)| camera.transform_point_to_image(point_ifs))
.for_each(|point_pixel| image.put_pixel(point_pixel.x, point_pixel.y, Luma([255])));
let temp = NamedTempFile::with_suffix(".png").context("Unable to create file for image")?;
image.save(temp.path()).context("Unable to save image")?;