1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
extern crate zip;
extern crate png_encode_mini;
extern crate ico;
extern crate icns;
pub extern crate nsvg;

mod write;

use std::{convert::From, path::Path, marker::Sized, io::{self, Write, Seek}, default::Default, collections::HashMap};
use nsvg::{image::{imageops, DynamicImage, RgbaImage, GenericImage, FilterType}, SvgImage};
use zip::result::ZipError;
pub use nsvg::image;

const MAX_ICO_SIZE: u16 = 265;
const VALID_ICNS_SIZES: [(u16, u16);7] = [(16, 16), (32, 32), (64, 64), (128, 128), (256, 256), (512, 512), (1024, 1024)];

pub type Size = (u16, u16);
pub type SourceMap<'a> = HashMap<IconOptions, &'a SourceImage>;
pub type Result<T> = std::result::Result<T, Error>;

pub mod prelude {
    pub use super::{Icon, IconOptions, IconType, SourceImage, ResamplingFilter, Crop, FromFile};
}

/// A generic representation of an icon.
pub struct Icon<'a> {
    source_map: SourceMap<'a>,
    icon_type: IconType
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
/// A representation of an entry's properties.
pub struct IconOptions {
    sizes: Vec<Size>,
    pub filter: ResamplingFilter,
    pub crop: Crop
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ResamplingFilter {
    Neareast,
    Linear,
    Cubic
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum IconType {
    Ico,
    Icns,
    PngSequence
}

/// A representation of a bitmap or an svg image.
pub enum SourceImage {
    Bitmap(DynamicImage),
    Svg(SvgImage)
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Crop {
    Proportional,
    Square
}

#[derive(Debug)]
pub enum Error {
    Nsvg(nsvg::Error),
    Image(image::ImageError),
    Zip(ZipError),
    Io(io::Error),
    SizeAlreadyIncluded(Size),
    InvalidIcoSize(Size),
    InvalidIcnsSize(Size)
}

/// Trait for constructing structs from a given path.
pub trait FromFile where Self: Sized {
    /// Constructs `Self` from a given path.
    fn from_file<P: AsRef<Path>>(path: P) -> Option<Self>;
}

/// Rasterizes a generic image to series of `RgbaImage`'s, conforming to the configuration options specifyed in the `options` argument.
pub trait Raster<E> {
    /// Rasterizes `self` to series of `RgbaImage`'s, conforming to the configuration options specifyed in the `options` argument.
    /// 
    /// Returns `Ok(Vec<RgbaImage>)` if the rasterazation was sucessfull. Otherwise returns `Err<E>`
    fn raster(&self, opts: &IconOptions) -> std::result::Result<Vec<RgbaImage>, E>;
}

impl<'a> Icon<'a> {
    /// Creates an `Icon` with the `Ico` icon type.
    /// # Arguments
    /// * `capacity` The target capacity for the underliyng `HashMap<IconOptions, &SourceImage>`.
    /// 
    /// It is important to note that although the returned `Icon` has the capacity specified, the `Icon` will have zero entries.
    /// For an explanation of the difference between length and capacity, see
    /// [*Capacity and reallocation*](https://doc.rust-lang.org/std/vec/struct.Vec.html#capacity-and-reallocation).
    pub fn ico(capacity: usize) -> Self {
        Icon { source_map: HashMap::with_capacity(capacity), icon_type: IconType::Ico }
    }

    /// Creates an `Icon` with the `Icns` icon type.
    /// # Arguments
    /// * `capacity` The target capacity for the underliyng `HashMap<IconOptions, &SourceImage>`.
    /// 
    /// It is important to note that although the returned `Icon` has the capacity specified, the `Icon` will have zero entries.
    /// For an explanation of the difference between length and capacity, see
    /// [*Capacity and reallocation*](https://doc.rust-lang.org/std/vec/struct.Vec.html#capacity-and-reallocation).
    pub fn icns(capacity: usize) -> Self {
        Icon { source_map: HashMap::with_capacity(capacity), icon_type: IconType::Icns }
    }

    /// Creates an `Icon` with the `PngSequece` icon type.
    /// # Arguments
    /// * `capacity` The target capacity for the underliyng `HashMap<IconOptions, &SourceImage>`.
    /// 
    /// It is important to note that although the returned `Icon` has the capacity specified, the `Icon` will have zero entries.
    /// For an explanation of the difference between length and capacity, see
    /// [*Capacity and reallocation*](https://doc.rust-lang.org/std/vec/struct.Vec.html#capacity-and-reallocation).
    pub fn png_sequence(capacity: usize) -> Self {
        Icon { source_map: HashMap::with_capacity(capacity), icon_type: IconType::PngSequence }
    }

    pub fn new(icon_type: IconType, capacity: usize) -> Self {
        Icon { source_map: HashMap::with_capacity(capacity), icon_type }
    }

    /// Adds an entry to the icon.
    /// 
    /// Returns `Err(Error::SizeAlreadyIncluded(Size))` if any of the sizes listed in `opts.sizes()` is already associated to another entry.
    /// Otherwise returns `Ok(())`.
    pub fn add_entry(&mut self, opts: IconOptions, source: &'a SourceImage) -> Result<()> {
        let sizes = self.sizes();

        if self.icon_type == IconType::Ico {
            for (w, h) in opts.sizes() {
                if w > MAX_ICO_SIZE || h > MAX_ICO_SIZE || w != h {
                    return Err(Error::InvalidIcoSize((w, h)));
                }
            }
        } else if self.icon_type == IconType::Icns {
            for size in opts.sizes() {
                if !VALID_ICNS_SIZES.contains(&size) {
                    return Err(Error::InvalidIcnsSize(size));
                }
            }
        }

        for size in opts.sizes() {
            if sizes.contains(&size) {
                return Err(Error::SizeAlreadyIncluded(size));
            }
        }

        self.source_map.insert(opts, source);

        Ok(())
    }

    /// Remove an entry from the icon.
    /// 
    /// Returns `Some(&SourceImage)` if the icon contains an entry associated with the `opts` argument. Returns `None` otherwise.
    pub fn remove_entry(&mut self, opts: &IconOptions) -> Option<&SourceImage> {
        self.source_map.remove(opts)
    }

    /// Returns a list of all sizes listed in all icon's entries.
    pub fn sizes(&self) -> Vec<Size> {
        let mut sizes = Vec::with_capacity(self.n_sizes());

        for (opt, _) in &self.source_map {
            let mut opt_sizes = opt.sizes().clone();
            sizes.append(&mut opt_sizes);
        }

        sizes
    }

    /// Returns the total number of sizes in all icon's entries.
    /// 
    /// This method avoids allocating unnecessary resources when accessing `self.sizes().len()`.
    pub fn n_sizes(&self) -> usize {
        self.source_map.iter().fold(0, |sum, (opts, _)| sum + opts.n_sizes())
    }

    pub fn raster(&self) -> Result<Vec<RgbaImage>> {
        let mut rasters = Vec::with_capacity(self.n_sizes());

        for (opts, source) in &self.source_map {
            match source {
                SourceImage::Svg(svg) => rasters.append(&mut svg.raster(&opts)?),
                SourceImage::Bitmap(bit) => rasters.append(&mut bit.raster(&opts)?)
            }
        }

        Ok(rasters)
    }

    /// Writes the icon to a file or stream.
    pub fn write<W: Write + Seek>(&self, w: W) -> Result<()> {
        let rasters = self.raster()?;

        match self.icon_type {
            IconType::Ico => write::ico(rasters, w),
            IconType::Icns => write::icns(rasters, w),
            IconType::PngSequence => write::png_sequence(rasters, w)
        }
    }
}

impl<'a> AsRef<SourceMap<'a>> for Icon<'a> {
    fn as_ref(&self) -> &SourceMap<'a> {
        &self.source_map
    }
}

impl IconOptions {
    /// Constructs a new `IconOptions`.
    pub fn new(
        sizes: Vec<Size>,
        filter: ResamplingFilter,
        crop: Crop
    ) -> Self {
        IconOptions { sizes, filter, crop }
    }

    /// Returns a copy of `self.sizes`.
    pub fn sizes(&self) -> Vec<Size> {
        self.sizes.clone()
    }

    /// Returns the lenght of `self.sizes`.
    /// 
    /// This method avoids allocating unnecessary resources when accessing `self.sizes().len()`.
    pub fn n_sizes(&self) -> usize {
        self.sizes.len()
    }
}

impl Default for IconOptions {
    fn default() -> Self {
        IconOptions { sizes: Vec::new(), filter: ResamplingFilter::Neareast, crop: Crop::Square }
    }
}

impl ResamplingFilter {
    pub fn from(filter: FilterType) -> Option<Self> {
        match filter {
            FilterType::Nearest    => Some(ResamplingFilter::Neareast),
            FilterType::Triangle   => Some(ResamplingFilter::Linear),
            FilterType::CatmullRom => Some(ResamplingFilter::Cubic),
            _ => None
        }
    }
}

impl Into<FilterType> for ResamplingFilter {
    fn into(self) -> FilterType {
        match self {
            ResamplingFilter::Neareast => FilterType::Nearest,
            ResamplingFilter::Linear   => FilterType::Triangle,
            ResamplingFilter::Cubic    => FilterType::CatmullRom
        }
    }
}

impl SourceImage {
    /// Returns the width of the original image in pixels.
    pub fn width(&self) -> f32 {
        match self {
            SourceImage::Bitmap(bit) => bit.width() as f32,
            SourceImage::Svg(svg) => svg.width()
        }
    }

    /// Returns the height of the original image in pixels.
    pub fn height(&self) -> f32 {
        match self {
            SourceImage::Bitmap(bit) => bit.height() as f32,
            SourceImage::Svg(svg) => svg.height()
        }
    }

    /// Returns the dimentions of the original image in pixels.
    pub fn dimentions(&self) -> (f32, f32) {
        match self {
            SourceImage::Bitmap(bit) => (bit.width() as f32, bit.height() as f32),
            SourceImage::Svg(svg) => (svg.width(), svg.height())
        }
    }
}

impl From<SvgImage> for SourceImage {
    fn from(svg: SvgImage) -> Self {
        SourceImage::Svg(svg)
    }
}

impl From<DynamicImage> for SourceImage {
    fn from(din: DynamicImage) -> Self {
        SourceImage::Bitmap(din)
    }
}

impl FromFile for SourceImage {
    fn from_file<P: AsRef<Path>>(path: P) -> Option<Self> {
        if let Ok(din) = image::open(&path) {
            Some(SourceImage::Bitmap(din))
        } else if let Ok(svg) = nsvg::parse_file(path.as_ref(), nsvg::Units::Pixel, 96.0) {
            Some(SourceImage::Svg(svg))
        } else {
            None
        }
    }
}

impl Raster<Error> for SvgImage {
    fn raster(&self, opts: &IconOptions) -> Result<Vec<RgbaImage>> {
        let mut images = Vec::with_capacity(opts.n_sizes());

        for (w, h) in opts.sizes() {
            match self.rasterize(f32::from(w) / self.width()) {
                Ok(buf) => if opts.crop == Crop::Square && (w as u32 != buf.width() || h as u32 != buf.height()) {
                    let din = DynamicImage::ImageRgba8(buf);
                    let reframed = reframe(&din, w as u32, h as u32);

                    images.push(reframed);
                } else {
                    images.push(buf);
                },
                Err(err) => match err {
                    nsvg::Error::IoError(err) => return Err(Error::Io(err)),
                    err => return Err(Error::Nsvg(err))
                }
            }
        }

        Ok(images)
    }
}

impl Raster<Error> for DynamicImage {
    fn raster(&self, opts: &IconOptions) -> Result<Vec<RgbaImage>> {
        let mut rasters = Vec::with_capacity(opts.n_sizes());

        for (w, h) in opts.sizes() {
            let reframed = reframe(&self.resize(w as u32, h as u32, opts.filter.into()), w as u32, h as u32);
            rasters.push(reframed);
        }

        Ok(rasters)
    }
}

fn reframe(source: &DynamicImage, w: u32, h: u32) -> RgbaImage {
    if source.width() == w && source.height() == h {
        source.to_rgba()
    } else {
        let mut output = DynamicImage::new_rgba8(w, h);
        let dx = (output.width() - source.width()) / 2;
        let dy = (output.height() - source.height()) / 2;

        imageops::overlay(&mut output, &source, dx, dy);
        output.to_rgba()
    }
}