* Parses the given data. * * @param {String} input - The LUT data. * @return {LookupTexture} The lookup texture. * @throws {Error} Fails if the data is invalid.
(input)
| 90 | */ |
| 91 | |
| 92 | parse(input) { |
| 93 | |
| 94 | const regExpGridInfo = /^[\d ]+$/m; |
| 95 | const regExpDataPoints = /^([\d.e+-]+) +([\d.e+-]+) +([\d.e+-]+) *$/gm; |
| 96 | |
| 97 | // The first line describes the positions of values on the LUT grid. |
| 98 | let result = regExpGridInfo.exec(input); |
| 99 | |
| 100 | if(result === null) { |
| 101 | |
| 102 | throw new Error("Missing grid information"); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | const gridLines = result[0].trim().split(/\s+/g).map((n) => Number(n)); |
| 107 | const gridStep = gridLines[1] - gridLines[0]; |
| 108 | const size = gridLines.length; |
| 109 | const sizeSq = size ** 2; |
| 110 | |
| 111 | for(let i = 1, l = gridLines.length; i < l; ++i) { |
| 112 | |
| 113 | if(gridStep !== (gridLines[i] - gridLines[i - 1])) { |
| 114 | |
| 115 | throw new Error("Inconsistent grid size"); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | |
| 121 | const data = new Float32Array(size ** 3 * 4); |
| 122 | let maxValue = 0.0; |
| 123 | let index = 0; |
| 124 | |
| 125 | while((result = regExpDataPoints.exec(input)) !== null) { |
| 126 | |
| 127 | const r = Number(result[1]); |
| 128 | const g = Number(result[2]); |
| 129 | const b = Number(result[3]); |
| 130 | |
| 131 | maxValue = Math.max(maxValue, r, g, b); |
| 132 | |
| 133 | const bLayer = index % size; |
| 134 | const gLayer = Math.floor(index / size) % size; |
| 135 | const rLayer = Math.floor(index / (sizeSq)) % size; |
| 136 | |
| 137 | // b grows first, then g, then r. |
| 138 | const d4 = (bLayer * sizeSq + gLayer * size + rLayer) * 4; |
| 139 | data[d4 + 0] = r; |
| 140 | data[d4 + 1] = g; |
| 141 | data[d4 + 2] = b; |
| 142 | data[d4 + 3] = 1.0; |
| 143 | |
| 144 | ++index; |
| 145 | |
| 146 | } |
| 147 | |
| 148 | // Determine the bit depth and scale the values to [0.0, 1.0]. |
| 149 | const bits = Math.ceil(Math.log2(maxValue)); |