icns-rs

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

commit 54dd8283aa3e7f117616fa6341bdf71fa445586a
parent e005630774147530f580ae5276272f2447dd73a2
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date:   Mon, 31 Dec 2018 13:59:03 +1300

[+] Accept concrete type, and let callers convert to it.

Diffstat:
Mexamples/convert_png.rs | 4++--
Aexamples/fractal_icns.rs | 28++++++++++++++++++++++++++++
Msrc/encode.rs | 6+++---
3 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/examples/convert_png.rs b/examples/convert_png.rs @@ -1,7 +1,7 @@ use clap::{App, Arg}; use std::io::prelude::*; use std::fs; -use image; +use image::{self}; use icns::encode::Encoder; fn main() { @@ -32,6 +32,6 @@ fn main() { let mut output = fs::File::create(&output) .expect("creating output file"); Encoder::new(&mut output) - .encode(&png) + .encode(&png.to_rgba()) .expect("encoding icns"); } \ No newline at end of file diff --git a/examples/fractal_icns.rs b/examples/fractal_icns.rs @@ -0,0 +1,27 @@ +use image::{self, ConvertBuffer}; +use std::fs; +use icns::encode::Encoder; + +fn main() { + let imgx = 800; + let imgy = 800; + + // Create a new ImgBuf with width: imgx and height: imgy. + let mut imgbuf = image::ImageBuffer::new(imgx, imgy); + + // Iterate over the coordinates and pixels of the image. + for (x, y, pixel) in imgbuf.enumerate_pixels_mut() { + let r = (0.3 * x as f32) as u8; + let b = (0.3 * y as f32) as u8; + *pixel = image::Rgb([r, 0, b]); + } + + // Open output file. + let mut output = fs::File::create("fractal.icns") + .expect("creating output file"); + + // Encode the image as icns. + Encoder::new(&mut output) + .encode(&imgbuf.convert()) + .expect("encoding icns"); +} +\ No newline at end of file diff --git a/src/encode.rs b/src/encode.rs @@ -1,7 +1,7 @@ use std::cmp::max; use std::io::{self, Write}; use byteorder::{BigEndian, WriteBytesExt}; -use image::{self, DynamicImage, RgbaImage, GenericImageView, png::PNGEncoder}; +use image::{self, RgbaImage, png::PNGEncoder}; use image::imageops::{resize, Lanczos3}; use rayon::{self, prelude::*}; @@ -18,7 +18,7 @@ impl<W: Write> Encoder<W> { Encoder{w} } // Encode the icns from a source png encoded buffer. - pub fn encode(&mut self, img: &DynamicImage) -> io::Result<()> { + pub fn encode(&mut self, img: &RgbaImage) -> io::Result<()> { IconSet::from(img).write_to(self.w.by_ref()) } } @@ -35,7 +35,7 @@ impl IconSet { /// Create an IconSet from the provided image. /// If width != height, the image will be resized using the largest side /// without preserving the aspect ratio. - fn from(img: &DynamicImage) -> Self { + fn from(img: &RgbaImage) -> Self { let kind = OSType::nearest(max(img.width(), img.height())); let icons: Vec<Icon> = kind.smaller_variants() .par_iter()