From 81eb469b68ff02b254eae1311c9562def9352eb4 Mon Sep 17 00:00:00 2001 From: HistidineDwarf Date: Fri, 27 Jan 2023 22:25:49 -0800 Subject: [PATCH] Werid error with bytes missing in the middle, idk --- src/etcher.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++----- src/main.rs | 28 ++++++++++++--- src/settings.rs | 22 ++++++++---- 3 files changed, 120 insertions(+), 21 deletions(-) diff --git a/src/etcher.rs b/src/etcher.rs index 8c8b5b1..62efade 100644 --- a/src/etcher.rs +++ b/src/etcher.rs @@ -8,7 +8,7 @@ use opencv::core::{Mat, Vector, VecN, Size, CV_8UC3,}; use opencv::imgcodecs::{imread, imwrite, IMREAD_COLOR}; use opencv::videoio::{VideoWriter, VideoCapture, CAP_ANY}; -use crate::settings::{Settings, OutputMode, Data}; +use crate::settings::{Settings, OutputMode, Data, self}; use crate::embedsource::EmbedSource; //Get and write bytes from and to files. Start and end of app @@ -47,6 +47,25 @@ pub fn rip_binary(byte_data: Vec) -> anyhow::Result> { return Ok(binary_data); } +fn translate_binary(binary_data: Vec) -> anyhow::Result>{ + let mut buffer: Vec = Vec::new(); + let mut byte_data: Vec = Vec::new(); + + for bit in binary_data { + buffer.push(bit); + + if buffer.len() == 8 { + //idk how this works but it does + let byte = buffer.iter().fold(0u8, |v, b| (v << 1) + (*b as u8)); + // dbg!(byte); + byte_data.push(byte); + buffer.clear(); + } + } + + return Ok(byte_data); +} + //Bit of a waste pub fn rip_binary_u64(byte: u64) -> anyhow::Result> { let mut binary_data: Vec = Vec::new(); @@ -69,7 +88,7 @@ pub fn rip_binary_u64(byte: u64) -> anyhow::Result> { return Ok(binary_data); } -fn write_bytes(path: &str, data: Vec) -> anyhow::Result<()> { +pub fn write_bytes(path: &str, data: Vec) -> anyhow::Result<()> { fs::write(path, data)?; println!("File written succesfully"); return Ok(()); @@ -111,7 +130,7 @@ fn get_pixel(frame: &EmbedSource, x: i32, y: i32) -> Option> { g_average as u8, b_average as u8 ]; - dbg!(&rgb_average); + // dbg!(&rgb_average); return Some(rgb_average); } @@ -192,7 +211,7 @@ fn etch_frame(source: &mut EmbedSource, data: &Data, global_index: &mut usize) return Ok(()); } -pub fn read_frame(source: &EmbedSource, out_mode: &OutputMode) { +fn read_frame(source: &EmbedSource, out_mode: &OutputMode) -> anyhow::Result>{ let size = source.size as usize; let half_size = (source.size/2) as i32; let width = source.width; @@ -215,6 +234,8 @@ pub fn read_frame(source: &EmbedSource, out_mode: &OutputMode) { } } } + + return Ok(byte_data); }, OutputMode::Binary => { let mut binary_data: Vec = Vec::new(); @@ -233,6 +254,8 @@ pub fn read_frame(source: &EmbedSource, out_mode: &OutputMode) { } } } + + return Ok(translate_binary(binary_data)?); } } } @@ -240,10 +263,12 @@ pub fn read_frame(source: &EmbedSource, out_mode: &OutputMode) { Instructions: Etched on first frame, always be wrtten in binary despite output mode Output mode is the first byte +Size is constant 5 11111111 = Color (255), 00000000 = Binary(0), Second byte will be the size of the pixels FPS doesn't matter, but can add it anyways Potentially add ending pointer so it doesn't make useless bytes +^^Currently implemented(?), unused */ fn etch_instructions(settings: &Settings, data: &Data) @@ -276,17 +301,34 @@ fn etch_instructions(settings: &Settings, data: &Data) Err(_) => {println!("End of data reached")} } - highgui::named_window("window", WINDOW_FULLSCREEN)?; - highgui::imshow("window", &source.image)?; - highgui::wait_key(10000000)?; + // highgui::named_window("window", WINDOW_FULLSCREEN)?; + // highgui::imshow("window", &source.image)?; + // highgui::wait_key(10000000)?; - imwrite("src/out/test1.png", &source.image, &Vector::new())?; + // imwrite("src/out/test1.png", &source.image, &Vector::new())?; return Ok(source); } -fn read_instructions() { +fn read_instructions(source: &EmbedSource) -> anyhow::Result<(OutputMode, Settings)> { + let byte_data = read_frame(source, &OutputMode::Binary)?; + let out_mode = byte_data[0]; + + let out_mode = match out_mode { + 255 => OutputMode::Color, + _ => OutputMode::Binary, + }; + + let size = byte_data[1] as i32; + let fps = byte_data[2] as i32; + let height = source.height; + let width = source.width; + + let settings = Settings::new(size, fps, width, height); + + // println!("Output mode is: {}\nsize is: {}\nfps is: {}", out_mode, size, fps); + return Ok((out_mode, settings)); } pub fn etch(path: &str, data: Data, settings: Settings) -> anyhow::Result<()> { @@ -323,4 +365,35 @@ pub fn etch(path: &str, data: Data, settings: Settings) -> anyhow::Result<()> { println!("Video embedded succesfully at {}", path); return Ok(()); +} + +pub fn read(path: &str) -> anyhow::Result> { + let mut video = VideoCapture::from_file(&path, CAP_ANY) + .expect("Could not open video path"); + let mut frame = Mat::default(); + + //Could probably avoid cloning + video.read(&mut frame)?; + let instruction_source = EmbedSource::from(frame.clone(), 5); + let (out_mode, settings) = read_instructions(&instruction_source)?; + + let mut byte_data: Vec = Vec::new(); + loop { + video.read(&mut frame)?; + + //If it reads an empty image, the video stopped + if frame.cols() == 0 { + break; + } + + //Passing Data might speed up + //CLONING, AAAAAAAAAAAAAA + //Massive slow down vvv + let source = EmbedSource::from(frame.clone(), settings.size); + let batch = read_frame(&source, &out_mode)?; + byte_data.extend(batch); + } + + println!("Video read succesfully"); + return Ok(byte_data); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7f621e6..1ac1eb9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ mod etcher; mod settings; mod embedsource; -use etcher::read_frame; use settings::{Data, Settings}; //Make RGB a struct @@ -11,13 +10,32 @@ use settings::{Data, Settings}; fn main() -> anyhow::Result<()> { // ui::summon_gooey(); let bytes = etcher::rip_bytes("src/tests/Baby.wav")?; - let binary = etcher::rip_binary(bytes)?; - let data = Data::from_binary(binary); + dbg!(bytes.len()); + // for i in 0..10000 { + // if bytes[i] != 0 { + // dbg!(bytes[i]); + // } + // } + + // let binary = etcher::rip_binary(bytes)?; + + let data = Data::from_color(bytes); let settings = Settings::new(1, 30, 640, 360); - read_frame(); - // etcher::etch("src/out/output.avi", data, settings)?; + etcher::etch("src/out/output.avi", data, settings)?; + + let out_data = etcher::read("src/out/output.avi")?; + + //WHO ATE THE BYTES ??? + dbg!(out_data.len()); + // for i in 0..10000 { + // if out_data[i] != 0 { + // dbg!(out_data[i]); + // } + // } + + etcher::write_bytes("src/out/imbaby.wav", out_data)?; return Ok(()); } diff --git a/src/settings.rs b/src/settings.rs index 34d3a4c..68340af 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -11,7 +11,15 @@ pub struct Data { } impl Data { - pub fn from_binary(binary: Vec) -> Data{ + pub fn new_out_mode(out_mode: OutputMode) -> Data { + Data { + bytes: Vec::new(), + binary: Vec::new(), + out_mode + } + } + + pub fn from_binary(binary: Vec) -> Data { Data { bytes: Vec::new(), binary, @@ -38,11 +46,11 @@ pub struct Settings { impl Settings { pub fn new(size: i32, fps: i32, width: i32, height: i32,) -> Settings { - Settings { - size, - fps: fps as f64, - height, - width - } + Settings { + size, + fps: fps as f64, + height, + width + } } } \ No newline at end of file