use std::{env, fs, path::Path}; fn main() { let out_dir = env::var_os("OUT_DIR").unwrap(); let out_dir = Path::new(&out_dir); // Blue noise // ------------------------------------------------------------------- let size_bits = 7; let size = 1 << size_bits; let frac_bits = 3; let mut points = bluenoise::WrappingBlueNoise::::from_seed( size as f32, size as f32, 2.0, 0x1b346a578222c7b, ) .map(|p| { // f32 -> i{size_bits}.{frac_bits} let p = [p.x, p.y] .map(|c| c * (1 << frac_bits) as f32) .map(|c| (c as u32).min((1 << frac_bits << size_bits) - 1)); let i = hilbert_2d::u32::xy2h_discrete( p[0] >> frac_bits, p[1] >> frac_bits, size_bits, hilbert_2d::Variant::Hilbert, ); (p, i) }) .collect::>(); // Sort by hilbert index points.sort_by_key(|x| x.1); // Delta encoding let mut last_p = [0, 0]; let points_delta: Vec = points .iter() .flat_map(|&(p, _)| { let dp: [i8; 2] = std::array::from_fn(|i| { (p[i] as i32 - last_p[i] as i32) .try_into() .expect("delta too large") }); last_p = p; // Encode as bytes dp.map(|x| x as u8) }) .collect::>(); // Output the delta encoding fs::write(out_dir.join("blue.bin"), &points_delta).expect("failed to write blue.bin"); fs::write( out_dir.join("blue.rs"), format!( "pub const SIZE_BITS: u32 = {size_bits};\ pub const FRAC_BITS: u32 = {frac_bits};" ), ) .expect("failed to write blue.rs"); // sRGB gamma curve // ------------------------------------------------------------------- let table: Vec<_> = (0..256) .map(|x| { let x = x as f64 / 255.0; let x = if x <= 0.0031308 { 12.92 * x } else { 1.055 * x.powf(1.0 / 2.4) - 0.055 }; (x * 255.0).round() as u8 }) .collect(); fs::write(out_dir.join("srgb.bin"), &table).expect("faile to write srgb.bin"); println!("cargo:rerun-if-changed=build.rs"); }