Refactor code a bit.

This commit is contained in:
Nathan Vegdahl 2022-04-13 15:15:46 -07:00
parent 1b19033f15
commit 10635b26ac
2 changed files with 101 additions and 56 deletions

View File

@ -1,60 +1,10 @@
const GRADIENT_LEN: usize = 1 << 16; mod test_image;
const TABLE_SIZE: usize = 128;
const RES_X: usize = 2048; use test_image::{RES_X, RES_Y};
const RES_Y: usize = 1556;
fn main() { fn main() {
// Build the test image. // Build the test image.
let pixels = { let pixels = test_image::build();
let mut pixels = vec![[0.0f32; 3]; RES_X * RES_Y];
assert!(((GRADIENT_LEN * 4) + (TABLE_SIZE * TABLE_SIZE * TABLE_SIZE)) <= pixels.len());
// Keep track of which pixel we're at.
let mut pi = 0;
// Gray gradient.
for i in 0..GRADIENT_LEN {
let v = i as f32 / (GRADIENT_LEN - 1) as f32;
pixels[pi] = [v, v, v];
pi += 1;
}
// Red gradient.
for i in 0..GRADIENT_LEN {
let v = i as f32 / (GRADIENT_LEN - 1) as f32;
pixels[pi][0] = v;
pi += 1;
}
// Green gradient.
for i in 0..GRADIENT_LEN {
let v = i as f32 / (GRADIENT_LEN - 1) as f32;
pixels[pi][1] = v;
pi += 1;
}
// Blue gradient.
for i in 0..GRADIENT_LEN {
let v = i as f32 / (GRADIENT_LEN - 1) as f32;
pixels[pi][2] = v;
pi += 1;
}
// 3D RGB table.
for r in 0..TABLE_SIZE {
let rn = r as f32 / (TABLE_SIZE - 1) as f32;
for g in 0..TABLE_SIZE {
let gn = g as f32 / (TABLE_SIZE - 1) as f32;
for b in 0..TABLE_SIZE {
let bn = b as f32 / (TABLE_SIZE - 1) as f32;
pixels[pi] = [rn, gn, bn];
pi += 1;
}
}
}
pixels
};
// Write the test image. // Write the test image.
use exr::{ use exr::{
@ -63,7 +13,7 @@ fn main() {
prelude::WritableImage, prelude::WritableImage,
}; };
Image::from_layer(Layer::new( Image::from_layer(Layer::new(
(1920, 1080), (RES_X, RES_Y),
LayerAttributes::named(""), LayerAttributes::named(""),
Encoding::SMALL_LOSSLESS, Encoding::SMALL_LOSSLESS,
SpecificChannels::rgb(|co: exr::math::Vec2<usize>| { SpecificChannels::rgb(|co: exr::math::Vec2<usize>| {
@ -72,6 +22,6 @@ fn main() {
}), }),
)) ))
.write() .write()
.to_file("lut_extractor_2048x1556.exr") .to_file(format!("lut_extractor_{}x{}.exr", RES_X, RES_Y))
.unwrap(); .unwrap();
} }

95
src/test_image.rs Normal file
View File

@ -0,0 +1,95 @@
pub const GRADIENT_LEN: usize = 1 << 17;
pub const TABLE_SIZE: usize = 144;
pub const RES_X: usize = 2560;
pub const RES_Y: usize = 1440;
// Compile time assert.
const _: () = {
assert!(((GRADIENT_LEN * 4) + (TABLE_SIZE * TABLE_SIZE * TABLE_SIZE)) <= (RES_X * RES_Y));
};
/// Creates the test image, as a Vec of RGB triples.
pub fn build() -> Vec<[f32; 3]> {
let mut pixels = vec![[0.0f32; 3]; RES_X * RES_Y];
// Keep track of which pixel we're at.
let mut pi = 0;
// Gray gradient.
for i in 0..GRADIENT_LEN {
let v = i as f32 / (GRADIENT_LEN - 1) as f32;
pixels[pi] = [v, v, v];
pi += 1;
}
// Red gradient.
for i in 0..GRADIENT_LEN {
let v = i as f32 / (GRADIENT_LEN - 1) as f32;
pixels[pi][0] = v;
pi += 1;
}
// Green gradient.
for i in 0..GRADIENT_LEN {
let v = i as f32 / (GRADIENT_LEN - 1) as f32;
pixels[pi][1] = v;
pi += 1;
}
// Blue gradient.
for i in 0..GRADIENT_LEN {
let v = i as f32 / (GRADIENT_LEN - 1) as f32;
pixels[pi][2] = v;
pi += 1;
}
// 3D RGB table.
for r in 0..TABLE_SIZE {
let rn = r as f32 / (TABLE_SIZE - 1) as f32;
for g in 0..TABLE_SIZE {
let gn = g as f32 / (TABLE_SIZE - 1) as f32;
for b in 0..TABLE_SIZE {
let bn = b as f32 / (TABLE_SIZE - 1) as f32;
pixels[pi] = [rn, gn, bn];
pi += 1;
}
}
}
pixels
}
/// Gives the index in the test image Vec of the given
/// gray-gradient index.
#[inline(always)]
pub fn gray_idx(idx: usize) -> usize {
idx
}
/// Gives the index in the test image Vec of the given
/// red-gradient index.
#[inline(always)]
pub fn red_idx(idx: usize) -> usize {
GRADIENT_LEN + idx
}
/// Gives the index in the test image Vec of the given
/// green-gradient index.
#[inline(always)]
pub fn green_idx(idx: usize) -> usize {
GRADIENT_LEN * 2 + idx
}
/// Gives the index in the test image Vec of the given
/// blue-gradient index.
#[inline(always)]
pub fn blue_idx(idx: usize) -> usize {
GRADIENT_LEN * 3 + idx
}
/// Gives the index in the test image Vec of the given
/// rgb table indices.
#[inline(always)]
pub fn rgb_idx(r_idx: usize, g_idx: usize, b_idx: usize) -> usize {
(GRADIENT_LEN * 4) + (r_idx * TABLE_SIZE * TABLE_SIZE) + (g_idx * TABLE_SIZE) + b_idx
}