Hoist math out of inner loops
All checks were successful
All checks were successful
This commit is contained in:
parent
d72632245b
commit
164b11c2f3
36
src/lib.rs
36
src/lib.rs
|
@ -79,6 +79,9 @@ impl Encoder {
|
|||
// get offset in terms of remaining bytes on head of rgba8_image
|
||||
let offset = (BYTES_PER_PIXEL - offset) % BYTES_PER_PIXEL;
|
||||
|
||||
let basis_scale_x = PI / self.bounds.width as f32;
|
||||
let basis_scale_y = PI / self.bounds.height as f32;
|
||||
|
||||
for (ComponentState { basis, .. }, [_, g, b]) in self.factors.iter_mut() {
|
||||
for (byte, value) in rgba8_image[..offset].iter().zip(
|
||||
[&mut *b, &mut *g][..offset.saturating_sub(BYTES_PER_PIXEL - 2)]
|
||||
|
@ -97,16 +100,11 @@ impl Encoder {
|
|||
let px = pixels + i as u32;
|
||||
let px_x = px % self.bounds.width;
|
||||
let px_y = px / self.bounds.width;
|
||||
let scale_x = px_x as f32 * basis_scale_x;
|
||||
let scale_y = px_y as f32 * basis_scale_y;
|
||||
|
||||
for (ComponentState { x, y, .. }, [r, g, b]) in self.factors.iter_mut() {
|
||||
let basis = compute_basis(
|
||||
*x as _,
|
||||
*y as _,
|
||||
px_x as _,
|
||||
px_y as _,
|
||||
self.bounds.width as _,
|
||||
self.bounds.height as _,
|
||||
);
|
||||
let basis = f32::cos(*x as f32 * scale_x) * f32::cos(*y as f32 * scale_y);
|
||||
|
||||
*r += basis * srgb_to_linear(chunk[0]);
|
||||
*g += basis * srgb_to_linear(chunk[1]);
|
||||
|
@ -118,16 +116,11 @@ impl Encoder {
|
|||
let px = pixels + (rgba8_image[offset..].len() / BYTES_PER_PIXEL) as u32;
|
||||
let px_x = px % self.bounds.width;
|
||||
let px_y = px / self.bounds.width;
|
||||
let scale_x = px_x as f32 * basis_scale_x;
|
||||
let scale_y = px_y as f32 * basis_scale_y;
|
||||
|
||||
for (ComponentState { x, y, basis }, [r, g, b]) in self.factors.iter_mut() {
|
||||
*basis = compute_basis(
|
||||
*x as _,
|
||||
*y as _,
|
||||
px_x as _,
|
||||
px_y as _,
|
||||
self.bounds.width as _,
|
||||
self.bounds.height as _,
|
||||
);
|
||||
*basis = f32::cos(*x as f32 * scale_x) * f32::cos(*y as f32 * scale_y);
|
||||
|
||||
for (byte, value) in chunks.remainder().iter().zip([&mut *r, &mut *g, &mut *b]) {
|
||||
*value += *basis * srgb_to_linear(*byte);
|
||||
|
@ -177,17 +170,6 @@ impl Encoder {
|
|||
}
|
||||
}
|
||||
|
||||
fn compute_basis(
|
||||
component_x: f32,
|
||||
component_y: f32,
|
||||
px_x: f32,
|
||||
px_y: f32,
|
||||
width: f32,
|
||||
height: f32,
|
||||
) -> f32 {
|
||||
f32::cos(PI * component_x * px_x / width) * f32::cos(PI * component_y * px_y / height)
|
||||
}
|
||||
|
||||
fn encode_dc([r, g, b]: [f32; 3]) -> u32 {
|
||||
let r = linear_to_srgb(r);
|
||||
let g = linear_to_srgb(g);
|
||||
|
|
Loading…
Reference in a new issue