Skip to content

Commit

Permalink
Optimize IntoLinear table generation as detailed by okaneco
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Aug 22, 2024
1 parent d07ebd0 commit e7a6c95
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions palette/build/lut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ pub fn build_transfer_fn(writer: &mut File) {
}

fn gen_into_linear_lut_u8(writer: &mut File, entries: &[LutEntryU8]) {
use std::io::Write;
use std::fmt::Write as _;
use std::io::Write as _;

for LutEntryU8 {
fn_type,
Expand All @@ -155,20 +156,24 @@ fn gen_into_linear_lut_u8(writer: &mut File, entries: &[LutEntryU8]) {
{
let table_size = 1 << 8;
let mut table = Vec::new();
for i in 0..table_size {
// Handle integer floats printing without decimal
table.extend_from_slice(b"\t0.0,\n");

let mut float_string = String::new();

for i in 1..(table_size - 1) {
let encoded = (i as f64) / ((table_size - 1) as f64);
let linear = into_linear(encoded);
// Handle integer floats printing without decimal
let float_string = if linear <= 0.0 {
"\t0.0,\n".to_owned()
} else if linear >= 1.0 {
"\t1.0,\n".to_owned()
} else {
format!("\t{linear},\n")
};

writeln!(&mut float_string, "\t{linear},").unwrap();
table.extend_from_slice(float_string.as_bytes());

float_string.clear();
}

// Handle integer floats printing without decimal
table.extend_from_slice(b"\t1.0,\n");

let table_name = format!("{fn_type_uppercase}_U8_TO_F64");
writeln!(writer, "const {table_name}: [f64; {table_size}] = [").unwrap();
writer.write_all(&table).unwrap();
Expand Down Expand Up @@ -200,7 +205,8 @@ fn gen_into_linear_lut_u8(writer: &mut File, entries: &[LutEntryU8]) {

#[cfg(feature = "prophoto_lut")]
fn gen_into_linear_lut_u16(writer: &mut File, entries: &[LutEntryU16]) {
use std::io::Write;
use std::fmt::Write as _;
use std::io::Write as _;

for LutEntryU16 {
fn_type,
Expand All @@ -211,20 +217,24 @@ fn gen_into_linear_lut_u16(writer: &mut File, entries: &[LutEntryU16]) {
{
let table_size = 1 << 16;
let mut table = Vec::new();
for i in 0..table_size {
// Handle integer floats printing without decimal
table.extend_from_slice(b"\t0.0,\n");

let mut float_string = String::new();

for i in 1..(table_size - 1) {
let encoded = (i as f64) / ((table_size - 1) as f64);
let linear = into_linear(encoded);
// Handle integer floats printing without decimal
let float_string = if linear <= 0.0 {
"\t0.0,\n".to_owned()
} else if linear >= 1.0 {
"\t1.0,\n".to_owned()
} else {
format!("\t{linear},\n")
};

writeln!(&mut float_string, "\t{linear},").unwrap();
table.extend_from_slice(float_string.as_bytes());

float_string.clear();
}

// Handle integer floats printing without decimal
table.extend_from_slice(b"\t1.0,\n");

let table_name = format!("{fn_type_uppercase}_U16_TO_F64");
writeln!(writer, "static {table_name}: [f64; {table_size}] = [").unwrap();
writer.write_all(&table).unwrap();
Expand Down

0 comments on commit e7a6c95

Please sign in to comment.