sequential & bandpass

This commit is contained in:
randogoth 2025-01-05 15:37:42 +02:00
parent 92fd5531e9
commit 8b277ff53b
2 changed files with 6 additions and 57 deletions

View file

@ -28,7 +28,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
let quick = args.contains(&String::from("-q")); let quick = args.contains(&String::from("-q"));
let pp = args.contains(&String::from("-p"));
let camera_index: usize = args[1].parse().expect("Failed to parse camera index as a number"); let camera_index: usize = args[1].parse().expect("Failed to parse camera index as a number");
let length: usize = args[2].parse().expect("Failed to parse entropy length as a number"); let length: usize = args[2].parse().expect("Failed to parse entropy length as a number");
@ -72,21 +71,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let grayscale_data = frame_to_grayscale(&data); let grayscale_data = frame_to_grayscale(&data);
let entropy: Vec<u8> = if pp { let entropy: Vec<u8> = if quick {
let mut low = 10;
let mut high = 245;
if ocelli.is_covered(&grayscale_data, 50) {
low = 3;
high = 252;
}
if let Some(last) = last_frame {
ocelli.whiten(&ocelli.tune_and_prune(&grayscale_data, &last, low, high))
} else {
Vec::new()
}
} else if quick {
ocelli.whiten(&ocelli.pick_and_flip(&grayscale_data, frame_count as usize)) ocelli.whiten(&ocelli.pick_and_flip(&grayscale_data, frame_count as usize))
} else { } else {
if ocelli.is_covered(&grayscale_data, 50) { if ocelli.is_covered(&grayscale_data, 50) {

View file

@ -50,7 +50,7 @@ impl Ocelli {
} }
pub fn pick_and_flip(&self, data: &[u8], current_frame_index: usize) -> Vec<u8> { pub fn pick_and_flip(&self, data: &[u8], low: u8, high: u8, current_frame_index: usize) -> Vec<u8> {
// Extracts the least significant bit (LSB) of each pixel brightness, flipping it based on the frame index. // Extracts the least significant bit (LSB) of each pixel brightness, flipping it based on the frame index.
// Generates entropy by combining these bits into bytes. // Generates entropy by combining these bits into bytes.
// Algorithm is a simplified version of R. Li, "A True Random Number Generator algorithm from // Algorithm is a simplified version of R. Li, "A True Random Number Generator algorithm from
@ -59,7 +59,7 @@ impl Ocelli {
let mut entropy = Vec::new(); let mut entropy = Vec::new();
for &pixel in data { for &pixel in data {
if (2..=253).contains(&pixel) { // filter bias if (low..=high).contains(&pixel) { // filter bias
let mut lsb = pixel & 1; let mut lsb = pixel & 1;
if current_frame_index % 2 == 0 { // flip bits if current_frame_index % 2 == 0 { // flip bits
lsb ^= 1; lsb ^= 1;
@ -71,20 +71,6 @@ impl Ocelli {
self.bits_to_bytes(&entropy) self.bits_to_bytes(&entropy)
} }
pub fn tune_and_prune(&self, current: &Vec<u8>, previous: &Vec<u8>, low: u8, high: u8) -> Vec<u8> {
// Extracts the least significant bit (LSB) of each pixel brightness while filtering out bias
// It's a combination of methods from the other two entropy extraction algorithms
let mut entropy = Vec::new();
for (&c, &p) in current.iter().zip(previous.iter()) {
if c != p && (low..=high).contains(&c) {
entropy.push(c & 1);
}
}
self.bits_to_bytes(&entropy)
}
pub fn shannon(&self, data: &Vec<u8>) -> f64 { pub fn shannon(&self, data: &Vec<u8>) -> f64 {
// Calculates the Shannon entropy of a given byte vector to measure its randomness. // Calculates the Shannon entropy of a given byte vector to measure its randomness.
// Uses a frequency map to compute probabilities and their contributions to entropy. // Uses a frequency map to compute probabilities and their contributions to entropy.
@ -178,6 +164,8 @@ pub extern "C" fn chop_and_tack(
pub extern "C" fn pick_and_flip( pub extern "C" fn pick_and_flip(
data_ptr: *const u8, data_ptr: *const u8,
data_len: usize, data_len: usize,
low: u8,
high: u8,
current_frame_index: usize, current_frame_index: usize,
result_ptr: *mut u8, result_ptr: *mut u8,
result_len: &mut usize, result_len: &mut usize,
@ -185,31 +173,7 @@ pub extern "C" fn pick_and_flip(
let data = unsafe { slice::from_raw_parts(data_ptr, data_len) }; let data = unsafe { slice::from_raw_parts(data_ptr, data_len) };
let ocelli = Ocelli; let ocelli = Ocelli;
let result = ocelli.pick_and_flip(data, current_frame_index); let result = ocelli.pick_and_flip(data, low, high, current_frame_index);
unsafe {
let result_slice = slice::from_raw_parts_mut(result_ptr, result.len());
result_slice.copy_from_slice(&result);
*result_len = result.len();
}
}
#[no_mangle]
pub extern "C" fn tune_and_prune(
current_ptr: *const u8,
current_len: usize,
previous_ptr: *const u8,
previous_len: usize,
low: u8,
high: u8,
result_ptr: *mut u8,
result_len: &mut usize,
) {
let current = unsafe { slice::from_raw_parts(current_ptr, current_len) };
let previous = unsafe { slice::from_raw_parts(previous_ptr, previous_len) };
let ocelli = Ocelli;
let result = ocelli.tune_and_prune(&current.to_vec(), &previous.to_vec(), low, high);
unsafe { unsafe {
let result_slice = slice::from_raw_parts_mut(result_ptr, result.len()); let result_slice = slice::from_raw_parts_mut(result_ptr, result.len());