Files
hcie-rust-v3.05/hcie-io/examples/tune_sultan.rs
T

136 lines
5.3 KiB
Rust
Raw Normal View History

2026-07-09 02:59:53 +03:00
#![allow(dead_code, unused_imports, unused_variables, unused_macros)]
use std::io::Write;
const SHADOW_RS: &str = "hcie-fx/src/shadow.rs";
const LOG_PATH: &str = "/tmp/sultan_tune_log.txt";
const PSD_BASE: &str = "_images/_psd_stil_test/sultan_test/sultan";
const SULTAN_PSD: &str = "_images/_psd_stil_test/sultan_test/sultan.psd";
const SULTAN_REF: &str = "_images/_psd_stil_test/sultan_test/sultan.png";
2026-07-09 02:59:53 +03:00
fn main() {
let original = std::fs::read_to_string(SHADOW_RS).expect("read shadow.rs");
let mut log = std::fs::File::create(LOG_PATH).expect("create log");
let baseline = measure_mae_sultan(&original);
println!("Baseline Sultan composite: {:.2}", baseline.0);
println!("Baseline Sultan Layer 2 (Bevel): {:.2}", baseline.1);
writeln!(log, "Sultan Tune Baseline: composite={:.2} bevel={:.2}", baseline.0, baseline.1).ok();
let mut best_composite = baseline.0;
let mut best_bevel = baseline.1;
let mut best_source = original.clone();
let variants: Vec<(&str, &str, &str)> = vec![
("negate_ndl",
"let factor = ndl;",
"let factor = -ndl;"),
("no_tilt",
"crate::types::BevelStyle::Emboss => {\n let px_x = (i % iw) as f32;\n let px_y = (i / iw) as f32;\n let tilt = (px_x - cx) * light_x + (px_y - cy) * light_y;\n if alpha[i] > 0 {\n let edge_factor = (d_in / radius).min(1.0);\n radius * edge_factor + tilt\n } else {\n let t = (d_out / radius).min(1.0);\n (radius * (1.0 - t) * 0.5 + tilt).max(0.0)\n }\n }",
"crate::types::BevelStyle::Emboss => {\n if alpha[i] > 0 {\n (d_in / radius).min(1.0) * radius\n } else {\n ((radius - d_out).max(0.0) / radius) * radius\n }\n }"),
("blur_0",
"let lighting_blur = soften_radius + 25;",
"let lighting_blur = 0;"),
("blur_5",
"let lighting_blur = soften_radius + 25;",
"let lighting_blur = 5;"),
("blur_10",
"let lighting_blur = soften_radius + 25;",
"let lighting_blur = 10;"),
("blur_15",
"let lighting_blur = soften_radius + 25;",
"let lighting_blur = 15;"),
("blur_soften",
"let lighting_blur = soften_radius + 25;",
"let lighting_blur = soften_radius;"),
("no_depth",
"let depth_scale = depth / 100.0;",
"let depth_scale = 1.0;"),
("depth_half",
"let depth_scale = depth / 100.0;",
"let depth_scale = depth / 200.0;"),
];
for (name, search, replacement) in &variants {
let modified = apply_change(&original, search, replacement);
if modified.is_empty() || modified == original {
println!("[{}] SKIPPED (no match)", name);
writeln!(log, "[{}] SKIPPED", name).ok();
continue;
}
let (comp, bevel) = measure_mae_sultan(&modified);
std::fs::write(SHADOW_RS, &original).expect("restore");
println!("[{}] composite={:.2} bevel={:.2}", name, comp, bevel);
writeln!(log, "[{}] composite={:.2} bevel={:.2}", name, comp, bevel).ok();
if bevel < best_bevel {
best_bevel = bevel;
best_composite = comp;
best_source = modified;
println!(" *** NEW BEST bevel={:.2}", best_bevel);
}
}
println!("\n=== BEST ===");
println!("composite={:.2} bevel={:.2}", best_composite, best_bevel);
writeln!(log, "\n=== BEST composite={:.2} bevel={:.2} ===", best_composite, best_bevel).ok();
let (fc, fb) = measure_mae_sultan(&best_source);
println!("Verify: composite={:.2} bevel={:.2}", fc, fb);
std::fs::write(SHADOW_RS, &best_source).expect("write final");
println!("Written to {}", SHADOW_RS);
}
fn apply_change(source: &str, search: &str, replacement: &str) -> String {
if let Some(pos) = source.find(search) {
let mut r = String::with_capacity(source.len());
r.push_str(&source[..pos]);
r.push_str(replacement);
r.push_str(&source[pos + search.len()..]);
r
} else {
String::new()
}
}
fn measure_mae_sultan(shadow_source: &str) -> (f64, f64) {
std::fs::write(SHADOW_RS, shadow_source).expect("write");
let output = std::process::Command::new("cargo")
.args(["run", "-p", "hcie-io", "--example", "test_sultan", "-q"])
.current_dir("/mnt/extra/00_PROJECTS/hcie-rust-v4")
.output()
.expect("run");
let stdout = String::from_utf8_lossy(&output.stdout);
let mut composite_mae = 0.0;
let mut bevel_mae = 0.0;
let mut in_bevel_section = false;
for line in stdout.lines() {
if line.contains("sultan_0003_Layer 2.psd") {
in_bevel_section = true;
} else if line.starts_with("===") {
in_bevel_section = false;
}
if line.contains(" Avg diff per channel:") {
let val: f64 = line.trim_start_matches(" Avg diff per channel:").trim().parse().unwrap_or(f64::MAX);
if in_bevel_section {
bevel_mae = val;
}
composite_mae = val;
}
}
(composite_mae, bevel_mae)
}