u32 instructions with final byte work ?

This commit is contained in:
HistidineDwarf 2023-02-11 19:07:09 -08:00
parent d74e03e91f
commit 52438cb2f0
1 changed files with 58 additions and 35 deletions

View File

@ -73,24 +73,27 @@ fn translate_binary(binary_data: Vec<bool>) -> anyhow::Result<Vec<u8>>{
} }
//Bit of a waste //Bit of a waste
pub fn rip_binary_u64(byte: u64) -> anyhow::Result<Vec<bool>> { pub fn rip_binary_u32(bytes: Vec<u32>) -> anyhow::Result<Vec<bool>> {
let mut binary_data: Vec<bool> = Vec::new(); let mut binary_data: Vec<bool> = Vec::new();
let mut bits = format!("{:b}", byte); for byte in bytes {
let missing_0 = 64 - bits.len(); let mut bits = format!("{:b}", byte);
let missing_0 = 32 - bits.len();
//Adding the missing 0's, could be faster //Adding the missing 0's, could be faster
for _ in 0..missing_0 { for _ in 0..missing_0 {
bits.insert(0, '0'); bits.insert(0, '0');
} }
for bit in bits.chars() { for bit in bits.chars() {
if bit == '1' { if bit == '1' {
binary_data.push(true); binary_data.push(true);
} else { } else {
binary_data.push(false); binary_data.push(false);
}
} }
} }
return Ok(binary_data); return Ok(binary_data);
} }
@ -288,33 +291,53 @@ fn etch_instructions(settings: &Settings, data: &Data)
-> anyhow::Result<EmbedSource> { -> anyhow::Result<EmbedSource> {
let instruction_size = 5; let instruction_size = 5;
let mut u8_instructions: Vec<u8> = Vec::new(); let mut u32_instructions: Vec<u32> = Vec::new();
//Both adds the output mode to instructions and finds last byte //calculating at what frame and pixel the file ends
let last_byte_pointer = match data.out_mode { let frame_size = (settings.height * settings.width) as usize;
//Adds the output mode to instructions
//Instead of putting entire size of file, add at which frame and pixel file ends
//Saves space on instruction frame
match data.out_mode {
OutputMode::Color => { OutputMode::Color => {
u8_instructions.push(255); u32_instructions.push(u32::MAX);
rip_binary_u64(data.bytes.len() as u64)?
let final_byte = data.bytes.len() % frame_size;
let pixel_length = (data.bytes.len() as f64 / 3.).ceil() as i32;
let pixel_length = (pixel_length * settings.size.pow(2)) as usize;
let mut final_frame = pixel_length / frame_size;
if pixel_length % frame_size != 0 {
final_frame += 1;
}
dbg!(final_frame, final_byte);
u32_instructions.push(final_frame as u32);
u32_instructions.push(final_byte as u32);
}, },
OutputMode::Binary => { OutputMode::Binary => {
u8_instructions.push(0); u32_instructions.push(u32::MIN);
rip_binary_u64(data.binary.len() as u64)?
let final_byte = data.binary.len() % frame_size;
let pixel_length = (data.binary.len() as i32 * settings.size.pow(2)) as usize;
let mut final_frame = pixel_length / frame_size;
if pixel_length % frame_size != 0 {
final_frame += 1;
}
dbg!(final_frame, final_byte);
u32_instructions.push(final_frame as u32);
u32_instructions.push(final_byte as u32);
}, },
}; };
//Could choke and die u32_instructions.push(settings.size as u32);
u8_instructions.push(settings.size as u8);
u8_instructions.push(settings.fps as u8);
let mut instruction_data = rip_binary(u8_instructions)?;
//Instead of putting entire size of file, add at which frame and pixel file ends
//Saves space on instruction frame
instruction_data.extend(last_byte_pointer);
//Here to make sure instruction frame and the rest are of the same size let instruction_data = rip_binary_u32(u32_instructions)?;
//Using EmbedSource::new() in case it changes and this code becomes disconnected from the rest
// let dummy = EmbedSource::new(settings.size, settings.width, settings.height);
// let width = dummy.width;
// let height = dummy.height;
let mut source = EmbedSource::new(instruction_size, settings.width, settings.height); let mut source = EmbedSource::new(instruction_size, settings.width, settings.height);
let mut index = 0; let mut index = 0;
@ -323,11 +346,11 @@ fn etch_instructions(settings: &Settings, data: &Data)
Err(_) => {println!("Instructions written")} Err(_) => {println!("Instructions written")}
} }
// 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);
} }