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
use crate::color::RgbColor;
use crate::escape::{Sixel, SixelData};
const MAX_PARAMS: usize = 5;
const MAX_SIXEL_SIZE: usize = 100_000_000;
pub struct SixelBuilder {
pub sixel: Sixel,
params: [i64; MAX_PARAMS],
param_no: usize,
current_command: u8,
}
impl SixelBuilder {
pub fn new(params: &[i64]) -> Self {
let pan = match params.get(0).unwrap_or(&0) {
7 | 8 | 9 => 1,
0 | 1 | 5 | 6 => 2,
3 | 4 => 3,
2 => 5,
_ => 2,
};
let background_is_transparent = match params.get(1).unwrap_or(&0) {
1 => true,
_ => false,
};
let horizontal_grid_size = params.get(2).map(|&x| x);
Self {
sixel: Sixel {
pan,
pad: 1,
pixel_width: None,
pixel_height: None,
background_is_transparent,
horizontal_grid_size,
data: vec![],
},
param_no: 0,
params: [-1; MAX_PARAMS],
current_command: 0,
}
}
pub fn push(&mut self, data: u8) {
match data {
b'$' => {
self.finish_command();
self.sixel.data.push(SixelData::CarriageReturn);
}
b'-' => {
self.finish_command();
self.sixel.data.push(SixelData::NewLine);
}
0x3f..=0x7e if self.current_command == b'!' => {
self.sixel.data.push(SixelData::Repeat {
repeat_count: self.params[0] as u32,
data: data - 0x3f,
});
self.finish_command();
}
0x3f..=0x7e => {
self.finish_command();
self.sixel.data.push(SixelData::Data(data - 0x3f));
}
b'#' | b'!' | b'"' => {
self.finish_command();
self.current_command = data;
}
b'0'..=b'9' if self.current_command != 0 => {
let pos = self.param_no;
if pos >= MAX_PARAMS {
return;
}
if self.params[pos] == -1 {
self.params[pos] = 0;
}
self.params[pos] = self.params[pos]
.saturating_mul(10)
.saturating_add((data - b'0') as i64);
}
b';' if self.current_command != 0 => {
let pos = self.param_no;
if pos >= MAX_PARAMS {
return;
}
self.param_no += 1;
}
_ => {
// Invalid; break out of current command
self.finish_command();
}
}
}
fn finish_command(&mut self) {
match self.current_command {
b'#' if self.param_no >= 4 => {
// Define a color
let color_number = self.params[0] as u16;
let system = self.params[1] as u16;
let a = self.params[2] as u16;
let b = self.params[3] as u8;
let c = self.params[4] as u8;
if system == 1 {
self.sixel.data.push(SixelData::DefineColorMapHSL {
color_number,
hue_angle: a,
lightness: b,
saturation: c,
});
} else {
let r = a as f32 * 255.0 / 100.;
let g = b as f32 * 255.0 / 100.;
let b = c as f32 * 255.0 / 100.;
let rgb = RgbColor::new_8bpc(r as u8, g as u8, b as u8); // FIXME: from linear
self.sixel
.data
.push(SixelData::DefineColorMapRGB { color_number, rgb });
}
}
b'#' => {
// Use a color
let color_number = self.params[0] as u16;
self.sixel
.data
.push(SixelData::SelectColorMapEntry(color_number));
}
b'"' => {
// Set raster attributes
let pan = if self.params[0] == -1 {
2
} else {
self.params[0]
};
let pad = if self.params[1] == -1 {
1
} else {
self.params[1]
};
let pixel_width = self.params[2];
let pixel_height = self.params[3];
self.sixel.pan = pan;
self.sixel.pad = pad;
if self.param_no >= 3 {
self.sixel.pixel_width.replace(pixel_width as u32);
self.sixel.pixel_height.replace(pixel_height as u32);
let (size, overflow) =
(pixel_width as usize).overflowing_mul(pixel_height as usize);
// Ideally we'd just use `try_reserve` here, but that is
// nightly Rust only at the time of writing this comment:
// <https://github.com/rust-lang/rust/issues/48043>
if size > MAX_SIXEL_SIZE || overflow {
log::error!(
"Ignoring sixel data {}x{} because {} bytes \
either overflows or exceeds the max allowed {}",
pixel_width,
pixel_height,
size,
MAX_SIXEL_SIZE
);
self.sixel.pixel_width = None;
self.sixel.pixel_height = None;
self.sixel.data.clear();
return;
}
self.sixel.data.reserve(size);
}
}
_ => {}
}
self.param_no = 0;
self.params = [-1; MAX_PARAMS];
self.current_command = 0;
}
pub fn finish(&mut self) {
self.finish_command();
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::escape::parser::Parser;
use crate::escape::{Action, Esc, EscCode};
use k9::assert_equal as assert_eq;
#[test]
fn sixel() {
let mut p = Parser::new();
let actions = p.parse_as_vec(b"\x1bP1;2;3;q@\x1b\\");
assert_eq!(
vec![
Action::Sixel(Box::new(Sixel {
pan: 2,
pad: 1,
pixel_width: None,
pixel_height: None,
background_is_transparent: false,
horizontal_grid_size: Some(3),
data: vec![SixelData::Data(1)]
})),
Action::Esc(Esc::Code(EscCode::StringTerminator)),
],
actions
);
assert_eq!(format!("{}", actions[0]), "\x1bP0;0;3q@");
// This is the "HI" example from wikipedia
let mut p = Parser::new();
let actions = p.parse_as_vec(
b"\x1bPq\
#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0\
#1~~@@vv@@~~@@~~$\
#2??}}GG}}??}}??-\
#1!14@\
\x1b\\",
);
assert_eq!(
format!("{}", actions[0]),
"\x1bP0;0q\
#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0\
#1~~@@vv@@~~@@~~$\
#2??}}GG}}??}}??-\
#1!14@"
);
use SixelData::*;
assert_eq!(
vec![
Action::Sixel(Box::new(Sixel {
pan: 2,
pad: 1,
pixel_width: None,
pixel_height: None,
background_is_transparent: false,
horizontal_grid_size: None,
data: vec![
DefineColorMapRGB {
color_number: 0,
rgb: RgbColor::new_8bpc(0, 0, 0)
},
DefineColorMapRGB {
color_number: 1,
rgb: RgbColor::new_8bpc(255, 255, 0)
},
DefineColorMapRGB {
color_number: 2,
rgb: RgbColor::new_8bpc(0, 255, 0)
},
SelectColorMapEntry(1),
Data(63),
Data(63),
Data(1),
Data(1),
Data(55),
Data(55),
Data(1),
Data(1),
Data(63),
Data(63),
Data(1),
Data(1),
Data(63),
Data(63),
CarriageReturn,
SelectColorMapEntry(2),
Data(0),
Data(0),
Data(62),
Data(62),
Data(8),
Data(8),
Data(62),
Data(62),
Data(0),
Data(0),
Data(62),
Data(62),
Data(0),
Data(0),
NewLine,
SelectColorMapEntry(1),
Repeat {
repeat_count: 14,
data: 1
}
]
})),
Action::Esc(Esc::Code(EscCode::StringTerminator)),
],
actions
);
}
}