-
Notifications
You must be signed in to change notification settings - Fork 1
/
w10n_XYZ-extractor-002.html
430 lines (354 loc) · 13.2 KB
/
w10n_XYZ-extractor-002.html
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: monospace;
}
#btnGo {
margin-top: 20px;
line-height: 25px;
}
</style>
<script type='text/javascript' src='https://code.jquery.com/jquery-3.5.1.min.js'></script>
</head>
</head>
<body>
<script>
// 0.0.2: First version properly creating output string in debug mode
// 0.0.1: First version with "float decoding" implemented
var PLY_HEADER = 'ply\n'+
'format ascii 1.0\n'+
'element vertex VVVVVV\n'+
'property float x\n'+
'property float y\n'+
'property float z\n'+
'element face 0\n'+
'property list uchar float vertex_indices\n'+
'end_header\n';
const FACTOR = 100;
const TOSKIP = 1;
const START_MARKER = '<A HREF="';
const END_MARKER = '">';
const PROXY = "http://win98.altervista.org/space/exploration/myp.php?pass=miapass&mode=native&url=";
const SERVER_URL = "https://pds-imaging.jpl.nasa.gov";
var PAGE_URL = "/w10n/mer/spirit/mer2no_0xxx/data/sol1866/rdr/2n292022791xylb0ozf0006l0m1.img/0/raster/data[]?output=json"; // provare con THUMBNAIL (DEBUG)
var toolsList = [];
var toolsListTable = [];
function loadFile(file) {
console.log(1);
const reader = new FileReader();
reader.addEventListener('load', (event) => {
stringContents = event.target.result;
console.log("Loaded: ", stringContents.length);
fileData = processFile(stringContents);
// jsonData = JSON.parse(fileData);
console.log("Processing ended.");
});
reader.readAsBinaryString(file);
console.log(2);
}
function saveFile(fileContent) {
myBlob = new Blob([fileContent], {type: "text/plain"});
var url = window.URL.createObjectURL(myBlob);
var anchor = document.createElement("a");
anchor.href = url;
anchor.download = "demo.txt";
anchor.click();
window.URL.revokeObjectURL(url);
document.removeChild(anchor);
}
function handleEvent(e) {
log.textContent = `${e.type}: ${e.loaded} bytes transferred\n` + log.textContent;
}
function addListeners(xhr) {
xhr.addEventListener('loadstart', handleEvent);
xhr.addEventListener('load', handleEvent);
xhr.addEventListener('loadend', handleEvent);
xhr.addEventListener('progress', handleEvent);
xhr.addEventListener('error', handleEvent);
xhr.addEventListener('abort', handleEvent);
console.log("Ok.");
}
function writeCanvas(x,y,r,g,b) {
//var x = Math.floor(Math.random() * canvasWidth);
//var y = Math.floor(Math.random() * canvasHeight);
//var r = Math.floor(Math.random() * 256);
//var g = Math.floor(Math.random() * 256);
//var b = Math.floor(Math.random() * 256);
// ctx.fillStyle = 'rgb(r,g,b)';
// ctx.fillRect(x,y,x+1,y+1);
var off = (y * id.width + x) * 4;
pixels[off] = r;
pixels[off + 1] = g;
pixels[off + 2] = b;
pixels[off + 3] = 255;
ctx.putImageData(id, 0, 0);
}
httpRequest = (PROXY, rawurl, method = 'GET', type="XML") => {
url = PROXY + rawurl;
console.log(">>>FILE:",url);
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
addListeners(xhr);
xhr.open(method, url);
xhr.onload = () => {
if (xhr.status === 200) {
console.log("OK!");
rawResponse = xhr.responseText;
try {
jsonResponse = JSON.parse(rawResponse);
} catch (e) {
console.log("JSON error:" , e);
console.log(rawResponse);
}
console.log(jsonResponse);
processData(jsonResponse);
} else {
console.log("ERROR!");
reject(new Error(xhr.responseText));
}
};
xhr.progress = (a) => {
console.log("PROG:",a);
document.getElementById("status").innerHTML = a;
};
xhr.send();
});
}
function processFile(fileContents) {
console.log("Start processing...", fileContents.length);
testami = fileContents;
const IMAGE_MARKER = "/* IMAGE DATA ELEMENTS */";
const START_OBJECT_MARKER = "OBJECT";
const END_OBJECT_MARKER = "END_OBJECT";
const BYTE_LENGTH = 8;
/////// Read PDS label data:
recordBytesLineStartPos = fileContents.indexOf("RECORD_BYTES");
recordBytesLineEndPos = fileContents.indexOf("\n",recordBytesLineStartPos);
recordBytesLine = fileContents.substr(recordBytesLineStartPos, recordBytesLineEndPos - recordBytesLineStartPos + 1);
recordBytesArr = recordBytesLine.split("=");
recordBytes = parseInt(recordBytesArr[1]);
console.log("recordBytes=",recordBytes);
fileRecordsLineStartPos = fileContents.indexOf("FILE_RECORDS");
fileRecordsLineEndPos = fileContents.indexOf("\n",fileRecordsLineStartPos);
fileRecordsLine = fileContents.substr(fileRecordsLineStartPos, fileRecordsLineEndPos - fileRecordsLineStartPos + 1);
fileRecordsArr = fileRecordsLine.split("=");
fileRecords = parseInt(fileRecordsArr[1]);
console.log("fileRecords=",fileRecords);
labelRecordsLineStartPos = fileContents.indexOf("LABEL_RECORDS");
labelRecordsLineEndPos = fileContents.indexOf("\n",labelRecordsLineStartPos);
labelRecordsLine = fileContents.substr(labelRecordsLineStartPos, labelRecordsLineEndPos - labelRecordsLineStartPos + 1);
labelRecordsArr = labelRecordsLine.split("=");
labelRecords = parseInt(labelRecordsArr[1]);
console.log("labelRecords=",labelRecords);
PDSlabelLength = recordBytes * labelRecords;
PDSlabel = fileContents.substr(0,PDSlabelLength);
console.log("PDS label:", PDSlabelLength, PDSlabel);
////// Read VICAR label data:
vicarLabelLengthLineStartPos = fileContents.indexOf("LBLSIZE");
vicarLabelLengthLineEndPos = fileContents.indexOf("\n",vicarLabelLengthLineStartPos);
vicarLabelLengthLine = fileContents.substr(vicarLabelLengthLineStartPos, vicarLabelLengthLineEndPos - vicarLabelLengthLineStartPos + 1);
vicarLabelLengthArr = vicarLabelLengthLine.split("=");
vicarLabelLength = parseInt(vicarLabelLengthArr[1]);
vicarLabel = fileContents.substr(PDSlabelLength, vicarLabelLength).replace('\x00',"");
vicarLabel=vicarLabel.split(" ").join("=========");
vicarLabel=vicarLabel.split(" ").join("---");
vicarLabel=vicarLabel.split(" ").join("\r\n");
console.log("vicarLabel:", vicarLabelLength, vicarLabel);
totalLabelLength = PDSlabelLength + vicarLabelLength;
onlyData = fileContents.substr(totalLabelLength , fileContents.length - totalLabelLength + 1);
console.log("onlyData length=", onlyData.length, onlyData.length/3);
///// Read image/band metadata:
imageDataStartPos = fileContents.indexOf(IMAGE_MARKER);
imageDataStartPos = fileContents.indexOf(START_OBJECT_MARKER,imageDataStartPos);
imageDataEndPos = fileContents.indexOf(END_OBJECT_MARKER,imageDataStartPos);
imageDataLabel = fileContents.substr(imageDataStartPos, imageDataEndPos - imageDataStartPos);
imageDataLabel = imageDataLabel.split(" ").join("");
imageDataLabelArr = imageDataLabel.split("\r\n");
imageDataLabelObj = {}
imageDataLabelArr.forEach((elem) => {
var pair = elem.split("=");
imageDataLabelObj[pair[0]] = pair[1];
});
imageLines = imageDataLabelObj["LINES"];
imageSamplesPerLine = imageDataLabelObj["LINE_SAMPLES"];
bytesPerSample = imageDataLabelObj["SAMPLE_BITS"]/BYTE_LENGTH;
lineLength = imageSamplesPerLine * bytesPerSample;
bandsNum = imageDataLabelObj["BANDS"]
bandLength = imageLines * lineLength;
imageLength = bandLength * bandsNum;
console.log("PDSlabelLength", "vicarLabelLength", "totalLabelLength");
console.log(PDSlabelLength, vicarLabelLength, totalLabelLength);
console.log(PDSlabelLength.toString(16), vicarLabelLength.toString(16), totalLabelLength.toString(16));
console.log("imageLength",imageLength, "0x"+ imageLength.toString(16));
console.log("bandLength",bandLength, "0x"+ bandLength.toString(16));
console.log("file length",fileContents.length, "0x"+ fileContents.length.toString(16));
console.log("imageLines",imageLines);
console.log("imageSamplesPerLine",imageSamplesPerLine);
console.log("bytesPerSample",bytesPerSample);
console.log("lineLength",lineLength);
console.log("bandsNum",bandsNum);
console.log("fileContents:", fileContents.length, "0x"+ fileContents.length.toString(16));
imageTable = [null, null, null]; // Source data (bytes)
bandArray = [null, null, null]; // Procesed data (floats)
for (var band = 0; band < bandsNum*1; band++) {
imageTable[band] = fileContents.slice(totalLabelLength + band * bandLength, totalLabelLength + bandLength + band * bandLength);
console.log( "imageTable[" + band + "]:" , imageTable[band].length, "0x"+ imageTable[band].length.toString(16));
bandArray[band] = [];
for (let i = 0; i < imageTable[band].length; i += lineLength) {
const fileLine = Array.from(imageTable[band].slice(i, i + lineLength));
floatArray = [];
for (let i = 0; i < fileLine.length; i += bytesPerSample) {
const floatGroup = fileLine.slice(i, i + bytesPerSample);
floatArray.push(floatGroup);
}
bandArray[band].push(floatArray);
}
console.log(bandArray[band]);
}
totalStr = "";
for (var row = 14 /* 0 */; row < 16 /*imageLines DEBUG */; row++) {
rowStr = "[";
for (var col = 0 ; col < imageSamplesPerLine; col++) {
element = "[";
for (var band = 0; band < bandsNum*1; band++) {
element += arrayToFloat(bandArray[band][row][col]) + ",";
}
element = element.substr(0,element.length-1) + "],"; // remove final comma and add "]"
rowStr += element;
}
rowStr = rowStr.substr(0,rowStr.length-1) + "],"; // remove final comma and add "]"
totalStr += rowStr;
}
console.log(totalStr);
return; // debug
}
function arrayToFloat(data) {
// Create a buffer
buf = new ArrayBuffer(4);
// Create a data view of it
view = new DataView(buf);
// set bytes
data.forEach(function (b, i) {
view.setUint8(i, b.charCodeAt());
});
// Read the bits as a float; note that by doing this, we're implicitly
// converting it from a 32-bit float into JavaScript's native 64-bit double
num = view.getFloat32(0);
// Done
if (num*1 === 0){
return ("0.0");
} else {
return ((num*1).toFixed(6));
}
}
function stringToArray(str) {
arr = [];
for (var i=0; i<str.length; i++) {
arr.push(str[i].charCodeAt())
}
console.log("STR ", str , "= ARR ", arr);
}
function testDebug() {
console.log(arrayToFloat(bandArray[15][32]));
}
function processData(obj) {
rows=obj.data;
rowsNum = rows.length;
cols = rows[0];
colsNum = cols.length;
bands = cols[0];
bandsNum = bands.length;
console.log(rowsNum,colsNum,bandsNum);
if (bandsNum) {
console.log("Rows x Columns =",rowsNum + "x" + colsNum + ", Bands: ", bandsNum);
rowsCount = 0;
pixelsCount = 0;
finalText = "";
rows.forEach( (row) => {
console.log("RIGA ",rowsCount);
cols = row
cols.forEach( (col) => {
console.log("COLONNA ",pixelsCount);
if ( (col[0] != 0) || (col[1] != 0) || (col[2] != 0)) {
console.log("" + col[0] + "," + col[1] + "," + col[2]);
finalText += "" + col[0]*FACTOR + " " + col[1]*FACTOR + " " + col[2]*FACTOR + "\n";
pixelsCount++;
document.getElementById("status").innerHTML = (100*(rowsCount / rowsNum)).toString();
}
});
rowsCount++;
});
console.log("Pixels:",pixelsCount);
PLY_FINAL_HEADER = PLY_HEADER.replace("VVVVVV",pixelsCount)
output.textContent = "Ready for download...";
saveFile(PLY_FINAL_HEADER + finalText);
} else {
// Single band file, create depthmap image
rowsCount = 0;
skipper = 0;
rows.forEach( (row) => {
values = row
valCount = 0;
values.forEach( (value) => {
skipper++;
if (skipper === TOSKIP) {
x = valCount;
y = rowsCount;
writeCanvas(x,y,value,value,value);
valCount++;
skipper = 0;
// console.log("Skipped at ", valCount);
} else {
valCount++;
}
});
rowsCount++;
});
}
}
function resolve(a) {
console.log("OK",a);
}
function reject(a) {
console.log("ERROR",a);
}
$(document).ready(function () {
log = document.querySelector('.event-log');
canvas = document.getElementById('depthmap');
ctx = canvas.getContext('2d');
canvasWidth = canvas.width;
canvasHeight = canvas.height;
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
id = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
pixels = id.data;
const fileSelector = document.getElementById('file-selector');
fileSelector.addEventListener('change', (event) => loadFile(event.target.files[0]));
console.log("Ready.");
document.getElementById("status").innerHTML = "READY.";
$("#btnGo").click(function () {
var MY_URL_1=SERVER_URL + PAGE_URL;
var url_1 = PROXY + encodeURIComponent(MY_URL_1);
console.log("1");
httpRequest(PROXY, MY_URL_1,"GET","list");
console.log("2");
});
});
</script>
</body>
</html>
<body>
<div>
<textarea id="output" cols="55" rows="15"></textarea>
</div>
<button id="btnGo">Go</button><br>
Status: <span id="status" name="status">-</span><br>
<br>
<input type="file" id="file-selector">
<button id="btnLoad" name="btnLoad" onclick="loadFile()">Load</button>
<br>
<textarea readonly class="event-log" cols=150 rows=3></textarea><br>
<canvas id="depthmap" name="depthmap" width = "1024" height="1024"></canvas><br>
</body>
</html>