Skip to content
Open
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
31 changes: 20 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -110,24 +111,24 @@ 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;
let r = p[0] as f32 * a / 255.0 + bg_rgb[0] * (1f32 - 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 => {
Expand All @@ -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 y & 1 == 0 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think y % 2 is a bit better. Also, a little concerned that this probably breaks Luma ColorMode?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with y & 1. @dmadisetti what's the concern with Luma ColorModel here?

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,
));
}
}
}
}
Expand All @@ -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,
Expand Down