[b86468]: / v3 / js / libs / utilities.js

Download this file

157 lines (98 with data), 3.0 kB

  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
/*jslint browser: true, node: true */
/*global require, module */
"use strict";
/*** Imports ***/
var nifti = nifti || {};
nifti.Utils = nifti.Utils || {};
/*** Static Pseudo-constants ***/
nifti.Utils.crcTable = null;
nifti.Utils.GUNZIP_MAGIC_COOKIE1 = 31;
nifti.Utils.GUNZIP_MAGIC_COOKIE2 = 139;
/*** Static methods ***/
nifti.Utils.getStringAt = function (data, start, end) {
var str = "", ctr, ch;
for (ctr = start; ctr < end; ctr += 1) {
ch = data.getUint8(ctr);
if (ch !== 0) {
str += String.fromCharCode(ch);
}
}
return str;
};
nifti.Utils.getByteAt = function (data, start) {
return data.getInt8(start);
};
nifti.Utils.getShortAt = function (data, start, littleEndian) {
return data.getInt16(start, littleEndian);
};
nifti.Utils.getIntAt = function (data, start, littleEndian) {
return data.getInt32(start, littleEndian);
};
nifti.Utils.getFloatAt = function (data, start, littleEndian) {
return data.getFloat32(start, littleEndian);
};
nifti.Utils.getDoubleAt = function (data, start, littleEndian) {
return data.getFloat64(start, littleEndian);
};
nifti.Utils.getLongAt = function (data, start, littleEndian) {
var ctr, array = [], value = 0;
for (ctr = 0; ctr < 8; ctr += 1) {
array[ctr] = nifti.Utils.getByteAt(data, start + ctr, littleEndian);
}
for (ctr = array.length - 1; ctr >= 0; ctr--) {
value = (value * 256) + array[ctr];
}
return value;
};
nifti.Utils.toArrayBuffer = function (buffer) {
var ab, view, i;
ab = new ArrayBuffer(buffer.length);
view = new Uint8Array(ab);
for (i = 0; i < buffer.length; i += 1) {
view[i] = buffer[i];
}
return ab;
};
nifti.Utils.isString = function (obj) {
return (typeof obj === "string" || obj instanceof String);
};
nifti.Utils.formatNumber = function (num, shortFormat) {
var val = 0;
if (nifti.Utils.isString(num)) {
val = Number(num);
} else {
val = num;
}
if (shortFormat) {
val = val.toPrecision(5);
} else {
val = val.toPrecision(7);
}
return parseFloat(val);
};
// http://stackoverflow.com/questions/18638900/javascript-crc32
nifti.Utils.makeCRCTable = function(){
var c;
var crcTable = [];
for(var n =0; n < 256; n++){
c = n;
for(var k =0; k < 8; k++){
c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
}
crcTable[n] = c;
}
return crcTable;
};
nifti.Utils.crc32 = function(dataView) {
var crcTable = nifti.Utils.crcTable || (nifti.Utils.crcTable = nifti.Utils.makeCRCTable());
var crc = 0 ^ (-1);
for (var i = 0; i < dataView.byteLength; i++ ) {
crc = (crc >>> 8) ^ crcTable[(crc ^ dataView.getUint8(i)) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
/*** Exports ***/
var moduleType = typeof module;
if ((moduleType !== 'undefined') && module.exports) {
module.exports = nifti.Utils;
}