-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
386 lines (342 loc) · 11.2 KB
/
Copy pathindex.js
File metadata and controls
386 lines (342 loc) · 11.2 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
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
/* libtga main */
const HEADER_SIZE = 18;
// image types: is bitfield-ish
const IMAGE_TYPE_NONE = 0|0;
const IMAGE_TYPE_COLORMAPPED = 1|0;
const IMAGE_TYPE_TRUECOLOR = 2|0;
const IMAGE_TYPE_GREYSCALE = 3|0;
// compression flag:
const IMAGE_RUNLENGTH_ENCODED = 0x8|0;
// color maps (documented on wikipedia; but not in the spec?):
/*const COLOR_MAP_NONE = 0|0;
const COLOR_MAP_EXISTS = 1|0;
const COLOR_MAP_TRUEVISION_START = 2|0;
const COLOR_MAP_TRUEVISION_END = 127|0;
const COLOR_MAP_DEV_START = 128|0;
const COLOR_MAP_DEV_END = 255|0;*/
// image descriptor constants:
const IMAGE_DESCRIPTOR_ATTRIBUTE_MASK = 0xf|0;
const IMAGE_DESCRIPTOR_ORIGIN_MASK = 0x30|0;
const IMAGE_DESCRIPTOR_INTERLEAVE_MASK = 0xc0|0;
// Origin values:
const IMAGE_ORIGIN_VERTICAL_MASK = 0x02|0;
const IMAGE_ORIGIN_HORIZONTAL_MASK = 0x01|0;
const IMAGE_ORIGIN_TOP = 0x02|0;
const IMAGE_ORIGIN_RIGHT = 0x01|0;
class TGA {
constructor(arraybuf) {
this.dataview = new DataView(arraybuf);
this.header = TGA.readHeader(this.dataview);
this.width = this.header.imageSpec.width;
this.height = this.header.imageSpec.height;
this.compressed = !!(this.header.imageType & IMAGE_RUNLENGTH_ENCODED);
this.imageId = TGA.readImageId(this.dataview, this.header);
this.colorMap = TGA.readColorMap(this.dataview, this.header);
this.imageData = TGA.readImage(this);
}
}
// add constant refs here:
TGA.HEADER_SIZE = HEADER_SIZE;
TGA.IMAGE_TYPE_NONE = IMAGE_TYPE_NONE;
TGA.IMAGE_TYPE_COLORMAPPED = IMAGE_TYPE_COLORMAPPED;
TGA.IMAGE_TYPE_TRUECOLOR = IMAGE_TYPE_TRUECOLOR;
TGA.IMAGE_TYPE_GREYSCALE = IMAGE_TYPE_GREYSCALE;
TGA.IMAGE_RUNLENGTH_ENCODED = IMAGE_RUNLENGTH_ENCODED;
// Utility functions don't really need to be in the prototype?
// Utility functions:
TGA.readHeader = function(dataview) {
var header = {
idLength: dataview.getUint8(0, true),
mapType: dataview.getUint8(1, true),
imageType: dataview.getUint8(2, true),
colorMapSpec: TGA.readColorMapSpec(dataview, 3),
imageSpec: TGA.readImageSpec(dataview, 8)
};
return header;
};
TGA.readColorMapSpec = function(dataview, offset) {
var bits = dataview.getUint8(offset+4, true);
var colorMapSpec = {
firstEntry: dataview.getUint16(offset, true),
length: dataview.getUint16(offset+2, true),
entrySizeBits: bits,
entrySizeBytes: Math.floor((bits + 7) / 8)
};
return colorMapSpec;
};
TGA.readImageSpec = function(dataview, offset) {
var descriptor = dataview.getUint8(offset+9);
var imageSpec = {
xOrigin: dataview.getUint16(offset, true),
yOrigin: dataview.getUint16(offset+2, true),
width: dataview.getUint16(offset+4, true),
height: dataview.getUint16(offset+6, true),
pixelDepth: dataview.getUint8(offset+8),
descriptor: descriptor,
attributeBits: descriptor & IMAGE_DESCRIPTOR_ATTRIBUTE_MASK,
origin: (descriptor & IMAGE_DESCRIPTOR_ORIGIN_MASK) >> 4,
interleave: (descriptor & IMAGE_DESCRIPTOR_INTERLEAVE_MASK) >> 6
};
return imageSpec;
};
TGA.readImageId = function(dataview, header) {
return new Uint8Array(dataview.buffer, HEADER_SIZE, header.idLength);
};
TGA.readColorMap = function(dataview, header) {
if(header.colorMapSpec.length <= 0) {
return null;
}
var colorMap = new Uint8ClampedArray(header.colorMapSpec.length * 4),
read = null,
offset = HEADER_SIZE + header.idLength;
switch(header.colorMapSpec.entrySizeBits) {
case 8:
read = TGA.readPixel8;
break;
case 16:
read = TGA.readPixel15;
break;
case 15:
read = TGA.readPixel16;
break;
case 24:
read = TGA.readPixel24;
break;
case 32:
read = TGA.readPixel32;
break;
default:
throw 'Unsupported pixel depth';
}
for(var i = 0; i < header.colorMapSpec.length; i++) {
read(dataview, offset, i, colorMap, i);
}
return colorMap;
};
TGA.readPixel8 = function(input, offset, i, output, j) {
var byte = input.getUint8(offset + i);
output[j * 4 + 2] = byte; // blue
output[j * 4 + 1] = byte; // green
output[j * 4 + 0] = byte; // red
output[j * 4 + 3] = 255; // alpha
};
TGA.readPixel15 = function(input, offset, i, output, j) {
var chunk = input.getUint16(offset + (i * 2), true);
output[j * 4 + 2] = (chunk & 0x1f) << 3; // blue
output[j * 4 + 1] = ((chunk >> 5) & 0x1f) << 3; // green
output[j * 4 + 0] = ((chunk >> 10) & 0x1f) << 3; // red
output[j * 4 + 3] = 255; // alpha
};
TGA.readPixel16 = function(input, offset, i, output, j) {
var chunk = input.getUint16(offset + (i * 2), true);
output[j * 4 + 2] = (chunk & 0x1f) << 3; // blue
output[j * 4 + 1] = ((chunk >> 5) & 0x1f) << 3; // green
output[j * 4 + 0] = ((chunk >> 10) & 0x1f) << 3; // red
output[j * 4 + 3] = (chunk & 0x80) == 0x80 ? 255 : 0; // alpha
};
TGA.readPixel24 = function(input, offset, i, output, j) {
output[j * 4 + 2] = input.getUint8(offset + (i * 3) + 0); //blue
output[j * 4 + 1] = input.getUint8(offset + (i * 3) + 1); //green
output[j * 4 + 0] = input.getUint8(offset + (i * 3) + 2); //red
output[j * 4 + 3] = 255;
};
TGA.readPixel32 = function(input, offset, i, output, j) {
output[j * 4 + 2] = input.getUint8(offset + (i * 4) + 0); //blue
output[j * 4 + 1] = input.getUint8(offset + (i * 4) + 1); //green
output[j * 4 + 0] = input.getUint8(offset + (i * 4) + 2); //red
output[j * 4 + 3] = 255;//input.getUint8(offset + (i * 4) + 3); // alpha
};
TGA.readMappedPixel8 = function(input, map, mapOffset, offset, i, output, j) {
var index = input.getUint8(offset + i) + mapOffset;
output[j * 4 + 0] = map[index * 4 + 0]; // blue
output[j * 4 + 1] = map[index * 4 + 1]; // green
output[j * 4 + 2] = map[index * 4 + 2]; // red
output[j * 4 + 3] = map[index * 4 + 3]; // alpha
};
// not sure these need to be separate functions...
TGA.readMappedPixel15 = function(input, map, mapOffset, offset, i, output, j) {
var index = input.getUint16(offset + (i * 2), true) + mapOffset;
output[j * 4 + 0] = map[index * 4 + 0]; // blue
output[j * 4 + 1] = map[index * 4 + 1]; // green
output[j * 4 + 2] = map[index * 4 + 2]; // red
output[j * 4 + 3] = map[index * 4 + 3]; // alpha
};
TGA.readMappedPixel16 = function(input, map, mapOffset, offset, i, output, j) {
var index = input.getUint16(offset + (i * 2), true) + mapOffset;
output[j * 4 + 0] = map[index * 4 + 0]; // blue
output[j * 4 + 1] = map[index * 4 + 1]; // green
output[j * 4 + 2] = map[index * 4 + 2]; // red
output[j * 4 + 3] = map[index * 4 + 3]; // alpha
};
// is this even valid?
TGA.readMappedPixel24 = function(input, map, mapOffset, offset, i, output, j) {
var index = input.getUint16(offset + (i * 2), true) + mapOffset; // uhhhhh
output[j * 4 + 0] = map[index * 4 + 0]; // blue
output[j * 4 + 1] = map[index * 4 + 1]; // green
output[j * 4 + 2] = map[index * 4 + 2]; // red
output[j * 4 + 3] = map[index * 4 + 3]; // alpha
};
// is this even valid, either?
TGA.readMappedPixel32 = function(input, map, mapOffset, offset, i, output, j) {
var index = input.getUint16(offset + (i * 2), true) + mapOffset; // uhhhhh
output[j * 4 + 0] = map[index * 4 + 0]; // blue
output[j * 4 + 1] = map[index * 4 + 1]; // green
output[j * 4 + 2] = map[index * 4 + 2]; // red
output[j * 4 + 3] = map[index * 4 + 3]; // alpha
};
TGA.readRLEImage = function(/*tga*/) {
throw 'NYI';
};
TGA.readColormappedImage = function(tga) {
var dataview = tga.dataview,
header = tga.header,
colorMap = tga.colorMap,
width = header.imageSpec.width,
height = header.imageSpec.height,
pixels = new Uint8ClampedArray(width * height * 4),
pixelDepth = header.imageSpec.pixelDepth,
offset = HEADER_SIZE + header.idLength +
(header.colorMapSpec.length * header.colorMapSpec.entrySizeBytes),
mapOffset = header.colorMapSpec.firstEntry,
read = null,
vScanDir = (header.imageSpec.origin & IMAGE_ORIGIN_VERTICAL_MASK) === IMAGE_ORIGIN_TOP ? 1 : -1,
hScanDir = (header.imageSpec.origin & IMAGE_ORIGIN_HORIZONTAL_MASK) === IMAGE_ORIGIN_RIGHT ? -1 : 1;
if(!colorMap) {
throw 'Image is described as color-mapped, but has no map';
}
switch(pixelDepth) {
case 8:
read = TGA.readMappedPixel8;
break;
case 16:
read = TGA.readMappedPixel15;
break;
case 15:
read = TGA.readMappedPixel16;
break;
case 24:
read = TGA.readMappedPixel24;
break;
case 32:
read = TGA.readMappedPixel32;
break;
default:
throw 'Unsupported pixel depth';
}
var vStart, vEnd, hStart, hEnd;
if(vScanDir > 0) {
vStart = 0;
vEnd = height;
} else {
vStart = height - 1;
vEnd = -1;
}
if(hScanDir > 0) {
hStart = 0;
hEnd = width;
} else {
hStart = width - 1;
hEnd = -1;
}
// output is always top->bottom, left->right, so:
var row = 0, col;
for(var i = vStart; i != vEnd; i += vScanDir) {
col = 0;
for(var j = hStart; j != hEnd; j += hScanDir) {
read(dataview, colorMap, mapOffset, offset, i * width + j, pixels, row * width + col++);
}
row++;
}
return pixels;
};
TGA.readTruecolorImage = function(tga) {
var header = tga.header,
dataview = tga.dataview,
width = header.imageSpec.width,
height = header.imageSpec.height,
pixels = new Uint8ClampedArray(width * height * 4),
pixelDepth = header.imageSpec.pixelDepth,
offset = HEADER_SIZE + header.idLength +
(header.colorMapSpec.length * header.colorMapSpec.entrySizeBytes),
read = null,
vScanDir = (header.imageSpec.origin & IMAGE_ORIGIN_VERTICAL_MASK) === IMAGE_ORIGIN_TOP ? 1 : -1,
hScanDir = (header.imageSpec.origin & IMAGE_ORIGIN_HORIZONTAL_MASK) === IMAGE_ORIGIN_RIGHT ? -1 : 1;
switch(pixelDepth) {
case 8:
read = TGA.readPixel8;
break;
case 16:
read = TGA.readPixel15;
break;
case 15:
read = TGA.readPixel16;
break;
case 24:
read = TGA.readPixel24;
break;
case 32:
read = TGA.readPixel32;
break;
default:
throw 'Unsupported pixel depth';
}
var vStart, vEnd, hStart, hEnd;
if(vScanDir > 0) {
vStart = 0;
vEnd = height;
} else {
vStart = height - 1;
vEnd = -1;
}
if(hScanDir > 0) {
hStart = 0;
hEnd = width;
} else {
hStart = width - 1;
hEnd = -1;
}
// output is always top->bottom, left->right, so:
var row = 0, col;
for(var i = vStart; i != vEnd; i += vScanDir) {
col = 0;
for(var j = hStart; j != hEnd; j += hScanDir) {
read(dataview, offset, i * width + j, pixels, row * width + col++);
}
row++;
}
return pixels;
};
TGA.readImage = function(tga) {
if(tga.header.compressed) {
return TGA.readRLEImage(tga);
} else {
if(tga.header.mapType === 0) { // not color mapped:
return TGA.readTruecolorImage(tga);
} else if(tga.header.mapType === 1) { // color mapped
return TGA.readColormappedImage(tga);
} else {
throw 'Unsupported map type';
}
}
};
// Base function.
var libtga = {
readFile: function(arraybuf) {
return new TGA(arraybuf);
},
loadFile: function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
callback(null, new TGA(this.response));
};
xhr.onerror = function(e) {
callback(e, null);
};
xhr.send();
},
TGA: TGA,
VERSION: '0.3.1'
};
export default libtga;