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
use byteorder::{ReadBytesExt, LittleEndian};
use std::io::{Read, Seek, SeekFrom};
use color::ColorType;
use image::{DecodingResult, ImageResult, ImageDecoder, ImageError};
use bmp::BMPDecoder;
use png::PNGDecoder;
use self::InnerDecoder::*;
const PNG_SIGNATURE: [u8; 8] = [137, 80, 78, 71, 13, 10, 26, 10];
pub struct ICODecoder<R: Read> {
selected_entry: DirEntry,
inner_decoder: InnerDecoder<R>,
}
enum InnerDecoder<R: Read> {
BMP(BMPDecoder<R>),
PNG(PNGDecoder<R>)
}
#[derive(Clone, Copy, Default)]
struct DirEntry {
width: u8,
height: u8,
color_count: u8,
reserved: u8,
num_color_planes: u16,
bits_per_pixel: u16,
image_length: u32,
image_offset: u32,
}
impl<R: Read + Seek> ICODecoder<R> {
pub fn new(mut r: R) -> ImageResult<ICODecoder<R>> {
let entries = try!(read_entries(&mut r));
let entry = try!(best_entry(entries));
let decoder = try!(entry.decoder(r));
Ok(ICODecoder {
selected_entry: entry,
inner_decoder: decoder,
})
}
}
fn read_entries<R: Read>(r: &mut R) -> ImageResult<Vec<DirEntry>> {
let _reserved = try!(r.read_u16::<LittleEndian>());
let _type = try!(r.read_u16::<LittleEndian>());
let count = try!(r.read_u16::<LittleEndian>());
(0..count).map(|_| read_entry(r)).collect()
}
fn read_entry<R: Read>(r: &mut R) -> ImageResult<DirEntry> {
let mut entry = DirEntry::default();
entry.width = try!(r.read_u8());
entry.height = try!(r.read_u8());
entry.color_count = try!(r.read_u8());
entry.reserved = try!(r.read_u8());
entry.num_color_planes = try!(r.read_u16::<LittleEndian>());
entry.bits_per_pixel = try!(r.read_u16::<LittleEndian>());
entry.image_length = try!(r.read_u32::<LittleEndian>());
entry.image_offset = try!(r.read_u32::<LittleEndian>());
Ok(entry)
}
fn best_entry(mut entries: Vec<DirEntry>) -> ImageResult<DirEntry> {
let mut best = try!(entries.pop().ok_or(ImageError::ImageEnd));
let mut best_score = (best.bits_per_pixel, best.real_width() as u32 * best.real_height() as u32);
for entry in entries.into_iter() {
let score = (entry.bits_per_pixel, entry.real_width() as u32 * entry.real_height() as u32);
if score > best_score {
best = entry;
best_score = score;
}
}
Ok(best)
}
impl DirEntry {
fn real_width(&self) -> u16 {
match self.width {
0 => 256,
w => w as u16
}
}
fn real_height(&self) -> u16 {
match self.height {
0 => 256,
h => h as u16
}
}
fn seek_to_start<R: Read + Seek>(&self, r: &mut R) -> ImageResult<()> {
try!(r.seek(SeekFrom::Start(self.image_offset as u64)));
Ok(())
}
fn is_png<R: Read + Seek>(&self, r: &mut R) -> ImageResult<bool> {
try!(self.seek_to_start(r));
let mut signature = [0u8; 8];
if try!(r.read(&mut signature)) != signature.len() {
return Err(ImageError::ImageEnd)
}
Ok(signature == PNG_SIGNATURE)
}
fn decoder<R: Read + Seek>(&self, mut r: R) -> ImageResult<InnerDecoder<R>> {
let is_png = try!(self.is_png(&mut r));
try!(self.seek_to_start(&mut r));
if is_png {
Ok(PNG(PNGDecoder::new(r)))
} else {
let mut decoder = BMPDecoder::new(r);
try!(decoder.read_metadata_in_ico_format(self.image_offset));
Ok(BMP(decoder))
}
}
}
impl<R: Read + Seek> ImageDecoder for ICODecoder<R> {
fn dimensions(&mut self) -> ImageResult<(u32, u32)> {
match self.inner_decoder {
BMP(ref mut decoder) => decoder.dimensions(),
PNG(ref mut decoder) => decoder.dimensions()
}
}
fn colortype(&mut self) -> ImageResult<ColorType> {
match self.inner_decoder {
BMP(ref mut decoder) => decoder.colortype(),
PNG(ref mut decoder) => decoder.colortype()
}
}
fn row_len(&mut self) -> ImageResult<usize> {
match self.inner_decoder {
BMP(ref mut decoder) => decoder.row_len(),
PNG(ref mut decoder) => decoder.row_len()
}
}
fn read_scanline(&mut self, buf: &mut [u8]) -> ImageResult<u32> {
match self.inner_decoder {
BMP(ref mut decoder) => decoder.read_scanline(buf),
PNG(ref mut decoder) => decoder.read_scanline(buf)
}
}
fn read_image(&mut self) -> ImageResult<DecodingResult> {
match self.inner_decoder {
PNG(ref mut decoder) => {
decoder.read_image()
}
BMP(ref mut decoder) => {
let (width, height) = try!(decoder.dimensions());
if try!(decoder.colortype()) != ColorType::RGBA(8) {
return Err(ImageError::UnsupportedError("Unsupported color type".to_string()))
}
let mut pixel_data = match try!(decoder.read_image()) {
DecodingResult::U8(v) => v,
_ => unreachable!()
};
let r = decoder.reader();
let mask_start = try!(r.seek(SeekFrom::Current(0)));
let mask_end = (self.selected_entry.image_offset + self.selected_entry.image_length) as u64;
let mask_length = mask_end - mask_start;
if mask_length > 0 {
let mask_row_bytes = ((width + 31) / 32) * 4;
let expected_length = (mask_row_bytes * height) as u64;
if mask_length < expected_length {
return Err(ImageError::ImageEnd)
}
for y in 0..height {
let mut x = 0;
for _ in 0..mask_row_bytes {
let mask_byte = try!(r.read_u8());
for bit in (0..8).rev() {
if x >= width { break }
if mask_byte & (1 << bit) != 0 {
pixel_data[((height - y - 1) * width + x) as usize * 4 + 3] = 0;
}
x += 1;
}
}
}
}
Ok(DecodingResult::U8(pixel_data))
}
}
}
}