From f9c7a34d14be930d6641c84e047327e1e2a352e4 Mon Sep 17 00:00:00 2001 From: Justin Hinckley Date: Sun, 3 Jul 2022 01:31:49 -0700 Subject: [PATCH 1/2] double vertical resolution with half blocks --- src/lib.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0ab9ffb..2d53754 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ const BLOCK_LIGHT: char = '\u{2591}'; const BLOCK_MEDIUM: char = '\u{2592}'; const BLOCK_DARK: char = '\u{2593}'; const BLOCK_FULL: char = '\u{2588}'; +const BLOCK_UPPER_HALF: char = '\u{2580}'; /// A tui-rs Widget which displays an image. pub struct Image<'a> { @@ -110,16 +111,16 @@ impl<'a> Image<'a> { let oy = max( 0, min( - area.height - 1, - (area.height - (img.height() / 2) as u16) / 2, + (2 * area.height) - 1, + ((2 * area.height) - img.height() as u16) / 2, ), ) as u16; // draw - for y in oy..(oy + min((img.height() / 2) as u16, area.height - 1)) { + for y in oy..min(oy + img.height() as u16, (2 * area.height) - 1) { for x in ox..min(ox + img.width() as u16, area.width - 1) { - let p = img.get_pixel((x - ox) as u32, 2 * (y - oy) as u32); + let p = img.get_pixel((x - ox) as u32, (y - oy) as u32); // composite onto background let a = p[3] as f32 / 255.0; @@ -127,7 +128,7 @@ impl<'a> Image<'a> { let g = p[1] as f32 * a / 255.0 + bg_rgb[1] * (1f32 - a); let b = p[2] as f32 * a / 255.0 + bg_rgb[2] * (1f32 - a); - let cell = buf.get_mut(area.left() + x, area.top() + y); + let cell = buf.get_mut(area.left() + x, area.top() + (y / 2)); match self.color_mode { ColorMode::Luma => { @@ -145,11 +146,19 @@ impl<'a> Image<'a> { }); } ColorMode::Rgb => { - cell.set_char(BLOCK_FULL).set_fg(Color::Rgb( - (255.0 * r) as u8, - (255.0 * g) as u8, - (255.0 * b) as u8, - )); + if false { + cell.set_char(BLOCK_UPPER_HALF).set_fg(Color::Rgb( + (255.0 * r) as u8, + (255.0 * g) as u8, + (255.0 * b) as u8, + )); + } else { + cell.set_bg(Color::Rgb( + (255.0 * r) as u8, + (255.0 * g) as u8, + (255.0 * b) as u8, + )); + } } } } @@ -175,7 +184,7 @@ impl<'a> Widget for Image<'a> { buf.set_style(area, self.style); if let Some(ref img) = self.img { - if img.width() > area.width as u32 || img.height() / 2 > area.height as u32 { + if img.width() > area.width as u32 || img.height() > 2 * area.height as u32 { let scaled = resize( img, area.width as u32, From 92bb3a00844a93719bbfa1aed1cf5d6f3c4e2b8a Mon Sep 17 00:00:00 2001 From: Justin Hinckley Date: Sun, 3 Jul 2022 15:21:41 -0700 Subject: [PATCH 2/2] fixed condition to use half-blocks --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2d53754..97429ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,7 +146,7 @@ impl<'a> Image<'a> { }); } ColorMode::Rgb => { - if false { + if y & 1 == 0 { cell.set_char(BLOCK_UPPER_HALF).set_fg(Color::Rgb( (255.0 * r) as u8, (255.0 * g) as u8,