forked from cameramanben/LUTCalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlutfile.js
252 lines (252 loc) · 8.19 KB
/
lutfile.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
/* lutfile.js
* File I/O handling object for the LUTCalc Web App.
* 7th October 2014
*
* 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 LUTFile(inputs) {
this.inputs = inputs;
this.filesaver = true;
try {
var isFileSaverSupported = !!new Blob();
} catch(e){
this.filesaver = false;
}
}
LUTFile.prototype.save = function(data, fileName, extension) {
if (this.inputs.isApp) { // From native app detection in lutcalc.js
return window.lutCalcApp.saveLUT(data, this.filename(fileName), extension);
} else if (this.inputs.isChromeApp) { // Use Chrome App fileSystem API to select destination
chrome.fileSystem.chooseEntry(
{
type: 'saveFile',
suggestedName: this.filename(fileName) + '.' + extension,
accepts: [{
extensions: [extension]
}],
acceptsAllTypes: false
},
function(writableFileEntry) {
writableFileEntry.createWriter(
function(writer) {
writer.onwriteend = function(e) {
notifyUser('LUTCalc',writableFileEntry.name + ' saved');
};
writer.write(new Blob([data],{type: 'text/plain;charset=UTF-8'}));
},
function() {
notifyUser('LUTCalc','File could not be saved');
}
);
}
);
} else if (this.filesaver) { // Detect FileSaver.js applicability for browsers other than Safari and older IE
saveAs(new Blob([data], {type: 'text/plain;charset=UTF-8'}), this.filename(fileName) + '.' + extension);
return true;
} else { // Fall back to opening LUT in a new tab for user to save with 'Save As...'
window.open("data:text/plain," + encodeURIComponent(data),"_blank");
return true;
}
};
LUTFile.prototype.saveBinary = function(data, fileName, extension) {
if (this.inputs.isApp) { // From native app detection in lutcalc.js
return window.lutCalcApp.saveBIN(data, this.filename(fileName), extension);
} else if (this.inputs.isChromeApp) { // Use Chrome App fileSystem API to select destination
chrome.fileSystem.chooseEntry(
{
type: 'saveFile',
suggestedName: this.filename(fileName) + '.' + extension,
accepts: [{
extensions: [extension]
}],
acceptsAllTypes: false
},
function(writableFileEntry) {
writableFileEntry.createWriter(
function(writer) {
writer.onwriteend = function(e) {
notifyUser('LUTCalc',writableFileEntry.name + ' saved');
};
writer.write(new Blob([data],{type: 'application/octet-stream'}));
},
function() {
notifyUser('LUTCalc','File could not be saved');
}
);
}
);
} else if (this.filesaver) { // Detect FileSaver.js applicability for browsers other than Safari and older IE
saveAs(new Blob([data], {type: 'application/octet-stream'}), this.filename(fileName) + '.' + extension);
return true;
} else {
console.log('Browser does not support file saving.');
return false;
}
};
LUTFile.prototype.loadLUTFromInput = function(fileInput, extensions, isTxt, destination, parentObject, next) {
if (this.inputs.isApp) {
window.lutCalcApp.loadLUT(extensions.toString(), destination, parentObject.p, next);
} else {
var file = fileInput.files[0];
var valid = false;
var dot = file.name.lastIndexOf('.');
var ext = '';
var isBin = false;
if (dot != -1) {
dot++;
ext = file.name.substr(dot).toLowerCase();
var max = extensions.length;
for (var i=0; i<max; i++) {
if (ext === extensions[i]) {
valid = true;
isBin = !isTxt[i];
break;
}
}
}
if (valid) {
if (window.File && window.FileReader && window.FileList && window.Blob) {
var reader = new FileReader();
var localDestination = this.inputs[destination];
localDestination.format = ext;
localDestination.title = file.name.substr(0,file.name.length-ext.length-1);
if (isBin) {
reader.onload = (function(theFile){
var theDestination = localDestination;
return function(e){
theDestination.isTxt = false;
theDestination.buff = e.target.result;
parentObject.followUp(next);
};
})(file);
reader.onerror = function(theFile) { return function() {
alert("Error reading file.");
inputBox.value = '';
}; }(file);
reader.readAsArrayBuffer(file);
} else {
reader.onload = (function(theFile){
var theDestination = localDestination;
return function(e){
theDestination.isTxt = true;
theDestination.text = e.target.result.split(/[\n\u0085\u2028\u2029]|\r\n?/);
parentObject.followUp(next);
};
})(file);
reader.onerror = function(theFile) { return function() {
alert("Error reading file.");
inputBox.value = '';
}; }(file);
reader.readAsText(file);
}
} else {
alert("Can't load LUT - your browser is not set to support Javascript File APIs.");
fileInput.value = '';
}
} else {
fileInput.value = '';
alert("LUTCalc does not understand this file.");
}
}
};
LUTFile.prototype.loadImgFromInput = function(fileInput, extensions, destination, parentObject, next) {
if (this.inputs.isApp) {
window.lutCalcApp.loadImg(extensions.toString(), destination, parentObject.p, next);
} else {
var file = fileInput.files[0];
var valid = false;
var dot = file.name.lastIndexOf('.');
var ext = '';
if (dot != -1) {
dot++;
ext = file.name.substr(dot).toLowerCase();
var max = extensions.length;
for (var i=0; i<max; i++) {
if (ext === extensions[i]) {
valid = true;
break;
}
}
}
if (valid) {
if (window.File && window.FileReader && window.FileList && window.Blob) {
var reader = new FileReader();
var localDestination = this.inputs[destination];
localDestination.format = ext;
localDestination.title = file.name.substr(0,file.name.length-ext.length-1);
reader.onload = (function(theFile){
var theDestination = localDestination;
return function(e){
theDestination.pic = new Image();
theDestination.pic.onload = function(e){
theDestination.w = theDestination.pic.width;
theDestination.h = theDestination.pic.height;
parentObject.followUp(next);
};
theDestination.pic.src = e.target.result;
};
})(file);
reader.onerror = function(theFile) { return function() {
alert("Error reading file.");
inputBox.value = '';
}; }(file);
reader.readAsDataURL(file);
} else {
alert("Can't load image - your browser is not set to support Javascript File APIs.");
fileInput.value = '';
}
} else {
fileInput.value = '';
alert("LUTCalc does not understand this image type.");
}
}
};
LUTFile.prototype.filename = function(filename) {
return filename.replace(/[^a-z0-9_\-\ ]/gi, '').replace(/[^a-z0-9_\-]/gi, '_');
};
// Functions available to native apps
function loadLUTFromApp(fileName, format, content, destination, parentIdx, next) {
lutInputs[destination].format = format;
if (format.toLowerCase() === 'labin') {
var max = content.length;
var data = new Uint8Array(max);
for (var j=0; j<max; j++) {
data[j] = content[j];
}
lutInputs[destination].title = fileName;
lutInputs[destination].buff = data.buffer;
lutInputs[destination].isTxt = false;
} else {
lutInputs[destination].title = fileName;
lutInputs[destination].text = content.split(/[\n\u0085\u2028\u2029]|\r\n?/);
lutInputs[destination].isTxt = true;
}
switch (parseInt(parentIdx)) {
case 5: lutGenerate.followUp(parseInt(next));
break;
case 10: lutTweaksBox.followUp(parseInt(parentIdx),parseInt(next));
break;
}
}
function loadImgFromApp(format, title, content, destination, parentIdx, next) {
var theDestination = lutInputs[destination];
var nextObject;
switch (parseInt(parentIdx)) {
case 8: nextObject = lutPreview;
break;
}
theDestination.title = title;
var max = content.length;
theDestination.imageData = new Float64Array(max);
var sample;
for (var j=0; j<max; j++) {
sample = parseFloat(content[ j ])/65535;
theDestination.imageData[ j ] = sample;
}
nextObject.followUp(parseInt(next));
}