diff --git a/Cargo.toml b/Cargo.toml index d011f38..f802800 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -image = "0.23.14" \ No newline at end of file +image = "0.25.8" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7384df9..9f55a40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,26 +1,37 @@ use image::{GenericImageView, Pixel}; use std::fs::File; -use std::io::Write; +use std::io::{BufWriter, Write}; use std::env; fn main() -> std::io::Result<()> { let args: Vec = env::args().collect(); + if args.len() != 3 { + eprintln!("Usage: {} ", args[0]); + std::process::exit(1); + } + let path = &args[1]; let out_path = &args[2]; + let img = image::open(path).map_err(|e| { std::io::Error::new(std::io::ErrorKind::Other, e.to_string()) })?; + let (width, height) = img.dimensions(); - let mut file = File::create(out_path)?; + + let file = File::create(out_path)?; + let mut writer = BufWriter::new(file); + + writer.write_all(&width.to_le_bytes())?; + writer.write_all(&height.to_le_bytes())?; + for y in 0..height { for x in 0..width { - let pixel = img.get_pixel(x, y); - let (r, g, b, _) = pixel.channels4(); - write!(file, "{}s{}s{}p", r, g, b)?; + let px = img.get_pixel(x, y).to_rgb(); + writer.write_all(&px.0)?; } - file.write_all(b"\n")?; } + writer.flush()?; Ok(()) } - diff --git a/testi.np2 b/testi.np2 new file mode 100644 index 0000000..253a6f1 Binary files /dev/null and b/testi.np2 differ diff --git a/testpic.png b/testpic.png index e69de29..764f251 100644 Binary files a/testpic.png and b/testpic.png differ