Skip to content

Commit

Permalink
rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Beinsezii committed Jan 3, 2024
1 parent bf26323 commit 4c2c7ef
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 40 deletions.
47 changes: 29 additions & 18 deletions examples/spectrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ const WIDTH: usize = 360;
const HEIGHT: usize = 100;

fn write_ppm(pixels: &[[f32; 3]], name: &str) {
std::fs::write(
format!("{}.ppm", name),
pixels.iter().map(|pixel| srgb_to_irgb(*pixel))
.fold(format!("P3 {} {} 255", WIDTH, HEIGHT), |acc, it| {
acc + "\n" + &it.map(|c| c.to_string()).join(" ")
})
+ "\n", // newline needed for some libs
)
.unwrap()

std::fs::write(
format!("{}.ppm", name),
pixels
.iter()
.map(|pixel| srgb_to_irgb(*pixel))
.fold(format!("P3 {} {} 255", WIDTH, HEIGHT), |acc, it| {
acc + "\n" + &it.map(|c| c.to_string()).join(" ")
})
+ "\n", // newline needed for some libs
)
.unwrap()
}

fn main() {
Expand Down Expand Up @@ -51,21 +52,31 @@ fn main() {
Space::JZCZHZ => [5650.0, 5650.0, 1.0],
_ => [1.0, 1.0, 1.0],
};
data.iter_mut()
.for_each(|p| {p.iter_mut()
.zip(add.iter()).zip(div.iter())
.for_each(|((c, a), d)| {*c += *a; *c /= *d});
if space == Space::HSV {*p = [p[2], p[1], p[0]]}
});
data.iter_mut().for_each(|p| {
p.iter_mut()
.zip(add.iter())
.zip(div.iter())
.for_each(|((c, a), d)| {
*c += *a;
*c /= *d
});
if space == Space::HSV {
*p = [p[2], p[1], p[0]]
}
});
convert_space_chunked(space, Space::SRGB, &mut data);

write_ppm(&data, filename)
}

let mut ictcp: Vec<[f32; 3]> = pixels.iter().map(|a| a.to_vec()).reduce(|mut acc, mut it| {
let mut ictcp: Vec<[f32; 3]> = pixels
.iter()
.map(|a| a.to_vec())
.reduce(|mut acc, mut it| {
acc.append(&mut it);
acc
}).unwrap();
})
.unwrap();
ictcp.iter_mut().for_each(|p| {
p[0] += 15.0;
p[0] /= 800.0;
Expand Down
51 changes: 29 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ const ICTCP_M2: [[f32; 3]; 3] = [
];

const ICTCP_M1_INV: [[f32; 3]; 3] = [
[ 3.4366066943, -2.5064521187, 0.0698454243],
[-0.7913295556, 1.9836004518, -0.1922708962],
[-0.0259498997, -0.0989137147, 1.1248636144]
[3.4366066943, -2.5064521187, 0.0698454243],
[-0.7913295556, 1.9836004518, -0.1922708962],
[-0.0259498997, -0.0989137147, 1.1248636144],
];
const ICTCP_M2_INV: [[f32; 3]; 3] = [
[ 1. , 0.008609037 , 0.111029625 ],
[ 1. , -0.008609037 , -0.111029625 ],
[ 1. , 0.5600313357, -0.320627175 ]
[1., 0.008609037, 0.111029625],
[1., -0.008609037, -0.111029625],
[1., 0.5600313357, -0.320627175],
];
/// 3 * 3x3 Matrix multiply
fn matmul3(pixel: [f32; 3], matrix: [[f32; 3]; 3]) -> [f32; 3] {
Expand Down Expand Up @@ -191,9 +191,10 @@ pub extern "C" fn srgb_eotf_inverse(n: f32) -> f32 {
#[no_mangle]
pub extern "C" fn _pq_eotf(e: f32) -> f32 {
let y = spowf(
(spowf(e, 1.0 / PQEOTF_M2) - PQEOTF_C1).max(0.0) /
(PQEOTF_C2 - PQEOTF_C3 * spowf(e, 1.0 / PQEOTF_M2))
, 1.0 / PQEOTF_M1);
(spowf(e, 1.0 / PQEOTF_M2) - PQEOTF_C1).max(0.0)
/ (PQEOTF_C2 - PQEOTF_C3 * spowf(e, 1.0 / PQEOTF_M2)),
1.0 / PQEOTF_M1,
);
10000.0 * y
}

Expand All @@ -203,9 +204,10 @@ pub extern "C" fn _pq_eotf(e: f32) -> f32 {
#[no_mangle]
pub extern "C" fn pqz_eotf(e: f32) -> f32 {
let y = spowf(
(spowf(e, 1.0 / JZAZBZ_P) - PQEOTF_C1).max(0.0) /
(PQEOTF_C2 - PQEOTF_C3 * spowf(e, 1.0 / JZAZBZ_P))
, 1.0 / PQEOTF_M1);
(spowf(e, 1.0 / JZAZBZ_P) - PQEOTF_C1).max(0.0)
/ (PQEOTF_C2 - PQEOTF_C3 * spowf(e, 1.0 / JZAZBZ_P)),
1.0 / PQEOTF_M1,
);
10000.0 * y
}

Expand All @@ -216,9 +218,9 @@ pub extern "C" fn pqz_eotf(e: f32) -> f32 {
pub extern "C" fn _pq_oetf(f: f32) -> f32 {
let y = f / 10000.0;
spowf(
(PQEOTF_C1 + PQEOTF_C2 * spowf(y, PQEOTF_M1)) /
(1.0 + PQEOTF_C3 * spowf(y, PQEOTF_M1))
, PQEOTF_M2)
(PQEOTF_C1 + PQEOTF_C2 * spowf(y, PQEOTF_M1)) / (1.0 + PQEOTF_C3 * spowf(y, PQEOTF_M1)),
PQEOTF_M2,
)
}

/// Dolby Perceptual Quantizer Optical-Electro Transfer Function modified for JzAzBz
Expand All @@ -228,9 +230,9 @@ pub extern "C" fn _pq_oetf(f: f32) -> f32 {
pub extern "C" fn pqz_oetf(f: f32) -> f32 {
let y = f / 10000.0;
spowf(
(PQEOTF_C1 + PQEOTF_C2 * spowf(y, PQEOTF_M1)) /
(1.0 + PQEOTF_C3 * spowf(y, PQEOTF_M1))
, JZAZBZ_P)
(PQEOTF_C1 + PQEOTF_C2 * spowf(y, PQEOTF_M1)) / (1.0 + PQEOTF_C3 * spowf(y, PQEOTF_M1)),
JZAZBZ_P,
)
}

// ### UTILITIES ### }}}
Expand Down Expand Up @@ -831,7 +833,6 @@ pub extern "C" fn _lrgb_to_ictcp(pixel: &mut [f32; 3]) {
// lms prime
lms.iter_mut().for_each(|c| *c = _pq_oetf(*c));
*pixel = matmul3t(lms, ICTCP_M2);

}

/// Convert from CIE LAB to CIE LCH.
Expand Down Expand Up @@ -1176,13 +1177,19 @@ mod tests {
}
for (a, b) in i.iter().zip(r.iter()) {
if (a - b).abs() > epsilon || !a.is_finite() || !b.is_finite() {
let dev = i.iter().zip(r.iter()).map(|(ix, rx)| ((ix - rx) + 1.0).abs().powi(2)).sum::<f32>();
let dev = i
.iter()
.zip(r.iter())
.map(|(ix, rx)| ((ix - rx) + 1.0).abs().powi(2))
.sum::<f32>();
err.push_str(&format!(
"\nA{n}: {:.8} {:.8} {:.8}\nB{n}: {:.8} {:.8} {:.8}\nERR²: {}\n",
i[0], i[1], i[2], r[0], r[1], r[2], dev
));
if dev.is_finite() {cum_err += dev};
break
if dev.is_finite() {
cum_err += dev
};
break;
}
}
}
Expand Down

0 comments on commit 4c2c7ef

Please sign in to comment.