Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/deck.gl-geotiff/src/geotiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,33 @@ function addAlphaChannel(rgbImage: TypedArrayWithDimensions): ImageData {
);
}
}

/**
* Parse the GeoTIFF `ColorMap` tag into an ImageData.
*
* @param {Uint16Array} cmap The colormap array from the GeoTIFF `ColorMap` tag.
*
* @return {ImageData} The parsed colormap as an ImageData object.
*/
export function parseColormap(cmap: Uint16Array): ImageData {
// TODO: test colormap handling on a 16-bit image with 2^16 entries?
const size = cmap.length / 3;
const rgba = new Uint8ClampedArray(size * 4);

const rOffset = 0;
const gOffset = size;
const bOffset = size * 2;

// Note: >> 8 is needed to convert from 16-bit to 8-bit color values
// It just divides by 256 and floors to nearest integer
for (let i = 0; i < size; i++) {
rgba[4 * i + 0] = cmap[rOffset + i]! >> 8;
rgba[4 * i + 1] = cmap[gOffset + i]! >> 8;
rgba[4 * i + 2] = cmap[bOffset + i]! >> 8;

// Full opacity
rgba[4 * i + 3] = 255;
}

return new ImageData(rgba, size, 1);
}
2 changes: 1 addition & 1 deletion packages/deck.gl-geotiff/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export {
extractGeotiffReprojectors,
fromGeoTransform,
} from "./geotiff-reprojection.js";
export { loadRgbImage } from "./geotiff.js";
export { loadRgbImage, parseColormap } from "./geotiff.js";

export * as proj from "./proj.js";
export * as texture from "./texture.js";