Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

Read .cube tables with a byte-order mark or Resolve's LUT_3D_INPUT_RANGE

theworker0255a17268d85c
diff --git a/src/crates/concat-effects/src/cube.rs b/src/crates/concat-effects/src/cube.rs
index 7cefc9c0..6bb88379 100644
--- a/src/crates/concat-effects/src/cube.rs
+++ b/src/crates/concat-effects/src/cube.rs
@@ -4,10 +4,11 @@
//! The `.cube` file: the look-up table format every grading tool writes.
//!
//! A header of `LUT_3D_SIZE n`, optionally a `TITLE` and a `DOMAIN_MIN` /
-//! `DOMAIN_MAX`, then `n³` rows of three numbers, red changing fastest.
-//! Read with the same tolerance as a manifest: comments and blank lines
-//! are skipped, a domain other than 0..1 is rescaled, and anything the
-//! rows do not add up to is an error naming the line.
+//! `DOMAIN_MAX` (or DaVinci Resolve's `LUT_3D_INPUT_RANGE`), then `n³` rows
+//! of three numbers, red changing fastest. Read with the same tolerance as
+//! a manifest: a byte-order mark, comments, blank lines and keywords the
+//! table does not need are skipped, a domain other than 0..1 is rescaled,
+//! and anything the rows do not add up to is an error naming the line.
use concat_core::Lut;
@@ -17,6 +18,7 @@ pub const LARGEST_TABLE: u32 = 65;
/// Parses the text of a `.cube` file into a table.
pub fn parse(text: &str) -> Result<Lut, String> {
+ let text = text.strip_prefix('\u{feff}').unwrap_or(text);
let mut size: Option<u32> = None;
let mut domain_min = [0.0f32; 3];
let mut domain_max = [1.0f32; 3];
@@ -57,6 +59,21 @@ pub fn parse(text: &str) -> Result<Lut, String> {
domain_max = triple;
}
}
+ "LUT_3D_INPUT_RANGE" => {
+ let mut pair = [0.0f32; 2];
+ for slot in &mut pair {
+ *slot = words
+ .next()
+ .and_then(|w| w.parse().ok())
+ .ok_or_else(|| format!("line {at}: {head} needs two numbers"))?;
+ }
+ domain_min = [pair[0]; 3];
+ domain_max = [pair[1]; 3];
+ }
+ // Another tool's keyword, such as Resolve's `LUT_IN_VIDEO_RANGE`:
+ // nothing the table's rows depend on.
+ _ if head.starts_with(|c: char| c.is_ascii_alphabetic())
+ && head.parse::<f32>().is_err() => {}
_ => {
let mut triple = [0.0f32; 3];
let mut rest = line.split_whitespace();
@@ -143,6 +160,43 @@ mod tests {
);
}
+ /// A table saved with a byte-order mark reads like one without.
+ #[test]
+ fn a_byte_order_mark_is_skipped() {
+ let lut = parse(&format!("\u{feff}{}", identity(3))).expect("parses");
+ assert_eq!(lut.size, 3);
+ }
+
+ /// DaVinci Resolve writes its domain as `LUT_3D_INPUT_RANGE`, and other
+ /// tools add keywords of their own; neither stops a table reading.
+ #[test]
+ fn resolve_input_range_and_unknown_keywords_are_read() {
+ let mut text = String::from(
+ "# Created by: DaVinci Resolve\nLUT_3D_SIZE 2\nLUT_3D_INPUT_RANGE 0.0 2.0\n\
+ LUT_IN_VIDEO_RANGE\n",
+ );
+ for b in 0..2 {
+ for g in 0..2 {
+ for r in 0..2 {
+ text.push_str(&format!("{} {} {}\n", r * 2, g * 2, b * 2));
+ }
+ }
+ }
+ let lut = parse(&text).expect("parses");
+ let out = lut.sample([0.5, 0.25, 1.0]);
+ assert!(
+ (out[0] - 0.5).abs() < 0.01
+ && (out[1] - 0.25).abs() < 0.01
+ && (out[2] - 1.0).abs() < 0.01,
+ "{out:?}"
+ );
+ assert!(
+ parse("LUT_3D_SIZE 2\nLUT_3D_INPUT_RANGE 0.0\n")
+ .unwrap_err()
+ .contains("line 2")
+ );
+ }
+
#[test]
fn a_short_table_names_the_shortfall() {
let mut text = identity(3);
diff --git a/src/crates/concat-effects/src/looks.rs b/src/crates/concat-effects/src/looks.rs
index 065631bc..a4e9dd44 100644
--- a/src/crates/concat-effects/src/looks.rs
+++ b/src/crates/concat-effects/src/looks.rs
@@ -89,13 +89,15 @@ pub fn import(dir: &Path, path: &Path) -> Result<String, String> {
}
let id = format!("user.{slug}");
let text = std::fs::read_to_string(path).map_err(|error| error.to_string())?;
- crate::cube::parse(&text)?;
+ // A byte-order mark would hide a `TITLE` on the first line from `space_of`.
+ let text = text.strip_prefix('\u{feff}').unwrap_or(&text);
+ crate::cube::parse(text)?;
let folder = dir.join(&id);
std::fs::create_dir_all(&folder).map_err(|error| error.to_string())?;
- let written = manifest(&id, stem.trim(), TABLE, space_of(path, &text));
+ let written = manifest(&id, stem.trim(), TABLE, space_of(path, text));
std::fs::write(folder.join("effect.toml"), written).map_err(|error| error.to_string())?;
std::fs::write(folder.join("effect.wgsl"), SHADER).map_err(|error| error.to_string())?;
- std::fs::write(folder.join(TABLE), &text).map_err(|error| error.to_string())?;
+ std::fs::write(folder.join(TABLE), text).map_err(|error| error.to_string())?;
// A still an earlier import drew for this name: the card drawn from the
// package's own shader replaces it.
let _ = std::fs::remove_file(folder.join("preview.png"));
@@ -227,6 +229,25 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
+ /// A table saved with a byte-order mark still has its title read for
+ /// the space, and its package keeps the table without the mark.
+ #[test]
+ fn a_table_with_a_byte_order_mark_imports() {
+ let dir = scratch("bom");
+ let source = dir.join("Grade.cube");
+ std::fs::write(&source, format!("\u{feff}{}", table("Grade ACEScct"))).expect("a table");
+ let looks = dir.join("looks");
+ let id = import(&looks, &source).expect("imports");
+ let package = Package::from_folder(&looks.join(&id)).expect("loads");
+ assert_eq!(
+ package.manifest.wgsl.as_ref().map(|wgsl| wgsl.space),
+ Some(Space::Log)
+ );
+ let kept = std::fs::read_to_string(looks.join(&id).join(TABLE)).expect("the table");
+ assert!(kept.starts_with("TITLE"), "{kept:?}");
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
/// A look an earlier build imported is rewritten onto the format 2 look,
/// once; a package of the same shape written by hand with anything
/// more in it is left as it was, and is refused at load.