Skip to content

Commit e7a6c95

Browse files
author
BuildTools
committed
Optimize IntoLinear table generation as detailed by okaneco
1 parent d07ebd0 commit e7a6c95

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

palette/build/lut.rs

+30-20
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ pub fn build_transfer_fn(writer: &mut File) {
144144
}
145145

146146
fn gen_into_linear_lut_u8(writer: &mut File, entries: &[LutEntryU8]) {
147-
use std::io::Write;
147+
use std::fmt::Write as _;
148+
use std::io::Write as _;
148149

149150
for LutEntryU8 {
150151
fn_type,
@@ -155,20 +156,24 @@ fn gen_into_linear_lut_u8(writer: &mut File, entries: &[LutEntryU8]) {
155156
{
156157
let table_size = 1 << 8;
157158
let mut table = Vec::new();
158-
for i in 0..table_size {
159+
// Handle integer floats printing without decimal
160+
table.extend_from_slice(b"\t0.0,\n");
161+
162+
let mut float_string = String::new();
163+
164+
for i in 1..(table_size - 1) {
159165
let encoded = (i as f64) / ((table_size - 1) as f64);
160166
let linear = into_linear(encoded);
161-
// Handle integer floats printing without decimal
162-
let float_string = if linear <= 0.0 {
163-
"\t0.0,\n".to_owned()
164-
} else if linear >= 1.0 {
165-
"\t1.0,\n".to_owned()
166-
} else {
167-
format!("\t{linear},\n")
168-
};
167+
168+
writeln!(&mut float_string, "\t{linear},").unwrap();
169169
table.extend_from_slice(float_string.as_bytes());
170+
171+
float_string.clear();
170172
}
171173

174+
// Handle integer floats printing without decimal
175+
table.extend_from_slice(b"\t1.0,\n");
176+
172177
let table_name = format!("{fn_type_uppercase}_U8_TO_F64");
173178
writeln!(writer, "const {table_name}: [f64; {table_size}] = [").unwrap();
174179
writer.write_all(&table).unwrap();
@@ -200,7 +205,8 @@ fn gen_into_linear_lut_u8(writer: &mut File, entries: &[LutEntryU8]) {
200205

201206
#[cfg(feature = "prophoto_lut")]
202207
fn gen_into_linear_lut_u16(writer: &mut File, entries: &[LutEntryU16]) {
203-
use std::io::Write;
208+
use std::fmt::Write as _;
209+
use std::io::Write as _;
204210

205211
for LutEntryU16 {
206212
fn_type,
@@ -211,20 +217,24 @@ fn gen_into_linear_lut_u16(writer: &mut File, entries: &[LutEntryU16]) {
211217
{
212218
let table_size = 1 << 16;
213219
let mut table = Vec::new();
214-
for i in 0..table_size {
220+
// Handle integer floats printing without decimal
221+
table.extend_from_slice(b"\t0.0,\n");
222+
223+
let mut float_string = String::new();
224+
225+
for i in 1..(table_size - 1) {
215226
let encoded = (i as f64) / ((table_size - 1) as f64);
216227
let linear = into_linear(encoded);
217-
// Handle integer floats printing without decimal
218-
let float_string = if linear <= 0.0 {
219-
"\t0.0,\n".to_owned()
220-
} else if linear >= 1.0 {
221-
"\t1.0,\n".to_owned()
222-
} else {
223-
format!("\t{linear},\n")
224-
};
228+
229+
writeln!(&mut float_string, "\t{linear},").unwrap();
225230
table.extend_from_slice(float_string.as_bytes());
231+
232+
float_string.clear();
226233
}
227234

235+
// Handle integer floats printing without decimal
236+
table.extend_from_slice(b"\t1.0,\n");
237+
228238
let table_name = format!("{fn_type_uppercase}_U16_TO_F64");
229239
writeln!(writer, "static {table_name}: [f64; {table_size}] = [").unwrap();
230240
writer.write_all(&table).unwrap();

0 commit comments

Comments
 (0)