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

67 lines
2.9 KiB
Rust
Raw Normal View History

2026-07-09 02:59:53 +03:00
use std::io::Write;
const RESULTS_FILE: &str = "/mnt/extra/00_PROJECTS/hcie-rust-v4/gradient_overlay_results.txt";
fn main() {
let psd_path = "_images/_psd_stil_test/gradient_overlay.psd";
let ref_path = "_images/_psd_stil_test/gradient_overlay.png";
2026-07-09 02:59:53 +03:00
let ref_img = image::open(ref_path).expect("open gradient_overlay.png").to_rgba8();
let (w, h) = (ref_img.width(), ref_img.height());
let px = (w * h) as usize;
let ref_pixels = ref_img.into_raw();
let layers = hcie_psd::import_psd(std::path::Path::new(psd_path)).expect("import gradient_overlay.psd");
let layer = &layers[0];
let alpha: Vec<u8> = layer.pixels.iter().skip(3).step_by(4).copied().collect();
let (angle, scale, gradient, blend_mode_str, opacity) = {
let mut ang = 0.0f32; let mut sc = 0.0f32; let mut grad = hcie_fx::types::GradientDef { color_stops: vec![], transparency_stops: vec![], midpoint: 50.0, angle: 0.0, scale: 100.0, gradient_type: 0 };
let mut bm = String::new(); let mut op = 0.0f32;
for e in &layer.effects {
if let hcie_protocol::effects::LayerEffect::GradientOverlay { enabled: true, angle, scale, gradient, blend_mode, opacity, .. } = e {
ang = *angle; sc = *scale;
grad = hcie_fx::types::GradientDef {
color_stops: gradient.color_stops.iter().map(|(loc, c)| (*loc, *c)).collect(),
transparency_stops: vec![],
midpoint: 50.0,
angle: *angle,
scale: *scale,
gradient_type: 0,
};
bm = blend_mode.clone(); op = *opacity;
}
}
(ang, sc, grad, bm, op)
};
let bm_parsed = hcie_fx::types::blend_mode_from_string(&blend_mode_str);
println!("GradientOverlay: angle={}, scale={}, blend={}, opacity={}", angle, scale, blend_mode_str, opacity);
println!("Image: {}x{}", w, h);
std::io::stdout().flush().ok();
let overlay = hcie_fx::tuned::generate_gradient_overlay_tuned(&alpha, w, h, angle, scale, &gradient);
let mut out = layer.pixels.clone();
for i in 0..px {
let sa = overlay[i * 4 + 3];
if sa == 0 { continue; }
let dst = [out[i * 4], out[i * 4 + 1], out[i * 4 + 2], out[i * 4 + 3]];
let src = [overlay[i * 4], overlay[i * 4 + 1], overlay[i * 4 + 2], sa];
let blended = hcie_blend::blend_pixels(dst, src, bm_parsed, opacity);
out[i * 4..i * 4 + 4].copy_from_slice(&blended);
}
let mut diff_sum = 0.0f64;
for i in 0..px {
let d = (0..4).map(|c| (out[i * 4 + c] as i32 - ref_pixels[i * 4 + c] as i32).abs() as f64).sum::<f64>();
diff_sum += d;
}
let mae = diff_sum / px as f64 / 4.0;
println!("MAE = {:.3}", mae);
if let Ok(mut file) = std::fs::OpenOptions::new().create(true).write(true).truncate(true).open(RESULTS_FILE) {
let _ = writeln!(file, "=== Gradient Overlay ===\n MAE: {:.3}", mae);
}
}