forked from cameramanben/LUTCalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlut-spi1d.js
134 lines (134 loc) · 3.46 KB
/
lut-spi1d.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
/* lut-spi1d.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 spi1dLUT(messages, isLE) {
this.messages = messages;
this.isLE = isLE;
}
spi1dLUT.prototype.build = function(buff, fileName, ext) {
var lut = new Float64Array(buff);
var info = {};
this.messages.getInfo(info);
var max = lut.length;
var d = '{' + "\n";
for (var j=0; j<max; j += 3) {
d += "\t" + lut[ j ].toFixed(12).toString();
if (info.doASCCDL) {
d += ' ' + lut[j+1].toFixed(12).toString();
d += ' ' + lut[j+2].toFixed(12).toString();
}
d += "\n";
}
d += '}' + "\n";
return {
lut: this.header(info) + d,
fileName: fileName,
ext: ext
};
};
spi1dLUT.prototype.header = function(info) {
var out = '';
out += 'Version 1' + "\n";
out += 'From ' + info.scaleMin.toString() + ' ' + info.scaleMax.toString() + "\n";
out += 'Length ' + info.dimension.toString() + "\n";
if (info.doASCCDL) {
out += 'Components 3' + "\n";
} else {
out += 'Components 1' + "\n";
}
return out;
};
spi1dLUT.prototype.parse = function(title, text, lut) {
var dimensions = 1;
var channels = false;
var size = false;
var version = false;
var from = 0;
var to = 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();
if (lower.search('{') >= 0) {
break;
} else if (lower.search('version') >= 0) {
version = parseInt(line.substr(parseInt(lower.search('version')) + 7).trim());
} else if (lower.search('from') >= 0) {
var data = line.substr(parseInt(lower.search('from')) + 4).trim().split(/\s+/g);
from = parseFloat(data[0]);
to = parseFloat(data[1]);
} else if (lower.search('length') >= 0) {
size = parseInt(line.substr(parseInt(lower.search('length')) + 6).trim());
} else if (lower.search('components') >= 0) {
channels = parseInt(line.substr(parseInt(lower.search('components')) + 10).trim());
}
}
if (channels && size) {
var arraySize = size;
i++;
if (channels === 1) {
var L = new Float64Array(arraySize);
var s=0;
for (var k=i; k<max; k++) {
var line = text[k].trim();
if ((!isNaN(parseFloat(line)) && isFinite(line))) {
L[s] = parseFloat(line);
s++;
}
}
if (s === size) {
lut.setDetails({
title: title,
format: 'spi1d',
dims: 1,
s: size,
min: [from,from,from],
max: [to,to,to],
C: [L.buffer]
});
return true;
} else {
return false;
}
} else if (channels < 4) {
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 (channels === 2 && !isNaN(vals[0]) && !isNaN(vals[1])) {
R[s] = parseFloat(vals[0]);
G[s] = parseFloat(vals[1]);
B[s] = 0;
s++;
} else if (channels === 3 && !isNaN(vals[0]) && !isNaN(vals[1]) && !isNaN(vals[2])) {
R[s] = parseFloat(vals[0]);
G[s] = parseFloat(vals[1]);
B[s] = parseFloat(vals[2]);
s++;
}
}
}
} else {
return false;
}
} else {
return false;
}
};