forked from cameramanben/LUTCalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlut-vlt.js
140 lines (140 loc) · 4.2 KB
/
lut-vlt.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
/* lut-vlt.js
* Panasonic Varicam .vlt 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 vltLUT(messages, isLE) {
this.messages = messages;
this.isLE = isLE;
}
vltLUT.prototype.build = function(buff, fileName, ext) {
var lut = new Float64Array(buff);
var max = lut.length;
var d = '';
for (var j=0; j<max; j += 3) {
d += Math.min(4095,Math.max(0,Math.round(lut[ j ]*4095))).toString() + ' ' +
Math.min(4095,Math.max(0,Math.round(lut[j+1]*4095))).toString() + ' ' +
Math.min(4095,Math.max(0,Math.round(lut[j+2]*4095))).toString() + "\n";
}
return {
lut: this.header() + d,
fileName: fileName,
ext: ext
};
};
vltLUT.prototype.header = function() {
var info = {};
this.messages.getInfo(info);
var out = '# panasonic vlt file version 1.0' + "\n";
out += '# source vlt file ""' + "\n";
out += 'LUT_3D_SIZE ' + info.dimension.toString() + "\n\n";
return out;
};
vltLUT.prototype.parse = function(title, text, lut) {
var dimensions = false;
var size = false;
var minimum = [0,0,0];
var maximum = [1,1,1];
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 === '-') {
break;
} else if (lower.search('title') >= 0) {
title = line.substr(lower.search('title') + 5).trim().replace(/"/g, '');
} else if (lower.search('lut_3d_size') >= 0) {
var dim = line.substr(lower.search('lut_3d_size') + 11).trim();
if (!isNaN(dim)) {
dimensions = 3;
size = parseInt(dim);
}
} else if (lower.search('lut_1d_size') >= 0) {
var dim = line.substr(parseInt(lower.search('lut_1d_size')) + 11).trim();
if (!isNaN(dim)) {
dimensions = 1;
size = parseInt(dim);
}
} else if (lower.search('domain_min') >= 0) {
var dom = line.substr(parseInt(lower.search('domain_min')) + 10).trim().split(/\s+/g);
if (!isNaN(dom[0]) && !isNaN(dom[1]) && !isNaN(dom[2])) {
minimum[0] = parseInt(dom[0]);
minimum[1] = parseInt(dom[1]);
minimum[2] = parseInt(dom[2]);
}
} else if (lower.search('domain_max') >= 0) {
var dom = line.substr(parseInt(lower.search('domain_max')) + 10).trim().split(/\s+/g);
if (!isNaN(dom[0]) && !isNaN(dom[1]) && !isNaN(dom[2])) {
maximum[0] = parseInt(dom[0]);
maximum[1] = parseInt(dom[1]);
maximum[2] = parseInt(dom[2]);
}
} else if (lower.search('lut_3d_input_range') >= 0) {
var ran = line.substr(parseInt(lower.search('lut_3d_input_range')) + 18).trim().split(/\s+/g);
if (!isNaN(ran[0]) && !isNaN(ran[1])) {
minimum[0] = parseInt(ran[0]);
minimum[1] = minimum[0];
minimum[2] = minimum[0];
maximum[0] = parseInt(ran[1]);
maximum[1] = maximum[0];
maximum[2] = maximum[0];
}
} else if (lower.search('lut_1d_input_range') >= 0) {
var ran = line.substr(parseInt(lower.search('lut_1d_input_range')) + 18).trim().split(/\s+/g);
if (!isNaN(ran[0]) && !isNaN(ran[1])) {
minimum[0] = parseInt(ran[0]);
minimum[1] = minimum[0];
minimum[2] = minimum[0];
maximum[0] = parseInt(ran[1]);
maximum[1] = maximum[0];
maximum[2] = maximum[0];
}
}
}
if (dimensions && size) {
var arraySize = size;
if (dimensions === 3) {
arraySize = size*size*size;
}
var R = new Float64Array(arraySize);
var G = new Float64Array(arraySize);
var B = new Float64Array(arraySize);
var s=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 === '-') {
var vals = line.split(/\s+/g);
if (!isNaN(vals[0]) && !isNaN(vals[1]) && !isNaN(vals[2])) {
R[s] = parseFloat(vals[0])/4095;
G[s] = parseFloat(vals[1])/4095;
B[s] = parseFloat(vals[2])/4095;
s++;
}
}
}
lut.setDetails({
title: title,
format: 'vlt',
dims: dimensions,
s: size,
min: minimum,
max: maximum,
C: [R.buffer,G.buffer,B.buffer]
});
return true;
} else {
return false;
}
};