-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSGILoaderBase.js
271 lines (192 loc) · 6 KB
/
SGILoaderBase.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// The spec:
// http://paulbourke.net/dataformats/sgirgb/
// http://paulbourke.net/dataformats/sgirgb/sgiversion.html
// https://www.fileformat.info/format/sgiimage/egff.htm
// reads a data view as a string up to max length, ending at the terminating character
function readUpTo( dataView, offset = 0, maxLength = Infinity ) {
let str = '';
let currOffset = offset;
while ( dataView.getUint8( currOffset ) !== 0 && currOffset - offset < maxLength ) {
str += String.fromCharCode( DataView.getUint8( currOffset ) );
currOffset ++;
}
return str;
}
/**
* @typedef {Object} SGIResult
*
* @param {String} name
* The image name embedded in the file.
*
* @param {Boolean} rle
* Whether the file is run length encoded or not.
*
* @param {Number} dimension
* The number of dimensions to the stored data.
*
* @param {Number} width
* The width of the sgi file in pixels.
*
* @param {Number} height
* The height of the sgi file in pixels.
*
* @param {Number} channels
* The number of color channels stored in the image.
*
* @param {Number} minValue
* The minimum channel value in the file.
*
* @param {Number} maxValue
* The maximum channel value in the file.
*
* @param {Number} bytesPerChannel
* The amount of bytes used to represent a single color channel.
*
* @param {Uint16Array|Uint8Array} data
* The SGI laid out in an array in row major order where each row has a stride
* of `width * channels * bytesPerChannel`.
*/
/** Class for loading Silicon Graphics Image files */
export class SGILoaderBase {
constructor() {
/**
* @member {Object}
* @description Fetch options for loading the file.
* @default { credentials: 'same-origin' }
*/
this.fetchOptions = { credentials: 'same-origin' };
}
/**
* Loads and parses the SGI file. The promise resolves with the returned
* data from the {@link #SGILoaderBase#parse parse} function.
* @param {String} url
* @returns {Promise<SGIResult>}
*/
load( url ) {
return fetch( url, this.fetchOptions )
.then( res => {
if ( ! res.ok ) {
throw new Error( `SGILoader: Failed to load file "${url}" with status ${res.status} : ${res.statusText}` );
}
return res.arrayBuffer();
} )
.then( buffer => this.parse( buffer ) );
}
/**
* Parses the contents of the given SGI contents and returns an object describing
* the telemetry.
* @param {ArrayBuffer|Uint8Array} buffer
* @returns {SGIResult}
*/
parse( buffer ) {
const HEADER_LENGTH = 512;
let dataView;
let uint8Buffer;
if ( buffer instanceof ArrayBuffer ) {
dataView = new DataView( buffer );
uint8Buffer = new Uint8Array( buffer );
} else {
dataView = new DataView( buffer.buffer, buffer.byteOffset, buffer.byteLength );
uint8Buffer = new Uint8Array( buffer.buffer, buffer.byteOffset, buffer.byteLength );
}
// read header
const magic = dataView.getUint16( 0, false );
if ( magic !== 474 ) {
throw new Error( `SGILoader : Magic bytes set to ${magic}. Expected 474.` );
}
const storage = dataView.getUint8( 2 );
const bytesPerChannel = dataView.getUint8( 3 );
const dimension = dataView.getUint16( 4, false );
const width = dataView.getUint16( 6, false );
const height = dataView.getUint16( 8, false );
const channels = dataView.getUint16( 10, false );
const minValue = dataView.getInt32( 12, false );
const maxValue = dataView.getInt32( 16, false );
// 4 0 bytes
const name = readUpTo( dataView, 20, 80 );
const colorMap = dataView.getInt32( 100, false );
if ( colorMap !== 0 ) {
throw new Error( `SGILoader : Obsolete colormap value ${colorMap} found.` );
}
// read image
const imageLength = width * height * channels * bytesPerChannel;
let data;
let source;
if ( bytesPerChannel === 2 ) {
data = new Uint16Array( imageLength / 2 );
source = new Uint16Array( uint8Buffer.buffer, uint8Buffer.byteOffset );
} else {
data = new Uint8Array( imageLength );
source = uint8Buffer;
}
// RLE
if ( storage === 1 ) {
if ( bytesPerChannel === 2 ) {
console.warn( 'SGILoader: RLE 2 bytes per channel files have not been tested.' );
}
const data8Buffer = new Uint8Array( data.buffer, data.byteOffset, data.byteLength );
const count = height * channels;
const startTableLength = count * 4;
for ( let c = 0; c < channels; c ++ ) {
for ( let r = 0; r < height; r ++ ) {
const tableRowIndex = c * height + r;
const start = dataView.getInt32( HEADER_LENGTH + tableRowIndex * 4, false );
const length = dataView.getInt32( HEADER_LENGTH + startTableLength + tableRowIndex * 4, false );
const end = start + length;
let targetOffset = r * width * channels * bytesPerChannel + c;
let offset = start;
while ( offset < end ) {
let color = source[ offset ];
offset ++;
let count = color & 0x7f; // bits 0-6
if ( count == 0 ) {
// end of the row
break;
} else if ( color & 0x80 ) {
// read upcoming characters
while ( count -- ) {
data8Buffer[ targetOffset ] = source[ offset ];
targetOffset += channels;
offset ++;
}
} else {
// repeat an existing value
color = source[ offset ];
offset ++;
while ( count -- ) {
data8Buffer[ targetOffset ] = color;
targetOffset += channels;
}
}
}
}
}
} else {
for ( let c = 0; c < channels; c ++ ) {
// the length of data for images before the current channel
const imageOffset = HEADER_LENGTH + width * height * c;
for ( let y = 0; y < height; y ++ ) {
const sourceOffset = imageOffset + y * width;
const targetOffset = width * channels * y;
for ( let x = 0; x < width; x ++ ) {
const sourceIndex = sourceOffset + x;
const targetIndex = targetOffset + x * channels + c;
data[ targetIndex ] = source[ sourceIndex ];
}
}
}
}
return {
name,
rle: storage === 1.0,
dimension,
width,
height,
channels,
minValue,
maxValue,
bytesPerChannel,
data,
};
}
}