From 00600c4213cac728d5e96b9cdba51e38303391a2 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Sat, 27 Dec 2025 21:46:48 -0800 Subject: [PATCH] feat: Parse GeoTIFF ColorMap tag --- packages/deck.gl-geotiff/src/geotiff.ts | 30 +++++++++++++++++++++++++ packages/deck.gl-geotiff/src/index.ts | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/deck.gl-geotiff/src/geotiff.ts b/packages/deck.gl-geotiff/src/geotiff.ts index fc2b450..2386d80 100644 --- a/packages/deck.gl-geotiff/src/geotiff.ts +++ b/packages/deck.gl-geotiff/src/geotiff.ts @@ -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); +} diff --git a/packages/deck.gl-geotiff/src/index.ts b/packages/deck.gl-geotiff/src/index.ts index 0dc9ffd..f3848ba 100644 --- a/packages/deck.gl-geotiff/src/index.ts +++ b/packages/deck.gl-geotiff/src/index.ts @@ -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";