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
pub extern crate nsvg;
use std::{convert::From, path::Path, marker::Sized, io::{self, Write}};
pub use nsvg::{image::{self, DynamicImage, RgbaImage, GenericImage}, SvgImage};
pub use crate::ico::Ico;
pub use crate::icns::Icns;
pub use png_sequence::PngSequence;
pub type Size = u32;
pub type Result<T> = std::result::Result<T, Error>;
pub mod ico;
pub mod icns;
pub mod png_sequence;
pub mod resample;
pub mod prelude {
pub use crate::{Icon, Ico, Icns, PngSequence, SourceImage, FromPath, resample};
}
pub trait Icon<W: Write> {
fn new(w: W) -> Self;
fn add_icon<F: FnMut(&SourceImage, Size) -> Result<RgbaImage>>(
&mut self,
filter: F,
source: &SourceImage,
size: Size
) -> Result<()>;
}
pub trait FromPath where Self: Sized {
fn from_path<P: AsRef<Path>>(path: P) -> Option<Self>;
}
pub enum SourceImage {
Bitmap(DynamicImage),
Svg(SvgImage)
}
#[derive(Debug)]
pub enum Error {
Nsvg(nsvg::Error),
Image(image::ImageError),
Io(io::Error),
InvalidIcoSize(Size),
InvalidIcnsSize(Size),
SizeAlreadyIncluded(Size)
}
impl SourceImage {
pub fn width(&self) -> f32 {
match self {
SourceImage::Bitmap(bit) => bit.width() as f32,
SourceImage::Svg(svg) => svg.width()
}
}
pub fn height(&self) -> f32 {
match self {
SourceImage::Bitmap(bit) => bit.height() as f32,
SourceImage::Svg(svg) => svg.height()
}
}
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 FromPath for SourceImage {
fn from_path<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
}
}
}