diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-15 15:00:27 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-15 15:00:27 +0200 |
commit | 340aa47c4652fe2f0ec5b0e4f293cfff407a0e6c (patch) | |
tree | 1d0aad02c54a223b092eb74d065dfbc68cba56f6 /light-client/tools/src/bin/tex_import.rs | |
parent | d3101bbfd9adf84ef8f0061383272eeaecf224d3 (diff) | |
download | hurrycurry-340aa47c4652fe2f0ec5b0e4f293cfff407a0e6c.tar hurrycurry-340aa47c4652fe2f0ec5b0e4f293cfff407a0e6c.tar.bz2 hurrycurry-340aa47c4652fe2f0ec5b0e4f293cfff407a0e6c.tar.zst |
add texture import/export system for light client
Diffstat (limited to 'light-client/tools/src/bin/tex_import.rs')
-rw-r--r-- | light-client/tools/src/bin/tex_import.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/light-client/tools/src/bin/tex_import.rs b/light-client/tools/src/bin/tex_import.rs new file mode 100644 index 00000000..89cbc8c9 --- /dev/null +++ b/light-client/tools/src/bin/tex_import.rs @@ -0,0 +1,58 @@ +use clap::Parser; +use std::{ + collections::HashMap, + fs::File, + io::{BufWriter, Write}, + path::PathBuf, + process::exit, +}; + +#[derive(Parser)] +struct Args { + input: PathBuf, + output: PathBuf, +} + +fn main() { + let Args { input, output } = Args::parse(); + + let palette = include_str!("../../../textures/palette.csv") + .split('\n') + .filter(|l| !l.is_empty()) + .map(|s| { + let (c, s) = s.split_once(",").unwrap(); + let (r, s) = s.split_once(",").unwrap(); + let (g, s) = s.split_once(",").unwrap(); + let (b, a) = s.split_once(",").unwrap(); + ( + [ + r.parse().unwrap(), + g.parse().unwrap(), + b.parse().unwrap(), + a.parse().unwrap(), + ], + c.chars().next().unwrap(), + ) + }) + .collect::<HashMap<_, _>>(); + + let input = image::open(input).unwrap().to_rgba8(); + let mut output = BufWriter::new(File::create(output).unwrap()); + + for y in 0..input.height() { + for x in 0..input.width() { + let mut c = input.get_pixel(x, y).0; + if c[3] == 0 { + c = [0, 0, 0, 0]; + } + let Some(char) = palette.get(&c) else { + eprintln!("color at {x},{y} not in palette: {c:?}"); + exit(1); + }; + write!(output, "{char}").unwrap(); + } + writeln!(output).unwrap(); + } + output.flush().unwrap(); + output.into_inner().unwrap().flush().unwrap() +} |