Werid error with bytes missing in the middle, idk

This commit is contained in:
HistidineDwarf 2023-01-27 22:25:49 -08:00
parent 958b0d0c60
commit 81eb469b68
3 changed files with 120 additions and 21 deletions

View File

@ -8,7 +8,7 @@ use opencv::core::{Mat, Vector, VecN, Size, CV_8UC3,};
use opencv::imgcodecs::{imread, imwrite, IMREAD_COLOR}; use opencv::imgcodecs::{imread, imwrite, IMREAD_COLOR};
use opencv::videoio::{VideoWriter, VideoCapture, CAP_ANY}; use opencv::videoio::{VideoWriter, VideoCapture, CAP_ANY};
use crate::settings::{Settings, OutputMode, Data}; use crate::settings::{Settings, OutputMode, Data, self};
use crate::embedsource::EmbedSource; use crate::embedsource::EmbedSource;
//Get and write bytes from and to files. Start and end of app //Get and write bytes from and to files. Start and end of app
@ -47,6 +47,25 @@ pub fn rip_binary(byte_data: Vec<u8>) -> anyhow::Result<Vec<bool>> {
return Ok(binary_data); return Ok(binary_data);
} }
fn translate_binary(binary_data: Vec<bool>) -> anyhow::Result<Vec<u8>>{
let mut buffer: Vec<bool> = Vec::new();
let mut byte_data: Vec<u8> = 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 //Bit of a waste
pub fn rip_binary_u64(byte: u64) -> anyhow::Result<Vec<bool>> { pub fn rip_binary_u64(byte: u64) -> anyhow::Result<Vec<bool>> {
let mut binary_data: Vec<bool> = Vec::new(); let mut binary_data: Vec<bool> = Vec::new();
@ -69,7 +88,7 @@ pub fn rip_binary_u64(byte: u64) -> anyhow::Result<Vec<bool>> {
return Ok(binary_data); return Ok(binary_data);
} }
fn write_bytes(path: &str, data: Vec<u8>) -> anyhow::Result<()> { pub fn write_bytes(path: &str, data: Vec<u8>) -> anyhow::Result<()> {
fs::write(path, data)?; fs::write(path, data)?;
println!("File written succesfully"); println!("File written succesfully");
return Ok(()); return Ok(());
@ -111,7 +130,7 @@ fn get_pixel(frame: &EmbedSource, x: i32, y: i32) -> Option<Vec<u8>> {
g_average as u8, g_average as u8,
b_average as u8 b_average as u8
]; ];
dbg!(&rgb_average); // dbg!(&rgb_average);
return Some(rgb_average); return Some(rgb_average);
} }
@ -192,7 +211,7 @@ fn etch_frame(source: &mut EmbedSource, data: &Data, global_index: &mut usize)
return Ok(()); return Ok(());
} }
pub fn read_frame(source: &EmbedSource, out_mode: &OutputMode) { fn read_frame(source: &EmbedSource, out_mode: &OutputMode) -> anyhow::Result<Vec<u8>>{
let size = source.size as usize; let size = source.size as usize;
let half_size = (source.size/2) as i32; let half_size = (source.size/2) as i32;
let width = source.width; let width = source.width;
@ -215,6 +234,8 @@ pub fn read_frame(source: &EmbedSource, out_mode: &OutputMode) {
} }
} }
} }
return Ok(byte_data);
}, },
OutputMode::Binary => { OutputMode::Binary => {
let mut binary_data: Vec<bool> = Vec::new(); let mut binary_data: Vec<bool> = 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: Instructions:
Etched on first frame, always be wrtten in binary despite output mode Etched on first frame, always be wrtten in binary despite output mode
Output mode is the first byte Output mode is the first byte
Size is constant 5
11111111 = Color (255), 00000000 = Binary(0), 11111111 = Color (255), 00000000 = Binary(0),
Second byte will be the size of the pixels Second byte will be the size of the pixels
FPS doesn't matter, but can add it anyways FPS doesn't matter, but can add it anyways
Potentially add ending pointer so it doesn't make useless bytes Potentially add ending pointer so it doesn't make useless bytes
^^Currently implemented(?), unused
*/ */
fn etch_instructions(settings: &Settings, data: &Data) fn etch_instructions(settings: &Settings, data: &Data)
@ -276,17 +301,34 @@ fn etch_instructions(settings: &Settings, data: &Data)
Err(_) => {println!("End of data reached")} Err(_) => {println!("End of data reached")}
} }
highgui::named_window("window", WINDOW_FULLSCREEN)?; // highgui::named_window("window", WINDOW_FULLSCREEN)?;
highgui::imshow("window", &source.image)?; // highgui::imshow("window", &source.image)?;
highgui::wait_key(10000000)?; // 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); 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<()> { 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); println!("Video embedded succesfully at {}", path);
return Ok(()); return Ok(());
}
pub fn read(path: &str) -> anyhow::Result<Vec<u8>> {
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<u8> = 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);
} }

View File

@ -3,7 +3,6 @@ mod etcher;
mod settings; mod settings;
mod embedsource; mod embedsource;
use etcher::read_frame;
use settings::{Data, Settings}; use settings::{Data, Settings};
//Make RGB a struct //Make RGB a struct
@ -11,13 +10,32 @@ use settings::{Data, Settings};
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
// ui::summon_gooey(); // ui::summon_gooey();
let bytes = etcher::rip_bytes("src/tests/Baby.wav")?; 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); 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(()); return Ok(());
} }

View File

@ -11,7 +11,15 @@ pub struct Data {
} }
impl Data { impl Data {
pub fn from_binary(binary: Vec<bool>) -> 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<bool>) -> Data {
Data { Data {
bytes: Vec::new(), bytes: Vec::new(),
binary, binary,
@ -38,11 +46,11 @@ pub struct Settings {
impl Settings { impl Settings {
pub fn new(size: i32, fps: i32, width: i32, height: i32,) pub fn new(size: i32, fps: i32, width: i32, height: i32,)
-> Settings { -> Settings {
Settings { Settings {
size, size,
fps: fps as f64, fps: fps as f64,
height, height,
width width
} }
} }
} }