icns-rs

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

commit d2a1b8eb506edb2aa7cb3925755e6095ed59e136
parent ae24a87564d06c2847d6c110faf153a672cabf40
Author: = <=>
Date:   Fri, 23 Aug 2019 19:39:31 +1200

[~] Upgrade deps.

Diffstat:
MCargo.toml | 11++---------
Mexamples/fractal_icns.rs | 2+-
Msrc/encode.rs | 87+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Msrc/main.rs | 2+-
4 files changed, 49 insertions(+), 53 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -18,14 +18,8 @@ byteorder = "1.3.2" rayon = "1.1.0" clap = "2.33.0" deflate = "0.7.20" - -[dependencies.png] -git = "https://github.com/JackMordaunt/image-png.git" -branch = "deflate/specify-compression" - -# [dependencies.png] -# path = "../../forks/image-png" +png = "0.15.0" [dev-dependencies] clap = "2.33.0" -num-complex = "0.2.3" -\ No newline at end of file +num-complex = "0.2.3" diff --git a/examples/fractal_icns.rs b/examples/fractal_icns.rs @@ -30,7 +30,7 @@ fn main() { } let pixel = imgbuf.get_pixel_mut(x, y); - let data = (*pixel as image::Rgb<u8>).data; + let data = (*pixel as image::Rgb<u8>).0; *pixel = image::Rgb([data[0], i as u8, data[2]]); } } diff --git a/src/encode.rs b/src/encode.rs @@ -1,31 +1,30 @@ -use std::cmp::max; -use std::io::{self, Write}; use byteorder::{BigEndian, WriteBytesExt}; -use image::{self, RgbaImage}; use image::imageops::{resize, Lanczos3}; +use image::{self, RgbaImage}; +use png; use rayon::{self, prelude::*}; -use png::{self, HasParameters}; -use deflate; +use std::cmp::max; +use std::io::{self, Write}; use crate::os_type::OSType; -/// Encoder encodes icns image into the provided writer. +/// Encoder encodes icns image into the provided writer. pub struct Encoder<W: Write> { - w: W + w: W, } impl<W: Write> Encoder<W> { - /// Create a new encoder that writes to ```w```. + /// Create a new encoder that writes to ```w```. pub fn new(w: W) -> Self { - Encoder{w} + Encoder { w } } - // Encode the icns from a source png encoded buffer. + // Encode the icns from a source png encoded buffer. pub fn encode(&mut self, img: &RgbaImage) -> io::Result<()> { IconSet::from(img).write_to(self.w.by_ref()) } } -/// IconSet encodes a vector of icons. +/// IconSet encodes a vector of icons. struct IconSet { icons: Vec<Icon>, } @@ -34,12 +33,13 @@ struct IconSet { const ICONSET_MAGIC: [char; 4] = ['i', 'c', 'n', 's']; impl IconSet { - /// Create an IconSet from the provided image. - /// If width != height, the image will be resized using the largest side + /// 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: &RgbaImage) -> Self { let kind = OSType::nearest(max(img.width(), img.height())); - let icons: Vec<Icon> = kind.smaller_variants() + let icons: Vec<Icon> = kind + .smaller_variants() .par_iter() .map(|v| Icon { kind: v.clone(), @@ -48,27 +48,29 @@ impl IconSet { .collect(); IconSet { icons } } - /// Write the encoded iconset to writer ```w```. + /// Write the encoded iconset to writer ```w```. fn write_to(self, mut wr: impl Write) -> io::Result<()> { - // Pre-buffer the encoded icons so we can calculate the final size. + // Pre-buffer the encoded icons so we can calculate the final size. let mut buffer: Vec<u8> = vec![]; for icon in self.icons { icon.write_to(&mut buffer)?; } // Write the 4-byte magic bytes to identify this as an icns image. - wr.write_all(&ICONSET_MAGIC - .into_iter() - .map(|c| *c as u8) - .collect::<Vec<u8>>())?; + wr.write_all( + &ICONSET_MAGIC + .into_iter() + .map(|c| *c as u8) + .collect::<Vec<u8>>(), + )?; // Write the 4-byte container size in bytes. wr.write_u32::<BigEndian>((buffer.len() + 8) as u32)?; - // Write the encoded icons. + // Write the encoded icons. wr.write_all(&buffer)?; Ok(()) } } -/// Icon encodes a single icon. +/// Icon encodes a single icon. struct Icon { kind: OSType, image: RgbaImage, @@ -77,26 +79,29 @@ struct Icon { impl Icon { /// Write the encoded icon to writer ```w```. fn write_to(self, mut wr: impl Write) -> io::Result<()> { - // Pre-buffer the png image so we can calculate size total. + // Pre-buffer the png image so we can calculate size total. let (width, height) = (self.image.width(), self.image.height()); let mut buffer: Vec<u8> = vec![]; - PNGEncoder::new(&mut buffer) - .encode( - self.image.into_raw().as_ref(), - width, - height, - png::ColorType::RGBA, - png::BitDepth::Eight, - )?; + PNGEncoder::new(&mut buffer).encode( + self.image.into_raw().as_ref(), + width, + height, + png::ColorType::RGBA, + png::BitDepth::Eight, + )?; // Write the 4-byte OSType identifier. // Coerce array of characters into slice of bytes through a vec deref. - wr.write_all(&self.kind.header() - .into_iter() - .map(|c| *c as u8) - .collect::<Vec<u8>>())?; + wr.write_all( + &self + .kind + .header() + .into_iter() + .map(|c| *c as u8) + .collect::<Vec<u8>>(), + )?; // Write the 4-byte icon size in bytes (data.len + header.len). wr.write_u32::<BigEndian>((buffer.len() + 8) as u32)?; - // Write the image data. + // Write the image data. wr.write_all(&buffer)?; Ok(()) } @@ -120,11 +125,10 @@ impl<W: Write> PNGEncoder<W> { bits: png::BitDepth, ) -> io::Result<()> { let mut encoder = png::Encoder::new(self.w, width, height); - encoder - .set(ct) - .set(bits) - .set(png::Compression::Default); + encoder.set_color(ct); + encoder.set_depth(bits); + encoder.set_compression(png::Compression::Default); let mut writer = encoder.write_header()?; writer.write_image_data(data).map_err(|e| e.into()) } -} -\ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs @@ -5,7 +5,7 @@ use std::io::{self, BufReader, BufWriter}; use std::fs::File; use clap::{App, Arg}; use image; -use icns::Encoder; +use encode::Encoder; fn main() { let cli = App::new("icnsify")