Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ reads that marker.
its colour, as xterm does. Decoding failed with `termlens does not
decode sixel HLS colours`, naming a colour space the image did not
use. HLS is still refused. (#846)
- A sixel colour definition with an RGB component over 100, as an encoder
that writes 0-255 values gives, is ignored and the register keeps its
colour, as xterm does. Each component was clamped to 100 and applied,
so the image decoded in colours xterm never shows. (#845)
- Both published crates, `termlens` and `termlens-cli`, ship `LICENSE-MIT`
and `LICENSE-APACHE`. They are `MIT OR Apache-2.0`, and both licences ask
for their text in every copy, but the texts sat only at the repository
Expand Down
30 changes: 28 additions & 2 deletions crates/termlens/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,8 @@ fn decode_sixel(data: &[u8], background: Option<(u32, u32)>) -> Result<Bitmap, D

fn percent(value: u32) -> u8 {
// Sixel colour components are percentages, and the rounding has to
// match what a terminal does or every assertion lands one off.
// match what a terminal does or every assertion lands one off. A
// definition with a component over 100 never gets here.
((value.min(100) * 255 + 50) / 100) as u8
}
fn rgb([r, g, b]: [u32; 3]) -> [u8; 4] {
Expand Down Expand Up @@ -893,7 +894,15 @@ fn decode_sixel(data: &[u8], background: Option<(u32, u32)>) -> Result<Bitmap, D
// and a guessed conversion would put wrong colours
// into an assertion that reads as exact.
1 => return Err(DecodeError::Unsupported("sixel HLS colours")),
2 => registers[index] = rgb([values[2], values[3], values[4]]),
2 => {
// A component over 100 % is a data error to
// xterm (`graphics_sixel.c`), which ignores the
// definition and leaves the register as it was.
let components = [values[2], values[3], values[4]];
if components.iter().all(|&value| value <= 100) {
registers[index] = rgb(components);
}
}
// Any other colour space is a data error to xterm
// (`graphics_sixel.c`), which ignores the definition
// and leaves the register as it was.
Expand Down Expand Up @@ -2923,6 +2932,23 @@ mod tests {
assert_eq!(bitmap.colours(), vec![([51, 51, 204, 255], 18)]);
}

#[cfg(feature = "decode")]
#[test]
fn a_colour_with_a_component_over_100_is_ignored() {
// Register 1 is defined blue, then given a red component of 150:
// xterm ignores that definition, so the columns stay blue.
let bitmap = sixel_payload(b"#1;2;0;0;100#1;2;150;0;0$#1!3~")
.decode()
.expect("decodes");
let top: Vec<[u8; 4]> = (0..bitmap.width())
.map(|x| bitmap.pixel(x, 0).unwrap())
.collect();
assert_eq!(top, [[0, 0, 255, 255]; 3]);
// 100 itself is in range.
let bitmap = sixel_payload(b"#1;2;100;0;0#1~").decode().expect("decodes");
assert_eq!(bitmap.pixel(0, 0), Some([255, 0, 0, 255]));
}

#[test]
fn the_debug_rendering_stays_short_enough_for_a_log() {
let payload = kitty_payload(b"a=T,f=32,o=z,s=954,v=133,i=7,c=106,r=7", &[b'A'; 4096]);
Expand Down
Loading