///|
/// Renders an 8-bit grayscale Image as an ASCII / Unicode block string for terminal display.
pub fn render_ascii_art(
img : Image,
target_width? : Int = 60,
use_unicode_blocks? : Bool = true,
) -> String {
let aspect_ratio = img.height.to_double() / img.width.to_double()
// Terminal characters are typically ~2x taller than wide, so compress height by 0.5
let target_height = (target_width.to_double() * aspect_ratio * 0.5)
.round()
.to_int()
let target_h = if target_height < 5 { 5 } else { target_height }
let small_img = img.resize(target_width, target_h)
let buf = StringBuilder()
let ascii_ramp = [" ", ".", ":", "-", "=", "+", "*", "#", "%", "@"]
let ramp_len = ascii_ramp.length()
let block_ramp = [" ", "░", "▒", "▓", "█"]
let block_len = block_ramp.length()
for y in 0..= block_len { block_len - 1 } else { idx }
buf.write_string(block_ramp[safe_idx])
} else {
let idx = pixel * ramp_len / 256
let safe_idx = if idx >= ramp_len { ramp_len - 1 } else { idx }
buf.write_string(ascii_ramp[safe_idx])
}
}
buf.write_string("\n")
}
buf.to_string()
}