-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryfilereader.js
59 lines (47 loc) · 1.57 KB
/
binaryfilereader.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
function BinaryFileReader(url) {
var req = new XMLHttpRequest();
// synchronous
req.open('GET', url, false);
// force parsing off
req.overrideMimeType('text/plain; charset=x-user-defined');
// send request
req.send();
if (req.readyState != 4 || (req.status != 200 && req.status != 0)) {
throw "Unable to load model!";
}
this.contents = req.responseText;
this.size = this.contents.length;
this.pointer = 0;
}
BinaryFileReader.prototype.ignore = function(num_bytes) {
this.pointer += num_bytes;
}
BinaryFileReader.prototype.readArray = function(num_bytes) {
var out = this.contents.slice(this.pointer, this.pointer + num_bytes);
this.pointer += num_bytes;
return out;
}
BinaryFileReader.prototype.readUInt8 = function() {
return this.contents.charCodeAt(this.pointer++) & 0xff;
}
BinaryFileReader.prototype.readUInt16 = function() {
return this.readUInt8() + (this.readUInt8() << 8);
}
BinaryFileReader.prototype.readUInt32 = function() {
return this.readUInt8() +
(this.readUInt8() << 8) +
(this.readUInt8() << 16) +
(this.readUInt8() << 24);
}
//TODO
BinaryFileReader.prototype.readInt8 = BinaryFileReader.prototype.readUInt8;
BinaryFileReader.prototype.readInt16 = BinaryFileReader.prototype.readUInt16;
BinaryFileReader.prototype.readInt32 = BinaryFileReader.prototype.readUInt32;
BinaryFileReader.prototype.readFloat32 = function() {
//TODO: NaN and infinities
var b = this.readUInt32();
var sign = b & 0x80000000 ? -1 : 1;
var expo = ((b & 0x7F800000) >> 23) - 127;
var mant = (b & 0x007FFFFF) | 0x00800000;
return sign * Math.pow(2, expo) * (mant / (1<<23));
}