main.rs (2663B)
1 mod decode; 2 mod encode; 3 mod os_type; 4 5 use clap::{App, Arg}; 6 use decode::Decoder; 7 use encode::Encoder; 8 use image::{self, png::PNGEncoder, ColorType, ImageFormat}; 9 use std::fs::File; 10 use std::io::{self, BufReader, BufWriter, Cursor}; 11 12 fn main() { 13 let cli = App::new("icnsify") 14 .version("0.2.0") 15 .author("Jack Mordaunt <jackmordaunt@gmail.com>") 16 .about("easily create icns icons from png images") 17 .arg( 18 Arg::with_name("in") 19 .short("i") 20 .long("input") 21 .takes_value(true) 22 .requires("out") 23 .help("path to input file"), 24 ) 25 .arg( 26 Arg::with_name("out") 27 .short("o") 28 .long("output") 29 .takes_value(true) 30 .requires("in") 31 .help("path to output file"), 32 ) 33 .arg( 34 Arg::with_name("decode") 35 .long("decode") 36 .help("decode an icns into a png (reverse the direction)"), 37 ) 38 .get_matches(); 39 if let (Some(src), Some(out)) = (cli.value_of("in"), cli.value_of("out")) { 40 if cli.is_present("decode") { 41 let src = BufReader::new(File::open(&src).expect("opening src file")); 42 let img = Decoder::new(src).decode().expect("decoding png from icns"); 43 img.save(&out).expect("writing png"); 44 } else { 45 let src = image::open(&src).expect("decoding input image"); 46 let out = BufWriter::new(File::create(&out).expect("creating output file")); 47 Encoder::new(out) 48 .encode(&src.to_rgba()) 49 .expect("encoding icns"); 50 } 51 } else { 52 // BUG: Piping doesn't work with pwsh version of cat (builtin, I think). 53 // What is the powershell way of piping? 54 // Check on Unix. 55 if cli.is_present("decode") { 56 let img = Decoder::new(BufReader::new(io::stdin().lock())) 57 .decode() 58 .expect("decoding icns") 59 .into_rgba(); 60 PNGEncoder::new(io::stdout().lock()) 61 .encode(&img, img.width(), img.height(), ColorType::Rgb8) 62 .expect("encoding png"); 63 } else { 64 let mut buffer: Vec<u8> = vec![]; 65 io::copy(&mut io::stdin().lock(), &mut buffer).expect("reading from stdin"); 66 let img = image::load(&mut Cursor::new(buffer), ImageFormat::Png).expect("loading png"); 67 Encoder::new(BufWriter::new(io::stdout().lock())) 68 .encode(&img.to_rgba()) 69 .expect("encoding icns"); 70 } 71 } 72 }