11import { describe , expect , it } from "vitest" ;
2- import { CubeLutParseError , packCubeLutToRgba8 , parseCubeLut } from "./colorLuts" ;
2+ import {
3+ CubeLutParseError ,
4+ fitCubeLutToTexture ,
5+ packCubeLutToRgba8 ,
6+ parseCubeLut ,
7+ type CubeLut3D ,
8+ } from "./colorLuts" ;
39
410const IDENTITY_2 = `
511# comment
@@ -102,9 +108,12 @@ describe("cube LUT parsing", () => {
102108 ` ) ,
103109 ) . toThrow ( "Expected 8 LUT rows" ) ;
104110
105- expect ( ( ) => parseCubeLut ( "LUT_3D_SIZE 65" , { maxSize : 64 } ) ) . toThrow (
106- "LUT_3D_SIZE 65 exceeds max 64" ,
107- ) ;
111+ expect ( ( ) => parseCubeLut ( "LUT_3D_SIZE 66" ) ) . toThrow ( "LUT_3D_SIZE 66 exceeds max 65" ) ;
112+ } ) ;
113+
114+ it ( "accepts the 65-point cube that grading tools export" , ( ) => {
115+ const rows = Array . from ( { length : 65 ** 3 } , ( ) => "0 0 0" ) . join ( "\n" ) ;
116+ expect ( parseCubeLut ( `LUT_3D_SIZE 65\n${ rows } ` ) . size ) . toBe ( 65 ) ;
108117 } ) ;
109118
110119 it ( "reports invalid numbers as cube parse errors" , ( ) => {
@@ -116,3 +125,69 @@ describe("cube LUT parsing", () => {
116125 ) . toThrow ( CubeLutParseError ) ;
117126 } ) ;
118127} ) ;
128+
129+ function identityCube ( size : number ) : CubeLut3D {
130+ const data = new Float32Array ( size ** 3 * 3 ) ;
131+ for ( let b = 0 ; b < size ; b ++ )
132+ for ( let g = 0 ; g < size ; g ++ )
133+ for ( let r = 0 ; r < size ; r ++ ) {
134+ const at = ( ( b * size + g ) * size + r ) * 3 ;
135+ data . set ( [ r / ( size - 1 ) , g / ( size - 1 ) , b / ( size - 1 ) ] , at ) ;
136+ }
137+ return { title : null , size, domainMin : [ 0 , 0 , 0 ] , domainMax : [ 1 , 1 , 1 ] , data } ;
138+ }
139+
140+ describe ( "fitCubeLutToTexture" , ( ) => {
141+ it ( "resamples a cube whose packed row is wider than the texture limit, keeping an identity identity" , ( ) => {
142+ const fitted = fitCubeLutToTexture ( identityCube ( 65 ) , 4096 ) ;
143+
144+ expect ( fitted . size ) . toBe ( 64 ) ;
145+ expect ( packCubeLutToRgba8 ( fitted ) . width ) . toBeLessThanOrEqual ( 4096 ) ;
146+ const expected = identityCube ( 64 ) . data ;
147+ const worst = expected . reduce ( ( m , v , i ) => Math . max ( m , Math . abs ( v - fitted . data [ i ] ! ) ) , 0 ) ;
148+ expect ( worst ) . toBeLessThan ( 1e-5 ) ;
149+ } ) ;
150+
151+ it ( "keeps the domain and title of the cube it resamples" , ( ) => {
152+ const cube = {
153+ ...identityCube ( 5 ) ,
154+ title : "warm" ,
155+ domainMin : [ 0 , 0 , 0.1 ] ,
156+ domainMax : [ 1 , 2 , 3 ] ,
157+ } ;
158+
159+ const fitted = fitCubeLutToTexture ( cube as CubeLut3D , 16 ) ;
160+
161+ expect ( fitted ) . toMatchObject ( {
162+ size : 4 ,
163+ title : "warm" ,
164+ domainMin : [ 0 , 0 , 0.1 ] ,
165+ domainMax : [ 1 , 2 , 3 ] ,
166+ } ) ;
167+ } ) ;
168+
169+ it ( "interpolates between the source points rather than dropping some" , ( ) => {
170+ const cube = identityCube ( 5 ) ;
171+ [ 0 , 0 , 1 , 1 , 1 ] . forEach ( ( red , r ) => {
172+ cube . data [ r * 3 ] = red ;
173+ } ) ;
174+
175+ const fitted = fitCubeLutToTexture ( cube , 16 ) ;
176+
177+ const alongRed = [ 0 , 1 , 2 , 3 ] . map ( ( r ) => fitted . data [ r * 3 ] ! ) ;
178+ expect ( alongRed [ 0 ] ) . toBeCloseTo ( 0 , 5 ) ;
179+ expect ( alongRed [ 1 ] ) . toBeCloseTo ( 1 / 3 , 5 ) ;
180+ expect ( alongRed [ 2 ] ) . toBeCloseTo ( 1 , 5 ) ;
181+ expect ( alongRed [ 3 ] ) . toBeCloseTo ( 1 , 5 ) ;
182+ } ) ;
183+
184+ it . each ( [
185+ [ "a cube that already fits" , 64 , 4096 ] ,
186+ [ "a texture limit it could not read" , 65 , Number . NaN ] ,
187+ [ "a texture limit of zero" , 65 , 0 ] ,
188+ ] ) ( "returns the same cube for %s" , ( _label , size , limit ) => {
189+ const cube = identityCube ( size ) ;
190+
191+ expect ( fitCubeLutToTexture ( cube , limit ) ) . toBe ( cube ) ;
192+ } ) ;
193+ } ) ;
0 commit comments