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
use crate::{PixelFormat, Rect, VncError, VncEvent};
use std::future::Future;
use tokio::io::{AsyncRead, AsyncReadExt};
use tracing::error;
use super::{uninit_vec, zlib::ZlibReader};
fn read_run_length(reader: &mut ZlibReader) -> Result<usize, VncError> {
let mut run_length_part;
let mut run_length = 1;
loop {
run_length_part = reader.read_u8()?;
run_length += run_length_part as usize;
if 255 != run_length_part {
break;
}
}
Ok(run_length)
}
fn copy_true_color(
reader: &mut ZlibReader,
pixels: &mut Vec<u8>,
pad: bool,
compressed_bpp: usize,
bpp: usize,
) -> Result<(), VncError> {
let mut buf = [255; 4];
std::io::Read::read_exact(
reader,
&mut buf[pad as usize..pad as usize + compressed_bpp],
)?;
pixels.extend_from_slice(&buf[..bpp]);
Ok(())
}
fn copy_indexed(palette: &[u8], pixels: &mut Vec<u8>, bpp: usize, index: u8) {
let start = index as usize * bpp;
pixels.extend_from_slice(&palette[start..start + bpp])
}
pub struct Decoder {
decompressor: Option<flate2::Decompress>,
}
impl Decoder {
pub fn new() -> Self {
Self {
decompressor: Some(flate2::Decompress::new(true)),
}
}
pub async fn decode<S, F, Fut>(
&mut self,
format: &PixelFormat,
rect: &Rect,
input: &mut S,
output_func: &F,
) -> Result<(), VncError>
where
S: AsyncRead + Unpin,
F: Fn(VncEvent) -> Fut,
Fut: Future<Output = Result<(), VncError>>,
{
let data_len = input.read_u32().await? as usize;
let mut zlib_data = uninit_vec(data_len);
input.read_exact(&mut zlib_data).await?;
let decompressor = self.decompressor.take().unwrap();
let mut reader = ZlibReader::new(decompressor, &zlib_data);
let bpp = format.bits_per_pixel as usize / 8;
let pixel_mask = ((format.red_max as u32) << format.red_shift)
| ((format.green_max as u32) << format.green_shift)
| ((format.blue_max as u32) << format.blue_shift);
let (compressed_bpp, alpha_at_first) =
if format.bits_per_pixel == 32 && format.true_color_flag > 0 && format.depth <= 24 {
if pixel_mask & 0x000000ff == 0 {
// rgb at the most significant bits
// if format.big_endian_flag is set
// then decompressed data is excepted to be [rgb.0, rgb.1, rgb.2, alpha]
// otherwise the decompressed data should be [alpha, rgb.0, rgb.1, rgb.2]
(3, format.big_endian_flag == 0)
} else if pixel_mask & 0xff000000 == 0 {
// rgb at the least significant bits
// if format.big_endian_flag is set
// then decompressed data should be [alpha, rgb.0, rgb.1, rgb.2]
// otherwise the decompressed data should be [rgb.0, rgb.1, rgb.2, alpha]
(3, format.big_endian_flag > 0)
} else {
(4, false)
}
} else {
(bpp, false)
};
let mut palette = Vec::with_capacity(128 * bpp);
let mut y = 0;
while y < rect.height {
let height = if y + 64 > rect.height {
rect.height - y
} else {
64
};
let mut x = 0;
while x < rect.width {
let width = if x + 64 > rect.width {
rect.width - x
} else {
64
};
let pixel_count = height as usize * width as usize;
let control = reader.read_u8()?;
let is_rle = control & 0x80 > 0;
let palette_size = control & 0x7f;
palette.truncate(0);
for _ in 0..palette_size {
copy_true_color(
&mut reader,
&mut palette,
alpha_at_first,
compressed_bpp,
bpp,
)?
}
let mut pixels = Vec::with_capacity(pixel_count * bpp);
match (is_rle, palette_size) {
(false, 0) => {
// True Color pixels
for _ in 0..pixel_count {
copy_true_color(
&mut reader,
&mut pixels,
alpha_at_first,
compressed_bpp,
bpp,
)?
}
}
(false, 1) => {
// Color fill
for _ in 0..pixel_count {
copy_indexed(&palette, &mut pixels, bpp, 0)
}
}
(false, 2..=16) => {
// Indexed pixels
let bits_per_index = match palette_size {
2 => 1,
3..=4 => 2,
5..=16 => 4,
_ => unreachable!(),
};
let mut encoded = reader.read_u8()?;
let mask = (1 << bits_per_index) - 1;
for y in 0..height {
let mut shift = 8 - bits_per_index;
for _ in 0..width {
if shift < 0 {
shift = 8 - bits_per_index;
encoded = reader.read_u8()?;
}
let idx = (encoded >> shift) & mask;
copy_indexed(&palette, &mut pixels, bpp, idx);
shift -= bits_per_index;
}
if shift < 8 - bits_per_index && y < height - 1 {
encoded = reader.read_u8()?;
}
}
}
(true, 0) => {
// True Color RLE
let mut count = 0;
let mut pixel = Vec::new();
while count < pixel_count {
pixel.truncate(0);
copy_true_color(
&mut reader,
&mut pixel,
alpha_at_first,
compressed_bpp,
bpp,
)?;
let run_length = read_run_length(&mut reader)?;
for _ in 0..run_length {
pixels.extend(&pixel)
}
count += run_length;
}
}
(true, 2..=127) => {
// Indexed RLE
let mut count = 0;
while count < pixel_count {
let control = reader.read_u8()?;
let longer_than_one = control & 0x80 > 0;
let index = control & 0x7f;
let run_length = if longer_than_one {
read_run_length(&mut reader)?
} else {
1
};
for _ in 0..run_length {
copy_indexed(&palette, &mut pixels, bpp, index);
}
count += run_length;
}
}
(x, y) => {
error!("ZRLE subencoding error {:?}", (x, y));
return Err(VncError::InvalidImageData);
}
}
output_func(VncEvent::RawImage(
Rect {
x: rect.x + x,
y: rect.y + y,
width,
height,
},
pixels,
))
.await?;
x += width;
}
y += height;
}
self.decompressor = Some(reader.into_inner()?);
Ok(())
}
}