-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimg-viewer-2d-mars.html
134 lines (87 loc) · 3.16 KB
/
img-viewer-2d-mars.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
<!doctype html>
<html lang=en>
<head>
<title>img-viewer-2d-mars.html</title>
<meta charset=utf-8 />
<meta name=viewport content='width=device-width,user-scalable=no,minimum-scale=1.0,maximum-scale=1.0' />
</head>
<body>
<script>
var path = '../heightmaps/'; // for developing etc
// var path = 'https://jaanga.github.io/mars/heightmaps/';
var fileName = path + 'megt90n000cb.img';
// var fileName = path + 'megr90n000cb.img';
var width = 1440; // 4P
var height = 720;
var xmlHttp;
var byteArray;
init();
function init() {
var css = document.head.appendChild( document.createElement( 'style' ) );
css.innerHTML = 'body { font: 12pt monospace; }';
var info = document.body.appendChild( document.createElement( 'div' ) );
info.innerHTML = '<h2><a href="" >' + document.title + '</a></h2>' +
'<p><a id=download href="" onclick=saveIt(); ><button>Save it</button></a></p>' +
'<div id=msg0 >' + fileName + '</div>' +
'<div id=msg1 ></div>' +
'<canvas id=canvas ></canvas>' +
'';
// canvas = document.createElement( 'canvas' );
// canvas = document.body.appendChild( document.createElement( 'canvas' ) );
canvas.width = width;
canvas.height = height;
canvas.style.cssText = 'border: 1px solid gray; ';
context = canvas.getContext( '2d' );
imageData = context.createImageData( canvas.width, canvas.height );
imageDataData = imageData.data;
requestIMGFile( fileName );
}
function requestIMGFile( fileName, startByte, finishByte ) {
xmlHttp = new XMLHttpRequest();
xmlHttp.crossOrigin = 'Anonymous';
xmlHttp.responseType = 'arraybuffer';
xmlHttp.open( 'GET', fileName, true );
xmlHttp.onreadystatechange = getData;
// xmlHttp.setRequestHeader('Range', 'bytes=' + startByte + '-' + finishByte );
xmlHttp.send( null );
}
function getData() {
if ( xmlHttp.readyState !== 4 ) { return; }
// byteArray = new Int16Array( xmlHttp.response );
byteArray = new Uint8Array( xmlHttp.response );
var index = 0;
elevations = [];
min = 1000000;
max = -1000000;
for ( var i = 0; i < byteArray.length; ) {
elevation = 256 * byteArray[ i++ ] + byteArray[ i++ ];
elevation = elevation < 32767 ? elevation : elevation - 65535;
elevation += 32767;
elevations.push( elevation );
imageDataData[ index++ ] = ( elevation & 0x0000ff );
imageDataData[ index++ ] = ( elevation & 0x00ff00 ) >> 8;
imageDataData[ index++ ] = ( elevation & 0xff0000 ) >> 16;
imageDataData[ index++ ] = 255;
}
context.putImageData( imageData, 0, 0 );
msg1.innerHTML = 'min: ' + min + ' max: ' + max;
/*
context.moveTo( 0, 360 );
context.lineTo( 1440, 360 );
context.stroke();
context.moveTo( 720, 0 );
context.lineTo( 720, 720 );
context.stroke();
*/
}
function saveIt() {
slash = navigator.platform === 'Win32' ? '\\' : '/';
name = fileName.substr( 1 + fileName.lastIndexOf( slash ) );
name = name.replace( /\.img|\.html|\.json|\.js|\.obj|\.stl/gi, '-' ) + width + 'x' + height + '.png';
var c2du = canvas.toDataURL();
download.download = name;
download.href = c2du;
}
</script>
</body>
</html>