sequential & bandpass
This commit is contained in:
parent
92fd5531e9
commit
8b277ff53b
2 changed files with 6 additions and 57 deletions
|
|
@ -28,7 +28,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
}
|
||||
|
||||
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 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 entropy: Vec<u8> = if pp {
|
||||
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 {
|
||||
let entropy: Vec<u8> = if quick {
|
||||
ocelli.whiten(&ocelli.pick_and_flip(&grayscale_data, frame_count as usize))
|
||||
} else {
|
||||
if ocelli.is_covered(&grayscale_data, 50) {
|
||||
|
|
|
|||
46
src/lib.rs
46
src/lib.rs
|
|
@ -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.
|
||||
// Generates entropy by combining these bits into bytes.
|
||||
// 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();
|
||||
|
||||
for &pixel in data {
|
||||
if (2..=253).contains(&pixel) { // filter bias
|
||||
if (low..=high).contains(&pixel) { // filter bias
|
||||
let mut lsb = pixel & 1;
|
||||
if current_frame_index % 2 == 0 { // flip bits
|
||||
lsb ^= 1;
|
||||
|
|
@ -71,20 +71,6 @@ impl Ocelli {
|
|||
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 {
|
||||
// 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.
|
||||
|
|
@ -178,6 +164,8 @@ pub extern "C" fn chop_and_tack(
|
|||
pub extern "C" fn pick_and_flip(
|
||||
data_ptr: *const u8,
|
||||
data_len: usize,
|
||||
low: u8,
|
||||
high: u8,
|
||||
current_frame_index: usize,
|
||||
result_ptr: *mut u8,
|
||||
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 ocelli = Ocelli;
|
||||
let result = ocelli.pick_and_flip(data, 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(¤t.to_vec(), &previous.to_vec(), low, high);
|
||||
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());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue