Skip to content

Commit

Permalink
feat: rayon based find_starting_point
Browse files Browse the repository at this point in the history
  • Loading branch information
alphastrata committed Apr 8, 2023
1 parent 30d7337 commit 4848bb3
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 119 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Expand Up @@ -23,4 +23,6 @@ lab = "0.11.0"
memmap = "0.7.0"
serde = { version = "1.0.125", features = ["derive"] }
string-builder = "0.2.0"
itertools = "0.10.5"
itertools = "0.10.5"


19 changes: 14 additions & 5 deletions src/binfilereader.rs
@@ -1,6 +1,7 @@
use memmap::Mmap;
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Endian {
Expand Down Expand Up @@ -33,24 +34,32 @@ macro_rules! bytes_to_primitive {
pub struct BinFileReader {
file_ptr: File,
map: Mmap,
file_path: String,
file_path: PathBuf,
endiness: Endian,
}

/// A strongly (over)simplified means of reading a file directly into primitive types. Wraps around a memory mapped file pointer
impl BinFileReader {
pub fn new(file_path: &str) -> BinFileReader {
pub fn new<P>(file_path: P) -> BinFileReader
where
P: AsRef<Path> + Copy,
PathBuf: From<P>,
{
BinFileReader::new_as_endiness(file_path, Endian::LittleEndian)
}

pub fn new_as_endiness(file_path: &str, endiness: Endian) -> BinFileReader {
pub fn new_as_endiness<P>(file_path: P, endiness: Endian) -> BinFileReader
where
P: AsRef<Path> + Copy,
PathBuf: From<P>,
{
let file_ptr = File::open(file_path).expect("Error opening file");
let map: Mmap = unsafe { Mmap::map(&file_ptr).expect("Error creating memory map") };

BinFileReader {
file_ptr,
map,
file_path: file_path.to_string(),
file_path: file_path.as_ref().into(),
endiness,
}
}
Expand Down Expand Up @@ -202,7 +211,7 @@ impl BinFileReader {
self.map.is_empty()
}

pub fn source_file_path(&self) -> String {
pub fn source_file_path(&self) -> PathBuf {
self.file_path.clone()
}

Expand Down
6 changes: 3 additions & 3 deletions src/debayer/amaze.rs
Expand Up @@ -130,9 +130,9 @@ fn imagebuffer_to_vek_array(buffer: &ImageBuffer) -> Vek<Vek<f32>> {
let mut image = vec_of_size(buffer.width * buffer.height, vec_of_size(3, 0.0_f32));
for y in 0..buffer.height {
for x in 0..buffer.width {
image[y * buffer.width + x][0] = buffer.get(x, y).unwrap_or(0.0);
image[y * buffer.width + x][1] = buffer.get(x, y).unwrap_or(0.0);
image[y * buffer.width + x][2] = buffer.get(x, y).unwrap_or(0.0);
image[y * buffer.width + x][0] = buffer.get(x, y);
image[y * buffer.width + x][1] = buffer.get(x, y);
image[y * buffer.width + x][2] = buffer.get(x, y);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/debayer/malvar.rs
Expand Up @@ -50,7 +50,7 @@ fn extract_window(
} else {
mask_5x5_window[((ny + 2) * 5 + (nx + 2)) as usize] = true;
data_5x5_window[((ny + 2) * 5 + (nx + 2)) as usize] =
buffer.get(bx as usize, by as usize).unwrap();
buffer.get(bx as usize, by as usize);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/decompanding.rs
Expand Up @@ -58,7 +58,7 @@ fn get_lut_value_from_ilt_value(ilt_value: u32, ilt: &[u32; 256]) -> u32 {
pub fn compand_buffer(buffer: &mut ImageBuffer, ilt: &[u32; 256]) {
for x in 0..buffer.width {
for y in 0..buffer.height {
let ilt_value = buffer.get(x, y).unwrap();
let ilt_value = buffer.get(x, y);
let lut_value = get_lut_value_from_ilt_value(ilt_value as u32, ilt);
buffer.put(x, y, lut_value as f32);
}
Expand Down
2 changes: 1 addition & 1 deletion src/drawable.rs
Expand Up @@ -306,7 +306,7 @@ impl Drawable for Image {

for channel in 0..num_channels {
let mut v = interpolated_color.get_channel_value(channel);
let v0 = self.get_band(channel).get(x, y).unwrap() as f64;
let v0 = self.get_band(channel).get(x, y) as f64;
if point_mask && avg_pixels && v0 > 0.0 {
v = (v + v0) / 2.0;
}
Expand Down
6 changes: 2 additions & 4 deletions src/guassianblur.rs
Expand Up @@ -65,8 +65,7 @@ pub fn guassian_blur_nband(
let kernel_value = kernel[(kernel_i + r) as usize];

(0..buff_len).for_each(|b| {
values[b] +=
buffers[b].get(x - kernel_i as usize, y).unwrap() * kernel_value;
values[b] += buffers[b].get(x - kernel_i as usize, y) * kernel_value;
});
}
});
Expand All @@ -88,8 +87,7 @@ pub fn guassian_blur_nband(
let kernel_value = kernel[(kernel_i + r) as usize];
(0..buff_len).for_each(|b| {
//FIXME: unsafe unwrap
values[b] +=
buffers[b].get(x, y - kernel_i as usize).unwrap() * kernel_value;
values[b] += buffers[b].get(x, y - kernel_i as usize) * kernel_value;
});
}
});
Expand Down
6 changes: 3 additions & 3 deletions src/hotpixel.rs
Expand Up @@ -37,7 +37,7 @@ fn isolate_window(buffer: &ImageBuffer, window_size: i32, x: usize, y: usize) ->
&& get_y >= 0
&& get_y < buffer.height as i32
{
v.push(buffer.get(get_x as usize, get_y as usize).unwrap());
v.push(buffer.get(get_x as usize, get_y as usize));
}
}
}
Expand All @@ -54,7 +54,7 @@ pub fn hot_pixel_detection(

for y in 1..buffer.height - 1 {
for x in 1..buffer.width - 1 {
let pixel_value = buffer.get(x, y).unwrap();
let pixel_value = buffer.get(x, y);
let window = isolate_window(buffer, window_size, x, y);
let z_score = stats::z_score(pixel_value, &window[0..]).unwrap();
if z_score > threshold {
Expand All @@ -68,7 +68,7 @@ pub fn hot_pixel_detection(
z_score,
});
} else {
map.put(x, y, buffer.get(x, y).unwrap());
map.put(x, y, buffer.get(x, y));
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/image.rs
Expand Up @@ -531,7 +531,7 @@ impl Image {
let mut v = std::f32::MIN;

for i in 0..self.bands.len() {
let b = self.bands[i].get(x, y).unwrap();
let b = self.bands[i].get(x, y);
if v == std::f32::MIN {
v = b;
} else if v != b {
Expand Down Expand Up @@ -715,7 +715,7 @@ impl Image {
out_img.put_pixel(
x as u32,
y as u32,
Luma([self.bands[band].get(x, y).unwrap().round() as u16]),
Luma([self.bands[band].get(x, y).round() as u16]),
);
}
}
Expand All @@ -741,9 +741,9 @@ impl Image {
x as u32,
y as u32,
Rgba([
self.bands[0].get(x, y).unwrap().round() as u16,
self.bands[1].get(x, y).unwrap().round() as u16,
self.bands[2].get(x, y).unwrap().round() as u16,
self.bands[0].get(x, y).round() as u16,
self.bands[1].get(x, y).round() as u16,
self.bands[2].get(x, y).round() as u16,
if self.get_alpha_at(x, y) {
std::u16::MAX
} else {
Expand Down Expand Up @@ -775,9 +775,9 @@ impl Image {
x as u32,
y as u32,
Rgb([
self.bands[0].get(x, y).unwrap().round() as u16,
self.bands[1].get(x, y).unwrap().round() as u16,
self.bands[2].get(x, y).unwrap().round() as u16,
self.bands[0].get(x, y).round() as u16,
self.bands[1].get(x, y).round() as u16,
self.bands[2].get(x, y).round() as u16,
]),
);
}
Expand Down Expand Up @@ -816,7 +816,7 @@ impl Image {
out_img.put_pixel(
x as u32,
y as u32,
Luma([self.bands[band].get(x, y).unwrap().round() as u8]),
Luma([self.bands[band].get(x, y).round() as u8]),
);
}
}
Expand All @@ -843,9 +843,9 @@ impl Image {
x as u32,
y as u32,
Rgba([
self.bands[0].get(x, y).unwrap().round() as u8,
self.bands[1].get(x, y).unwrap().round() as u8,
self.bands[2].get(x, y).unwrap().round() as u8,
self.bands[0].get(x, y).round() as u8,
self.bands[1].get(x, y).round() as u8,
self.bands[2].get(x, y).round() as u8,
if self.get_alpha_at(x, y) {
std::u8::MAX
} else {
Expand Down Expand Up @@ -877,9 +877,9 @@ impl Image {
x as u32,
y as u32,
Rgb([
self.bands[0].get(x, y).unwrap().round() as u8,
self.bands[1].get(x, y).unwrap().round() as u8,
self.bands[2].get(x, y).unwrap().round() as u8,
self.bands[0].get(x, y).round() as u8,
self.bands[1].get(x, y).round() as u8,
self.bands[2].get(x, y).round() as u8,
]),
);
}
Expand Down
25 changes: 13 additions & 12 deletions src/imagebuffer.rs
Expand Up @@ -488,10 +488,11 @@ impl ImageBuffer {
.to_vector()
}

pub fn get(&self, x: usize, y: usize) -> error::Result<f32> {
#[inline(always)]
pub fn get(&self, x: usize, y: usize) -> f32 {
if x < self.width && y < self.height {
let index = y * self.width + x;
Ok(self.buffer[index])
self.buffer[index]
} else {
panic!("Invalid pixel coordinates");
}
Expand All @@ -508,10 +509,10 @@ impl ImageBuffer {
let xd = x - xf;
let yd = y - yf;

let v00 = self.get(xf as usize, yf as usize).unwrap();
let v01 = self.get(xc as usize, yf as usize).unwrap();
let v10 = self.get(xf as usize, yc as usize).unwrap();
let v11 = self.get(xc as usize, yc as usize).unwrap();
let v00 = self.get(xf as usize, yf as usize);
let v01 = self.get(xc as usize, yf as usize);
let v10 = self.get(xf as usize, yc as usize);
let v11 = self.get(xc as usize, yc as usize);

let v0 = v10 * yd + v00 * (1.0 - yd);
let v1 = v11 * yd + v01 * (1.0 - yd);
Expand Down Expand Up @@ -752,7 +753,7 @@ impl ImageBuffer {

for y in 0..self.height {
for x in 0..self.width {
let val = self.get(x, y).unwrap();
let val = self.get(x, y);
if val >= threshold {
ox += x as Dn;
oy += y as Dn;
Expand Down Expand Up @@ -813,7 +814,7 @@ impl ImageBuffer {
shifted_buffer.put(
shift_x as usize,
shift_y as usize,
self.get(x as usize, y as usize).unwrap(),
self.get(x as usize, y as usize),
);
}
}
Expand Down Expand Up @@ -873,7 +874,7 @@ impl ImageBuffer {

for y in 0..self.height {
for x in 0..self.width {
let val = self.get(x, y).unwrap().round() as u8;
let val = self.get(x, y).round() as u8;
let a = if self.get_mask_at_point(x, y) {
std::u8::MAX
} else {
Expand All @@ -894,7 +895,7 @@ impl ImageBuffer {

for y in 0..self.height {
for x in 0..self.width {
let val = self.get(x, y).unwrap().round() as u16;
let val = self.get(x, y) as u16;
let a = if self.get_mask_at_point(x, y) {
std::u16::MAX
} else {
Expand All @@ -913,7 +914,7 @@ impl ImageBuffer {

for y in 0..self.height {
for x in 0..self.width {
let val = self.get(x, y).unwrap().round() as u16;
let val = self.get(x, y) as u16;
let a = if self.get_mask_at_point(x, y) {
std::u16::MAX
} else {
Expand All @@ -939,7 +940,7 @@ impl ImageBuffer {

for y in 0..self.height {
for x in 0..self.width {
let val = self.get(x, y).unwrap().round() as u8;
let val = self.get(x, y).round() as u8;
let a = if self.get_mask_at_point(x, y) {
std::u8::MAX
} else {
Expand Down

0 comments on commit 4848bb3

Please sign in to comment.