forked from cameramanben/LUTCalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlut-3dl.js
378 lines (378 loc) · 9.23 KB
/
lut-3dl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* lut-3dl.js
* .3dl LUT building / parsing for the LUTCalc Web App.
* 3rd June 2015
*
* LUTCalc generates 1D and 3D Lookup Tables (LUTs) for video cameras that shoot log gammas,
* principally the Sony CineAlta line.
*
* By Ben Turley, http://turley.tv
* First License: GPLv2
* Github: https://github.com/cameramanben/LUTCalc
*/
function threedlLUT(messages, isLE, flavour) {
this.messages = messages;
this.isLE = isLE;
this.flavour = flavour;
}
threedlLUT.prototype.build = function(buff, fileName, ext) {
var info = {};
this.messages.getInfo(info);
var lut = new Float64Array(buff);
var max = lut.length;
var d = '';
max = info.dimension;
var mult = (Math.pow(2,info.inBits)-1)/(max-1);
for (var j=0; j<max; j++) {
d += Math.ceil(j*mult) + ' ';
}
d = d.slice(0,d.length - 1);
d += "\n";
var t;
mult = Math.pow(2,info.outBits)-1;
for (var r=0; r<max; r++) {
for (var g=0; g<max; g++) {
for (var b=0; b<max; b++) {
t = (r + (g*max) + (b*max*max))*3;
if (lut[ t ] > 0) {
d += Math.round(lut[ t ]*mult).toString() + ' ';
} else {
d += '0 ';
}
if (lut[t+1] > 0) {
d += Math.round(lut[t+1]*mult).toString() + ' ';
} else {
d += '0 ';
}
if (lut[t+2] > 0) {
d += Math.round(lut[t+2]*mult).toString() + "\n";
} else {
d += '0' + "\n";
}
}
}
}
switch (this.flavour) {
case 1: // Flame
return {
lut: this.header(info) + d,
fileName: fileName,
ext: ext
};
case 2: // Lustre
return {
lut: this.header(info) + d + this.footer(info),
fileName: fileName,
ext: ext
};
case 3: // Kodak
return {
lut: this.header(info) + d,
fileName: fileName,
ext: ext
};
}
};
threedlLUT.prototype.header = function(info) {
var out = '';
var date = new Date();
out += '# Created with LUTCalc ' + info.version + ' by Ben Turley ' + info.date + ' #' + "\n";
out += '# IDENTIFICATION: LUTCalc - 3DLUT' + "\n";
out += '# CREATOR: LUTCalc ' + info.version + "\n";
out += '# USER: LUTCalc' + "\n";
out += '# DATA: ' +
date.getUTCFullYear() + '-' +
this.addZero(date.getUTCMonth()) + '-' +
this.addZero(date.getUTCDate()) + 'T' +
this.addZero(date.getUTCHours()) + ':' +
this.addZero(date.getUTCMinutes()) + ':' +
this.addZero(date.getUTCSeconds()) + ' #' + "\n";
out += '# NUMBER OF COLUMNS: 3' + "\n";
out += '# NUMBER OF ROWS: ' + Math.pow(info.dimension,3).toString() + "\n";
out += '# NUMBER OF NODES: ' + info.dimension.toString() + "\n";
out += '# INPUT RANGE: ' + info.inBits.toString() + "\n";
out += '# OUTPUT RANGE: ' + info.outBits.toString() + "\n";
out += '# TITLE : ' + info.name + "\n";
out += '# DESCRIPTION : ';
if (info.nul) {
out += ' Null LUT';
} else {
if (info.mlut) {
out += 'MLUT';
}
if (info.doFC) {
out += '*** FALSE COLOUR - DO NOT BAKE IN *** ';
}
if (this.doHG) {
out += info.inGammaName + '/' + info.inGamutName + ' -> ' + info.outGammaName + '/' + info.outGamutName + '(' + info.hgGamutName + ' in the highlights)';
} else {
out += info.inGammaName + '/' + info.inGamutName + ' -> ' + info.outGammaName + '/' + info.outGamutName;
}
out += ', Stop Shift ' + info.cineEI.toFixed(2).toString();
out += ', Black Level ' + info.blackLevel + '% IRE';
if (info.legalIn) {
out += ', Legal Input -> ';
} else {
out += ', Data Input -> ';
}
if (info.legalOut) {
out += 'Legal Output';
} else {
out += 'Data Output';
}
}
out += "\n";
if (this.flavour === 2) {
out += '3DMESH' + "\n";
out += 'Mesh ';
switch (info.dimension) {
case 9: out += '3 ';
break;
case 17: out += '4 ';
break;
case 33: out += '5 ';
break;
case 65: out += '6 ';
break;
case 129: out += '7 ';
break;
}
out += info.outBits.toString() + "\n";
}
return out;
};
threedlLUT.prototype.addZero = function(i) {
if (i < 10) {
i = '0' + i;
}
return i;
};
threedlLUT.prototype.footer = function(info) {
var out = '';
out += 'LUT8' + "\n";
out += 'gamma 1.0' + "\n";
return out;
};
threedlLUT.prototype.parse = function(title, text, lut) {
var size = false;
var minimum = [0,0,0];
var maximum = [1,1,1];
var maxIn = 0;
var setMaxIn = false;
var maxOut = false;
var mOut = 0;
var format = 'threedl1';
var mesh = false;
var outBits = false;
var shaper;
var max = text.length;
if (max === 0) {
return false;
}
var i;
for (i=0; i<max; i++) {
var line = text[i].trim();
var lower = line.toLowerCase();
var j = line.charAt(0);
if ((!isNaN(parseFloat(j)) && isFinite(j)) || j === '-') {
// The first line with just numbers will be the shaper
var shape = line.trim().split(/\s+/g);
var m = shape.length;
shaper = new Float64Array(m);
for (var l=0; l<m; l++) {
shaper[l] = parseFloat(shape[l]);
if (!setMaxIn && shaper[l] > maxIn) {
maxIn = shaper[l];
}
if (isNaN(shaper[l])) {
return false;
}
}
i++;
break;
} else if (lower.search('3dmesh') >= 0) {
// 3DMESH line distinguishes two types
mesh = true;
format = 'threedl2';
} else if (lower.search('mesh') >= 0) {
mesh = true;
format = 'threedl2';
var bits = line.substr(lower.search('mesh') + 4).trim().split(/\s+/g);
if (!isNaN(bits[0]) && !isNaN(bits[1])) {
size = Math.pow(2,parseInt(bits[0]))+1;
outBits = parseInt(bits[1]);
maxOut = Math.pow(2,outBits)-1;
}
} else if (lower.search('number of rows:') >= 0) {
var val = parseFloat(line.substr(lower.search('number of rows:') + 15));
if (!isNaN(val)) {
size = Math.round(Math.pow(val, 1/3));
}
} else if (lower.search('number of nodes:') >= 0) {
var val = parseInt(line.substr(lower.search('number of nodes:') + 16));
if (!isNaN(val)) {
size = val;
}
} else if (lower.search('input range:') >= 0) {
var val = parseInt(line.substr(lower.search('input range:') + 12).trim());
if (!isNaN(val)) {
maxIn = Math.pow(2,val)-1;
}
} else if (lower.search('output range:') >= 0) {
var val = parseInt(line.substr(lower.search('output range:') + 13).trim());
if (!isNaN(val)) {
outBits = val;
maxOut = Math.pow(2,outBits)-1;
}
} else if (lower.search('title:') >= 0) {
title = line.substr(lower.search('title:') + 6).trim();
} else if (lower.search('gamma') >= 0) {
// Unused at the moment
} else if (lower.search('lut8') >= 0) {
// Unused at the moment
}
}
if (!size) {
// 3D LUT dimension does not need to match shaper, without 'Mesh', must be gauged from the number of entries. Yuck!
size = 0;
for (var k=i; k<max; k++) {
var line = text[k].trim();
var j = line.charAt(0);
if ((!isNaN(parseFloat(j)) && isFinite(j)) || j === '-') {
size++;
}
}
size = Math.round(Math.pow(size, 1/3));
}
if (size > 0) {
var spline = false;
var doShaper = false;
var m = shaper.length;
if (!setMaxIn) {
if (maxIn < 511) {
maxIn = 255;
} else if (maxIn < 2047) {
maxIn = 1023;
} else if (maxIn < 8191) {
maxIn = 4095;
} else if (maxIn < 32767) {
maxIn = 16383;
} else {
maxIn = 65535;
}
}
for (var j=0; j<m; j++) {
if (Math.round(shaper[j]) !== Math.ceil(maxIn*j/(m-1))) {
doShaper = true;
}
}
if (doShaper) {
for (var j=0; j<m; j++) {
shaper[j] /= maxIn;
}
// var lutSpline = new LUTSpline(shaper.buffer);
// spline = new Float64Array(lutSpline.getReverse());
spline = shaper;
}
var arraySize = size*size*size;
var R = new Float64Array(arraySize);
var G = new Float64Array(arraySize);
var B = new Float64Array(arraySize);
var s=0;
var r=0;
var g=0;
var b=0;
var t;
for (var k=i; k<max; k++) {
var line = text[k].trim();
var j = line.charAt(0);
if ((!isNaN(parseFloat(j)) && isFinite(j)) || j === '-') {
var vals = line.split(/\s+/g);
if (!isNaN(vals[0]) && !isNaN(vals[1]) && !isNaN(vals[2])) {
t = r + (g*size) + (b*size*size);
R[t] = parseFloat(vals[0]);
G[t] = parseFloat(vals[1]);
B[t] = parseFloat(vals[2]);
if (maxOut) {
R[t] /= maxOut;
G[t] /= maxOut;
B[t] /= maxOut;
} else {
if (R[s] > mOut) {
mOut = R[s];
}
if (G[s] > mOut) {
mOut = G[s];
}
if (B[s] > mOut) {
mOut = B[s];
}
}
b++;
if (b === size) {
b=0;
g++;
}
if (g === size) {
g=0;
r++;
}
s++;
}
}
/*
else if (lower.search('gamma') >= 0) {
// Unused at the moment
} else if (lower.search('lut8') >= 0) {
// Unused at the moment
}
*/
}
if (s === arraySize) {
if (!maxOut) {
if (mOut < 511) {
maxOut = 255;
} else if (mOut < 2047) {
maxOut = 1023;
} else if (mOut < 8191) {
maxOut = 4095;
} else if (mOut < 32767) {
maxOut = 16383;
} else {
maxOut = 65535;
}
for (var j=0; j<arraySize; j++) {
R[j] /= maxOut;
G[j] /= maxOut;
B[j] /= maxOut;
}
}
if (doShaper) {
lut.setDetails({
title: title,
format: format,
dims: 3,
s: size,
min: minimum,
max: maximum,
spline: spline.buffer,
C: [R.buffer,G.buffer,B.buffer]
});
} else {
lut.setDetails({
title: title,
format: format,
dims: 3,
s: size,
min: minimum,
max: maximum,
C: [R.buffer,G.buffer,B.buffer]
});
}
return true;
} else {
return false;
}
} else {
return false;
}
};