commit b3e1b6afe5acb2401dbaa3c1aace34a314d18fd1
parent 3249860ced966ec9a9589bf53ff17af23617f8fd
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date: Mon, 31 Dec 2018 12:08:10 +1300
[+] Parallelise image resizing with rayon.
Diffstat:
2 files changed, 9 insertions(+), 11 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
image = "0.20.1"
byteorder = "1.2.7"
+rayon = "1.0.3"
[dev-dependencies]
clap = "2.32.0"
\ No newline at end of file
diff --git a/src/encode.rs b/src/encode.rs
@@ -3,6 +3,7 @@ use std::io::{self, Write};
use byteorder::{BigEndian, WriteBytesExt};
use image::{self, DynamicImage, RgbaImage, GenericImageView, png::PNGEncoder};
use image::imageops::{resize, Lanczos3};
+use rayon::{self, prelude::*};
use crate::os_type::OSType;
@@ -34,19 +35,15 @@ 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.
- /// TODO: - Reject images smaller than 16x16 pixels.
- /// - Parallelise the resizing via rayon.
fn from(img: &DynamicImage) -> Self {
- let mut icons: Vec<Icon> = vec![];
let kind = OSType::nearest(max(img.width(), img.height()));
- for variant in kind.smaller_variants() {
- let buffer = resize(img, variant.size(), variant.size(), Lanczos3);
- let icon = Icon{
- kind: variant,
- image: buffer,
- };
- icons.push(icon);
- }
+ let icons: Vec<Icon> = kind.smaller_variants()
+ .par_iter()
+ .map(|v| Icon {
+ kind: v.clone(),
+ image: resize(img, v.size(), v.size(), Lanczos3),
+ })
+ .collect();
IconSet { icons }
}
/// Write the encoded iconset to writer ```w```.