Skip to content

Commit 0833247

Browse files
committed
improve lut_filters_bench
1 parent 47f4e24 commit 0833247

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/lut_filters.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,20 @@ mod naive {
6868
}
6969

7070
/// Naive implementation: Apply gamma correction
71-
/// This is VERY slow because powf() is expensive!
7271
pub fn apply_gamma(img: &RgbImage, gamma: f32) -> RgbImage {
7372
let (width, height) = img.dimensions();
7473
let mut output = ImageBuffer::new(width, height);
7574

75+
let mut lut = vec![0.0; 256];
76+
for i in 0..256 {
77+
lut[i] = (i as f32 / 255.0).powf(1.0 / gamma) * 255.0;
78+
}
79+
7680
for (x, y, pixel) in img.enumerate_pixels() {
7781
// powf() is VERY expensive - this is why we need a LUT!
78-
let r = (pixel[0] as f32 / 255.0).powf(1.0 / gamma) * 255.0;
79-
let g = (pixel[1] as f32 / 255.0).powf(1.0 / gamma) * 255.0;
80-
let b = (pixel[2] as f32 / 255.0).powf(1.0 / gamma) * 255.0;
82+
let r = lut[pixel[0] as usize];
83+
let g = lut[pixel[1] as usize];
84+
let b = lut[pixel[2] as usize];
8185

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

0 commit comments

Comments
 (0)