Skip to content

Commit f657f85

Browse files
committed
fix image rendering
1 parent f58517c commit f657f85

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

image/src/iterm.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::get_dimensions;
12
use anyhow::Result;
23
use base64::{engine, Engine};
34
use image::{imageops::FilterType, DynamicImage};
4-
use libc::{ioctl, winsize, STDOUT_FILENO, TIOCGWINSZ};
55
use std::env;
66
use std::io::Cursor;
77

@@ -31,17 +31,13 @@ impl super::ImageBackend for ITermBackend {
3131
image: &DynamicImage,
3232
_colors: usize,
3333
) -> Result<String> {
34-
let tty_size = unsafe {
35-
let tty_size: winsize = std::mem::zeroed();
36-
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);
37-
tty_size
38-
};
34+
let tty_size = unsafe { get_dimensions() };
3935
let width_ratio = f64::from(tty_size.ws_col) / f64::from(tty_size.ws_xpixel);
4036
let height_ratio = f64::from(tty_size.ws_row) / f64::from(tty_size.ws_ypixel);
4137

4238
// resize image to fit the text height with the Lanczos3 algorithm
4339
let image = image.resize(
44-
u32::max_value(),
40+
u32::MAX,
4541
(lines.len() as f64 / height_ratio) as u32,
4642
FilterType::Lanczos3,
4743
);

image/src/kitty.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use crate::get_dimensions;
12
use anyhow::Result;
23
use base64::{engine, Engine};
34
use image::{imageops::FilterType, DynamicImage};
45
use libc::{
5-
c_void, ioctl, poll, pollfd, read, tcgetattr, tcsetattr, termios, winsize, ECHO, ICANON,
6-
POLLIN, STDIN_FILENO, STDOUT_FILENO, TCSANOW, TIOCGWINSZ,
6+
c_void, poll, pollfd, read, tcgetattr, tcsetattr, termios, ECHO, ICANON, POLLIN, STDIN_FILENO,
7+
TCSANOW,
78
};
89
use std::io::{stdout, Write};
910
use std::time::Instant;
@@ -91,17 +92,13 @@ impl super::ImageBackend for KittyBackend {
9192
image: &DynamicImage,
9293
_colors: usize,
9394
) -> Result<String> {
94-
let tty_size = unsafe {
95-
let tty_size: winsize = std::mem::zeroed();
96-
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);
97-
tty_size
98-
};
95+
let tty_size = unsafe { get_dimensions() };
9996
let width_ratio = f64::from(tty_size.ws_col) / f64::from(tty_size.ws_xpixel);
10097
let height_ratio = f64::from(tty_size.ws_row) / f64::from(tty_size.ws_ypixel);
10198

10299
// resize image to fit the text height with the Lanczos3 algorithm
103100
let image = image.resize(
104-
u32::max_value(),
101+
u32::MAX,
105102
(lines.len() as f64 / height_ratio) as u32,
106103
FilterType::Lanczos3,
107104
);

image/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,18 @@ pub fn get_image_backend(image_protocol: ImageProtocol) -> Option<Box<dyn ImageB
4848
let backend = None;
4949
backend
5050
}
51+
52+
#[cfg(not(windows))]
53+
unsafe fn get_dimensions() -> libc::winsize {
54+
use libc::{ioctl, winsize, STDOUT_FILENO, TIOCGWINSZ};
55+
use std::mem::zeroed;
56+
57+
let mut window: winsize = zeroed();
58+
let result = ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut window);
59+
60+
if result == -1 {
61+
zeroed()
62+
} else {
63+
window
64+
}
65+
}

image/src/sixel.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
use crate::get_dimensions;
12
use anyhow::{Context, Result};
23
use color_quant::NeuQuant;
34
use image::{
45
imageops::{colorops, FilterType},
56
DynamicImage, GenericImageView, ImageBuffer, Pixel, Rgb,
67
};
78
use libc::{
8-
c_void, ioctl, poll, pollfd, read, tcgetattr, tcsetattr, termios, winsize, ECHO, ICANON,
9-
POLLIN, STDIN_FILENO, STDOUT_FILENO, TCSANOW, TIOCGWINSZ,
9+
c_void, poll, pollfd, read, tcgetattr, tcsetattr, termios, ECHO, ICANON, POLLIN, STDIN_FILENO,
10+
TCSANOW,
1011
};
1112
use std::io::{stdout, Write};
1213
use std::time::Instant;
@@ -80,19 +81,15 @@ impl Default for SixelBackend {
8081
impl super::ImageBackend for SixelBackend {
8182
#[allow(clippy::map_entry)]
8283
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, colors: usize) -> Result<String> {
83-
let tty_size = unsafe {
84-
let tty_size: winsize = std::mem::zeroed();
85-
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);
86-
tty_size
87-
};
84+
let tty_size = unsafe { get_dimensions() };
8885
let cw = tty_size.ws_xpixel / tty_size.ws_col;
8986
let lh = tty_size.ws_ypixel / tty_size.ws_row;
9087
let width_ratio = 1.0 / cw as f64;
9188
let height_ratio = 1.0 / lh as f64;
9289

9390
// resize image to fit the text height with the Lanczos3 algorithm
9491
let image = image.resize(
95-
u32::max_value(),
92+
u32::MAX,
9693
(lines.len() as f64 / height_ratio) as u32,
9794
FilterType::Lanczos3,
9895
);

0 commit comments

Comments
 (0)