-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (36 loc) · 1.56 KB
/
index.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
function getPalette (image, { debug = false } = { debug: false }) {
if (debug) console.log("starting getPalette with image", image);
const { fileDirectory } = image;
const { BitsPerSample, ColorMap } = fileDirectory;
if (!ColorMap) {
throw new Error("[geotiff-palette]: the image does not contain a color map, so we can't make a palette.");
}
const count = Math.pow(2, BitsPerSample);
if (debug) console.log("[geotiff-palette]: count:", count);
const bandSize = ColorMap.length / 3;
if (debug) console.log("[geotiff-palette]: bandSize:", bandSize);
if (bandSize !== count) {
throw new Error("[geotiff-palette]: can't handle situations where the color map has more or less values than the number of possible values in a raster");
}
const greenOffset = bandSize;
const redOffset = greenOffset + bandSize;
const result = [];
for (let i = 0; i < count; i++) {
// colorMap[mapIndex] / 65536 * 256 equals colorMap[mapIndex] / 256
// because (1 / 2^16) * (2^8) equals 1 / 2^8
result.push([
Math.floor(ColorMap[i] / 256), // red
Math.floor(ColorMap[greenOffset + i] / 256), // green
Math.floor(ColorMap[redOffset + i] / 256), // blue
255 // alpha value is always 255
]);
}
if (debug) console.log("[geotiff-palette]: result is ", result);
return result;
}
if (typeof define === "function" && define.amd) {
define(function() { return { getPalette } });
}
if (typeof module === "object") {
module.exports = { getPalette };
}