Skip to content

Commit

Permalink
Merge pull request #2 from polyesterswing/dev
Browse files Browse the repository at this point in the history
Add color
  • Loading branch information
isinghdivyanshu authored Jul 11, 2024
2 parents f8d49b8 + 1769480 commit 4760d4e
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
use opencv::core::VecN;
use opencv::prelude::*;
use opencv::{highgui, imgproc, videoio, core, Result};
use opencv::{core, highgui, imgproc, videoio, Result};

fn map_range(from_range: (i32, i32), to_range: (i32, i32), s: i32) -> i32 {
to_range.0 + (s - from_range.0) * (to_range.1 - to_range.0) / (from_range.1 - from_range.0)
}

fn encode_frame(frame: Mat, table: &str, table_len: usize) -> Result<String> {
let mut out_frame = String::new();
fn find_colors(frame: &Mat, gray: &Mat, table: &str, table_len: usize) -> Result<String> {
let mut out_colors = String::new();

let rows = frame.rows();
let cols = frame.cols();

for row in 0..rows {
for col in 0..cols {
let pixel = frame.at_2d::<u8>(row, col)?;

let new_pixel = map_range((0, 255), (0, (table_len - 1) as i32), *pixel as i32);
out_frame.push(table.as_bytes()[new_pixel as usize] as char);
let pixel = frame.at_2d::<VecN<u8, 3>>(row, col)?;

let gray_pixel = gray.at_2d::<u8>(row, col)?;
let new_pixel = map_range((0, 255), (0, (table_len - 1) as i32), *gray_pixel as i32);
out_colors.push_str(&*format!(
"\x1b[38;2;{};{};{}m{}",
pixel[2],
pixel[1],
pixel[0],
table.as_bytes()[new_pixel as usize] as char
))
}

out_frame.push_str("\n");
out_colors.push_str("\n");
}

Ok(out_frame)
Ok(out_colors)
}

fn main() -> Result<()> {
Expand All @@ -35,7 +43,7 @@ fn main() -> Result<()> {
let window = "Video";
highgui::named_window(window, 1)?;

let mut cam = videoio::VideoCapture::from_file("bad-apple.webm", videoio::CAP_ANY)?;
let mut cam = videoio::VideoCapture::from_file("baby-shark.webm", videoio::CAP_ANY)?;

if !cam.is_opened()? {
panic!("Unable to open video file");
Expand All @@ -46,14 +54,28 @@ fn main() -> Result<()> {
cam.read(&mut frame)?;

if frame.size()?.width > 0 {
let mut smaller = Mat::default();
imgproc::resize(
&frame,
&mut smaller,
core::Size::new(term_size.0.into(), term_size.1.into()),
0.0,
0.0,
imgproc::INTER_AREA,
)?;

//let _ = find_colors(&smaller)?;
//println!("{}", find_colors(&smaller)?);

let mut gray = Mat::default();
imgproc::cvt_color_def(&frame, &mut gray, imgproc::COLOR_BGR2GRAY)?;
imgproc::cvt_color_def(&smaller, &mut gray, imgproc::COLOR_BGR2GRAY)?;

let mut smaller = Mat::default();
imgproc::resize(&gray, &mut smaller, core::Size::new(term_size.0.into(), term_size.1.into()), 0.0, 0.0, imgproc::INTER_AREA)?;
highgui::imshow(window, &smaller)?;

println!("{}", encode_frame(smaller, ascii_table, ascii_table_len)?);
println!(
"{}",
find_colors(&smaller, &gray, ascii_table, ascii_table_len)?
);
}

if highgui::wait_key(10)? > 0 {
Expand Down

0 comments on commit 4760d4e

Please sign in to comment.