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
use byteorder::{LittleEndian, WriteBytesExt};
use std::borrow::Cow;
use std::io::{self, Write};
use crate::color::ColorType;
use crate::error::{ImageError, ImageResult, ParameterError, ParameterErrorKind};
use crate::image::ImageEncoder;
use crate::codecs::png::PngEncoder;
const ICO_IMAGE_TYPE: u16 = 1;
const ICO_ICONDIR_SIZE: u32 = 6;
const ICO_DIRENTRY_SIZE: u32 = 16;
pub struct IcoEncoder<W: Write> {
w: W,
}
pub struct IcoFrame<'a> {
encoded_image: Cow<'a, [u8]>,
width: u8,
height: u8,
color_type: ColorType,
}
impl<'a> IcoFrame<'a> {
pub fn with_encoded(
encoded_image: impl Into<Cow<'a, [u8]>>,
width: u32,
height: u32,
color_type: ColorType,
) -> ImageResult<Self> {
let encoded_image = encoded_image.into();
if !(1..=256).contains(&width) {
return Err(ImageError::Parameter(ParameterError::from_kind(
ParameterErrorKind::Generic(format!(
"the image width must be `1..=256`, instead width {} was provided",
width,
)),
)));
}
if !(1..=256).contains(&height) {
return Err(ImageError::Parameter(ParameterError::from_kind(
ParameterErrorKind::Generic(format!(
"the image height must be `1..=256`, instead height {} was provided",
height,
)),
)));
}
Ok(Self {
encoded_image,
width: width as u8,
height: height as u8,
color_type,
})
}
pub fn as_png(buf: &[u8], width: u32, height: u32, color_type: ColorType) -> ImageResult<Self> {
let mut image_data: Vec<u8> = Vec::new();
PngEncoder::new(&mut image_data).write_image(buf, width, height, color_type)?;
let frame = Self::with_encoded(image_data, width, height, color_type)?;
Ok(frame)
}
}
impl<W: Write> IcoEncoder<W> {
pub fn new(w: W) -> IcoEncoder<W> {
IcoEncoder { w }
}
#[deprecated = "Use `IcoEncoder::write_image` instead. Beware that `write_image` has a different endianness convention"]
pub fn encode(self, data: &[u8], width: u32, height: u32, color: ColorType) -> ImageResult<()> {
let mut image_data: Vec<u8> = Vec::new();
#[allow(deprecated)]
PngEncoder::new(&mut image_data).encode(data, width, height, color)?;
let image = IcoFrame::with_encoded(&image_data, width, height, color)?;
self.encode_images(&[image])
}
pub fn encode_images(mut self, images: &[IcoFrame<'_>]) -> ImageResult<()> {
if !(1..=usize::from(u16::MAX)).contains(&images.len()) {
return Err(ImageError::Parameter(ParameterError::from_kind(
ParameterErrorKind::Generic(format!(
"the number of images must be `1..=u16::MAX`, instead {} images were provided",
images.len(),
)),
)));
}
let num_images = images.len() as u16;
let mut offset = ICO_ICONDIR_SIZE + (ICO_DIRENTRY_SIZE * (images.len() as u32));
write_icondir(&mut self.w, num_images)?;
for image in images {
write_direntry(
&mut self.w,
image.width,
image.height,
image.color_type,
offset,
image.encoded_image.len() as u32,
)?;
offset += image.encoded_image.len() as u32;
}
for image in images {
self.w.write_all(&image.encoded_image)?;
}
Ok(())
}
}
impl<W: Write> ImageEncoder for IcoEncoder<W> {
fn write_image(
self,
buf: &[u8],
width: u32,
height: u32,
color_type: ColorType,
) -> ImageResult<()> {
let image = IcoFrame::as_png(buf, width, height, color_type)?;
self.encode_images(&[image])
}
}
fn write_icondir<W: Write>(w: &mut W, num_images: u16) -> io::Result<()> {
w.write_u16::<LittleEndian>(0)?;
w.write_u16::<LittleEndian>(ICO_IMAGE_TYPE)?;
w.write_u16::<LittleEndian>(num_images)?;
Ok(())
}
fn write_direntry<W: Write>(
w: &mut W,
width: u8,
height: u8,
color: ColorType,
data_start: u32,
data_size: u32,
) -> io::Result<()> {
w.write_u8(width)?;
w.write_u8(height)?;
w.write_u8(0)?;
w.write_u8(0)?;
w.write_u16::<LittleEndian>(0)?;
w.write_u16::<LittleEndian>(color.bits_per_pixel())?;
w.write_u32::<LittleEndian>(data_size)?;
w.write_u32::<LittleEndian>(data_start)?;
Ok(())
}