icns-rs

Easily create .icns files (Mac Icons) with this Rust library or the included CLI app.
Log | Files | Refs | LICENSE

convert_png.rs (1595B)


      1 use std::fs::File;
      2 use std::io::{prelude::*, BufReader, BufWriter};
      3 use clap::{App, Arg};
      4 use image;
      5 use icns::Encoder;
      6 
      7 fn main() {
      8     let matches = App::new("convert png to icns")
      9         .version("0.1.0")
     10         .author("Jack Mordaunt <jackmordaunt@gmail.com>")
     11         .about("easily convert png to icns")
     12         .arg(Arg::with_name("input")
     13             .required(true)
     14             .takes_value(true)
     15             .short("i")
     16             .help("path to input png image"))
     17         .arg(Arg::with_name("output")
     18             .required(true)
     19             .takes_value(true)
     20             .short("o")
     21             .help("path to output icns image"))
     22         .get_matches();
     23 
     24     // Load inputs. Since we specified "required", these unwraps wont fail. 
     25     let input = matches.value_of("input").unwrap();
     26     let output = matches.value_of("output").unwrap();
     27 
     28     // Read the png file into a buffer.
     29     let mut png: Vec<u8> = vec![];
     30     BufReader::new(File::open(&input)
     31         .expect("opening input file"))
     32         .read_to_end(&mut png)
     33         .expect("buffering input file");
     34     
     35     // Load a DynamicImage object from the raw png data. 
     36     let png = image::load_from_memory(&png)
     37         .expect("decoding png from buffer");
     38     
     39     // Create the output file. 
     40     let mut output = BufWriter::new(File::create(&output)
     41         .expect("creating output file"));
     42 
     43     // Encode the png as icns into the output file. 
     44     // Note we use to_rgba to convert the DynamicImage into an RgbaImage. 
     45     Encoder::new(&mut output)
     46         .encode(&png.to_rgba())
     47         .expect("encoding icns");
     48 }