From 6133f88d6a3aa0dbea361b58740a916bb00a90bc Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 13 Mar 2021 13:34:44 -0800 Subject: [PATCH] term: avoid full image decode when parsing iterm2 protocol Just decode the size refs: https://github.com/wez/wezterm/issues/537 --- term/src/terminalstate.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index 17e7906f7..373ebf186 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -4,7 +4,6 @@ use super::*; use crate::color::{ColorPalette, RgbColor}; use anyhow::bail; -use image::{self, GenericImageView}; use log::{debug, error}; use num_traits::FromPrimitive; use ordered_float::NotNan; @@ -1572,9 +1571,14 @@ impl TerminalState { return; } - // Decode the image data - let decoded_image = match image::load_from_memory(&image.data) { - Ok(im) => im, + fn dimensions(data: &[u8]) -> anyhow::Result<(u32, u32)> { + let reader = + image::io::Reader::new(std::io::Cursor::new(data)).with_guessed_format()?; + Ok(reader.into_dimensions()?) + } + + let (image_width, image_height) = match dimensions(&image.data) { + Ok(dims) => dims, Err(e) => { error!( "Unable to decode image: {}: size={} {:?}", @@ -1596,13 +1600,13 @@ impl TerminalState { let height = image.height.to_pixels(cell_pixel_height, physical_rows); // Compute any Automatic dimensions - let aspect = decoded_image.width() as f32 / decoded_image.height() as f32; + let aspect = image_width as f32 / image_height as f32; let (width, height) = match (width, height) { (None, None) => { // Take the image's native size - let width = decoded_image.width() as usize; - let height = decoded_image.height() as usize; + let width = image_width as usize; + let height = image_height as usize; // but ensure that it fits if width as usize > self.pixel_width || height as usize > self.pixel_height { let width = width as f32;