Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ path = "src/lib.rs"
rand = "0.8"
image = "0.25"
image-compare = "0.5.0"
rayon = "1.11.0"

[dev-dependencies]
divan = { version = "4.0.2", package = "codspeed-divan-compat" }
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ cargo codspeed run -m walltime
```

Note: You can also set the `CODSPEED_RUNNER_MODE` environment variable to `walltime` to avoid passing `-m walltime` every time.

# Sauls Version
11 changes: 6 additions & 5 deletions src/bfs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::collections::{HashSet, VecDeque};

/// A simple graph represented as an adjacency list
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -27,21 +27,22 @@ impl Graph {
/// Returns the order in which nodes were visited
pub fn bfs_naive(graph: &Graph, start: usize) -> Vec<usize> {
let mut visited = HashSet::new();
let mut queue = Vec::new(); // Using Vec instead of VecDeque - intentionally inefficient!
// let mut queue = Vec::new(); // Using Vec instead of VecDeque - intentionally inefficient!
let mut queue = VecDeque::new();
let mut result = Vec::new();

queue.push(start);
queue.push_back(start);
visited.insert(start);

while !queue.is_empty() {
// remove(0) is O(n) - this makes BFS slow!
let node = queue.remove(0);
let node = queue.remove(0).expect("node is available");
result.push(node);

if let Some(neighbors) = graph.adjacency.get(node) {
for &neighbor in neighbors {
if visited.insert(neighbor) {
queue.push(neighbor);
queue.push_back(neighbor);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/dna_matcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use rayon::prelude::*;

/// Naive approach: Read the entire file as a string and filter lines
pub fn naive_dna_matcher(genome: &str, pattern: &str) -> Vec<String> {
genome
.lines()
.par_lines()
.filter(|line| !line.starts_with('>')) // Skip headers
.filter(|line| line.contains(pattern))
.map(|s| s.to_string())
Expand Down
61 changes: 58 additions & 3 deletions src/lut_filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
use image::{ImageBuffer, Rgb, RgbImage};

pub fn apply_brightness_contrast(img: &RgbImage, brightness: i16, contrast: f32) -> RgbImage {
naive::apply_brightness_contrast(img, brightness, contrast)
saul::apply_brightness_contrast(img, brightness, contrast)
}

pub fn apply_gamma(img: &RgbImage, gamma: f32) -> RgbImage {
naive::apply_gamma(img, gamma)
saul::apply_gamma(img, gamma)
}

pub fn apply_brightness_contrast_gamma(
Expand All @@ -32,7 +32,62 @@ pub fn apply_brightness_contrast_gamma(
gamma: f32,
) -> RgbImage {
let temp_img = apply_brightness_contrast(img, brightness, contrast);
naive::apply_gamma(&temp_img, gamma)
saul::apply_gamma(&temp_img, gamma)
}

mod saul {
use super::*;

fn create_brightness_contrast_lut(brightness: i16, contrast: f32) -> [u8; 256] {
let mut ret = [0; 256];
for r in 0..=u8::MAX {
let r_float = r as f32;
let value = ((r_float - 128.0) * (1.0 + contrast)) + 128.0 + brightness as f32;
ret[r as usize] = value.clamp(0.0, 255.0) as u8;
}
ret
}

/// Apply brightness and contrast with floating-point math per pixel
pub fn apply_brightness_contrast(img: &RgbImage, brightness: i16, contrast: f32) -> RgbImage {
let (width, height) = img.dimensions();
let mut output = ImageBuffer::new(width, height);
let lut = create_brightness_contrast_lut(brightness, contrast);

for (x, y, pixel) in img.enumerate_pixels() {
let r = pixel[0] as usize;
let g = pixel[1] as usize;
let b = pixel[2] as usize;
output.put_pixel(x, y, Rgb([lut[r], lut[g], lut[b]]));
}

output
}

fn create_gamma_lut(gamma: f32) -> [u8; 256] {
let mut ret = [0; 256];
for r in 0..=u8::MAX {
let value = (r as f32 / 255.0).powf(1.0 / gamma) * 255.0;
ret[r as usize] = value as u8;
}
ret
}

pub fn apply_gamma(img: &RgbImage, gamma: f32) -> RgbImage {
let (width, height) = img.dimensions();
let mut output = ImageBuffer::new(width, height);
let lut = create_gamma_lut(gamma);

for (x, y, pixel) in img.enumerate_pixels() {
let r = lut[pixel[0] as usize];
let g = lut[pixel[1] as usize];
let b = lut[pixel[2] as usize];

output.put_pixel(x, y, Rgb([r, g, b]));
}

output
}
}

mod naive {
Expand Down