fix quarterblock rendering bug

This commit is contained in:
Waveplate 2024-12-15 14:19:19 -08:00
parent 2dfac15db6
commit fb61052438
2 changed files with 25 additions and 75 deletions

View File

@ -105,7 +105,7 @@ impl AnsiImage {
let halfblock = halfblock_bitmap(&bitmap); let halfblock = halfblock_bitmap(&bitmap);
let quarterblock = quarterblock_bitmap(&bitmap); let quarterblock = quarterblock_bitmap(&bitmap);
AnsiImage { AnsiImage {
bitmap, bitmap,
halfblock, halfblock,
quarterblock, quarterblock,
@ -199,44 +199,20 @@ pub fn halfblock_bitmap(bitmap: &Vec<Vec<u32>>) -> Vec<Vec<AnsiPixelPair>> {
ansi_canvas ansi_canvas
} }
fn get_qb_char(pixel_quad: &AnsiPixelQuad, fg_color: u8, args: &args::Args) -> &'static str { fn get_qb_char<T: PartialEq>(pixels: &[T; 4], fg_color: &T) -> &'static str {
let c0 = if args.nograyscale {
pixel_quad.top_left.irc88
} else {
pixel_quad.top_left.irc
}; // top-left
let c1 = if args.nograyscale {
pixel_quad.top_right.irc88
} else {
pixel_quad.top_right.irc
}; // top-right
let c2 = if args.nograyscale {
pixel_quad.bottom_left.irc88
} else {
pixel_quad.bottom_left.irc
}; // bottom-left
let c3 = if args.nograyscale {
pixel_quad.bottom_right.irc88
} else {
pixel_quad.bottom_right.irc
}; // bottom-right
let mut pattern = 0; let mut pattern = 0;
if pixels[2] == *fg_color {
if c2 == fg_color {
pattern |= 1 << 0; // bit 0 (bottom-left) pattern |= 1 << 0; // bit 0 (bottom-left)
} }
if c3 == fg_color { if pixels[3] == *fg_color {
pattern |= 1 << 1; // bit 1 (bottom-right) pattern |= 1 << 1; // bit 1 (bottom-right)
} }
if c0 == fg_color { if pixels[0] == *fg_color {
pattern |= 1 << 2; // bit 2 (top-left) pattern |= 1 << 2; // bit 2 (top-left)
} }
if c1 == fg_color { if pixels[1] == *fg_color {
pattern |= 1 << 3; // bit 3 (top-right) pattern |= 1 << 3; // bit 3 (top-right)
} }
QUARTER_BLOCKS[pattern as usize] QUARTER_BLOCKS[pattern as usize]
} }
@ -248,28 +224,28 @@ pub fn irc_draw_qb(image: &AnsiImage, args: &args::Args) -> String {
for (x, pixel_quad) in row.iter().enumerate() { for (x, pixel_quad) in row.iter().enumerate() {
let c0 = if args.nograyscale { let c0 = if args.nograyscale {
pixel_quad.top_left.irc88
} else {
pixel_quad.top_left.irc
};
let c1 = if args.nograyscale {
pixel_quad.top_right.irc88 pixel_quad.top_right.irc88
} else { } else {
pixel_quad.top_right.irc pixel_quad.top_right.irc
}; };
let c1 = if args.nograyscale {
let c2 = if args.nograyscale { pixel_quad.top_left.irc88
pixel_quad.bottom_left.irc88
} else { } else {
pixel_quad.bottom_left.irc pixel_quad.top_left.irc
}; };
let c3 = if args.nograyscale { let c2 = if args.nograyscale {
pixel_quad.bottom_right.irc88 pixel_quad.bottom_right.irc88
} else { } else {
pixel_quad.bottom_right.irc pixel_quad.bottom_right.irc
}; };
let c3 = if args.nograyscale {
pixel_quad.bottom_left.irc88
} else {
pixel_quad.bottom_left.irc
};
let mut color_counts = HashMap::new(); let mut color_counts = HashMap::new();
for &color in &[c0, c1, c2, c3] { for &color in &[c0, c1, c2, c3] {
*color_counts.entry(color).or_insert(0) += 1; *color_counts.entry(color).or_insert(0) += 1;
@ -287,7 +263,8 @@ pub fn irc_draw_qb(image: &AnsiImage, args: &args::Args) -> String {
.next() .next()
.unwrap_or(&bg_color); .unwrap_or(&bg_color);
let char = get_qb_char(pixel_quad, fg_color, args); let pixels = [c0, c1, c2, c3];
let char = get_qb_char(&pixels, &fg_color);
if x == 0 || fg_color != last_fg || bg_color != last_bg { if x == 0 || fg_color != last_fg || bg_color != last_bg {
if fg_color == bg_color { if fg_color == bg_color {
@ -339,22 +316,8 @@ pub fn ansi_draw_24bit_qb(image: &AnsiImage) -> String {
.next() .next()
.unwrap_or(&bg_color); .unwrap_or(&bg_color);
let char = { let pixels = [c0_rgb, c1_rgb, c2_rgb, c3_rgb];
let mut pattern = 0; let char = get_qb_char(&pixels, &fg_color);
if c2_rgb == fg_color {
pattern |= 1 << 0; // bit 0 (bottom-left)
}
if c3_rgb == fg_color {
pattern |= 1 << 1; // bit 1 (bottom-right)
}
if c0_rgb == fg_color {
pattern |= 1 << 2; // bit 2 (top-left)
}
if c1_rgb == fg_color {
pattern |= 1 << 3; // bit 3 (top-right)
}
QUARTER_BLOCKS[pattern as usize]
};
out.push_str(&format!( out.push_str(&format!(
"\x1b[38;2;{};{};{}m\x1b[48;2;{};{};{}m{}", "\x1b[38;2;{};{};{}m\x1b[48;2;{};{};{}m{}",
@ -415,22 +378,8 @@ pub fn ansi_draw_8bit_qb(image: &AnsiImage, args: &args::Args) -> String {
.next() .next()
.unwrap_or(&bg_color); .unwrap_or(&bg_color);
let char = { let pixels = [c0, c1, c2, c3];
let mut pattern = 0; let char = get_qb_char(&pixels, &fg_color);
if c2 == fg_color {
pattern |= 1 << 0; // bit 0 (bottom-left)
}
if c3 == fg_color {
pattern |= 1 << 1; // bit 1 (bottom-right)
}
if c0 == fg_color {
pattern |= 1 << 2; // bit 2 (top-left)
}
if c1 == fg_color {
pattern |= 1 << 3; // bit 3 (top-right)
}
QUARTER_BLOCKS[pattern as usize]
};
out.push_str(&format!( out.push_str(&format!(
"\x1b[38;5;{}m\x1b[48;5;{}m{}", "\x1b[38;5;{}m\x1b[48;5;{}m{}",
@ -479,6 +428,7 @@ pub fn ansi_draw_8bit(image: &AnsiImage, args: &args::Args) -> String {
let mut out: String = String::new(); let mut out: String = String::new();
for row in &image.halfblock { for row in &image.halfblock {
for pixel_pair in row.iter() { for pixel_pair in row.iter() {
let fg = if args.nograyscale { let fg = if args.nograyscale {
pixel_pair.top.ansi232 pixel_pair.top.ansi232
} else { } else {
@ -543,7 +493,7 @@ pub fn luma(rgb: &[u8; 3]) -> u8 {
((0.299 * r as f32 + 0.587 * g as f32 + 0.114 * b as f32).round()) as u8 ((0.299 * r as f32 + 0.587 * g as f32 + 0.114 * b as f32).round()) as u8
} }
pub fn ansi_draw_braille_24bit(image_luma: &AnsiImage, image_chroma: &AnsiImage, args: &args::Args) -> String { pub fn ansi_draw_braille_24bit(image_luma: &AnsiImage, image_chroma: &AnsiImage) -> String {
let mut out: String = String::new(); let mut out: String = String::new();
let bitmap_luma = &image_luma.bitmap; let bitmap_luma = &image_luma.bitmap;
let bitmap_chroma = &image_chroma.bitmap; let bitmap_chroma = &image_chroma.bitmap;

View File

@ -29,7 +29,7 @@ async fn main() {
(_, true, _, _, true) => println!("{}", draw::ansi_draw_braille_8bit(&canvas_luma, &canvas_chroma, &args)), (_, true, _, _, true) => println!("{}", draw::ansi_draw_braille_8bit(&canvas_luma, &canvas_chroma, &args)),
(_, _, true, true, false) => println!("{}", draw::ansi_draw_24bit_qb(&canvas_chroma)), (_, _, true, true, false) => println!("{}", draw::ansi_draw_24bit_qb(&canvas_chroma)),
(_, _, true, false, false) => println!("{}", draw::ansi_draw_24bit(&canvas_chroma)), (_, _, true, false, false) => println!("{}", draw::ansi_draw_24bit(&canvas_chroma)),
(_, _, true, _, true) => println!("{}", draw::ansi_draw_braille_24bit(&canvas_luma, &canvas_chroma, &args)), (_, _, true, _, true) => println!("{}", draw::ansi_draw_braille_24bit(&canvas_luma, &canvas_chroma)),
(_, _, _, true, false) => println!("{}", draw::irc_draw_qb(&canvas_chroma, &args)), (_, _, _, true, false) => println!("{}", draw::irc_draw_qb(&canvas_chroma, &args)),
(_, _, _, _, true) => println!("{}", draw::irc_draw_braille(&canvas_luma, &canvas_chroma, &args)), (_, _, _, _, true) => println!("{}", draw::irc_draw_braille(&canvas_luma, &canvas_chroma, &args)),
_ => println!("{}", draw::irc_draw(&canvas_chroma, &args)), _ => println!("{}", draw::irc_draw(&canvas_chroma, &args)),