Interactive interface for browsing global, full-resolution satellite imagery
WV-3612 Disturbance layer color display issue (#5861)
Patrick Moulden3861ec22c656
diff --git a/web/js/components/sidebar/paletteLegend.js b/web/js/components/sidebar/paletteLegend.jsindex 830e1d2a1..c8874af14 100644--- a/web/js/components/sidebar/paletteLegend.js+++ b/web/js/components/sidebar/paletteLegend.js@@ -373,7 +373,7 @@ class PaletteLegend extends React.Component {{({ isVisible }) => (<div className={legendClass} key={`${legend.id}_${legendIndex}`}>{legend.colors.map((color, keyIndex) => {- const isActiveKey = activeKeyObj && activeKeyObj.index === keyIndex;+ const isActiveKey = activeKeyObj?.index === keyIndex;let palletteClass = isActiveKey ? 'wv-active wv-palettes-class' : 'wv-palettes-class';const isSubLayer = !!parentLayer;const parentLayerId = isSubLayer ? `-${parentLayer.id}` : '';diff --git a/web/js/ol/lookupimagetile.js b/web/js/ol/lookupimagetile.jsindex 865ca5081..9b2eace78 100644--- a/web/js/ol/lookupimagetile.js+++ b/web/js/ol/lookupimagetile.js@@ -1,7 +1,7 @@+/* eslint-disable func-names */import OlImageTile from 'ol/ImageTile';import OlTileState from 'ol/TileState';-import UPNG from 'upng-js';-import { cloneDeep as lodashCloneDeep } from 'lodash';+import { decodePNG, processImage } from './util';/*** @method getPixelColorsToDisplay@@ -20,13 +20,19 @@ function getPixelColorsToDisplay(obj) {}function getColormap(rawColormap) {- const colorMapArr = [];- for (let i = 0; i < rawColormap.length; i += 3) {- colorMapArr.push(rawColormap[i]);- colorMapArr.push(rawColormap[i + 1]);- colorMapArr.push(rawColormap[i + 2]);- colorMapArr.push(255);+ // Calculate number of colors in palette (each color is 3 bytes)+ const numColors = rawColormap.length / 3;+ // Create RGBA array (4 bytes per color)+ const colorMapArr = new Uint8Array(numColors * 4);++ // Fill with colors from PLTE+ for (let i = 0; i < numColors; i += 1) {+ colorMapArr[i * 4] = rawColormap[i * 3]; // R+ colorMapArr[(i * 4) + 1] = rawColormap[(i * 3) + 1]; // G+ colorMapArr[(i * 4) + 2] = rawColormap[(i * 3) + 2]; // B+ colorMapArr[(i * 4) + 3] = 255; // A}+return colorMapArr;}@@ -39,10 +45,10 @@ class LookupImageTile extends OlImageTile {this.customTileLoadFunction_ = tileLoadFunction;}}-LookupImageTile.prototype.getImage = function() {+LookupImageTile.prototype.getImage = function () {return this.canvas_;};-LookupImageTile.prototype.load = async function() {+LookupImageTile.prototype.load = async function () {if (this.state === OlTileState.IDLE) {this.state = OlTileState.LOADING;const that = this;@@ -53,66 +59,13 @@ LookupImageTile.prototype.load = async function() {that.canvas_ = document.createElement('canvas');that.canvas_.width = that.image_.width;that.canvas_.height = that.image_.height;- const octets = that.canvas_.width * that.canvas_.height * 4;const g = that.canvas_.getContext('2d');g.drawImage(that.image_, 0, 0);// If pixels were processed already, we skip this transformationif (!imageProcessed) {- const imageData = g.getImageData(- 0,- 0,- that.canvas_.width,- that.canvas_.height,- );-- // Process each pixel to color-swap single color palettes- const pixels = imageData.data;- const colorLookupObj = lodashCloneDeep(that.lookup_);- const defaultColor = Object.keys(that.lookup_)[0];- const paletteColor = that.lookup_[Object.keys(that.lookup_)[0]];-- // Load black/transparent into the lookup object- colorLookupObj['0,0,0,0'] = {- r: 0,- g: 0,- b: 0,- a: 0,- };-- for (let i = 0; i < octets; i += 4) {- const pixelColor = `${pixels[i + 0]},${- pixels[i + 1]},${- pixels[i + 2]},${- pixels[i + 3]}`;-- if (!colorLookupObj[pixelColor]) {- // Handle non-transparent pixels that do not match the palette exactly- const defaultColorArr = defaultColor.split(',');- const pixelColorArr = pixelColor.split(',');-- // Determine difference of pixel from default to replicate anti-aliasing- const rDifference = pixelColorArr[0] - defaultColorArr[0];- const gDifference = pixelColorArr[1] - defaultColorArr[1];- const bDifference = pixelColorArr[2] - defaultColorArr[2];- const alphaValue = pixelColorArr[3];-- // Store the resulting pair of pixel color & anti-aliased adjusted color- colorLookupObj[pixelColor] = {- r: paletteColor.r + rDifference,- g: paletteColor.g + gDifference,- b: paletteColor.b + bDifference,- a: alphaValue,- };- }-- // set the pixel color- pixels[i + 0] = colorLookupObj[pixelColor].r;- pixels[i + 1] = colorLookupObj[pixelColor].g;- pixels[i + 2] = colorLookupObj[pixelColor].b;- pixels[i + 3] = colorLookupObj[pixelColor].a;- }- g.putImageData(imageData, 0, 0);+ processImage(that.canvas_, that.lookup_);+ imageProcessed = true;}// uses the tileload function passed from layerbuilder@@ -135,90 +88,106 @@ LookupImageTile.prototype.load = async function() {if (!res.ok) throw new Error(`Failed to fetch image: ${res.status} ${res.statusText}`);const buffer = await res.arrayBuffer();// decode the buffer PNG file- const decodedPNG = UPNG.decode(buffer);- const { width, height } = decodedPNG;+ const decodedPNG = await decodePNG(buffer);+ const { width, height, data } = decodedPNG;- // Create an array buffer matching the pixel dimensions of the provided image- const bufferSize = height * width * 4;- const arrBuffer = new Uint32Array(bufferSize);+ that.canvas_ = document.createElement('canvas');+ that.canvas_.width = width;+ that.canvas_.height = height;+ const ctx = that.canvas_.getContext('2d');+ const blankImageData = ctx.createImageData(width, height);+ const outputData = blankImageData.data;- // Extract the pixel data. This is an array of integers corresponding to the colormap- // i.e. if pixelData[0] == 5, this pixel is the color of the 5th entry in the colormap- const pixelData = decodedPNG.data;+ // Extract the pixel data+ const pixelData = data;- let encodedBufferImage;-- if (decodedPNG.tabs && decodedPNG.tabs.PLTE) {- // Extract the colormap values. This is an array of integers representing rgba values.- // Used in sets of 4 (i.e. colorMapArr[0] = r, colorMapArr[1] = b, etc.)- // colorMapArr assumes a max of 256 colors+ if (decodedPNG?.tabs?.PLTE) {+ // Extract the colormap valuesconst colorMapArr = getColormap(decodedPNG.tabs.PLTE);- // iterate through the pixelData, drawing each pixel using the appropriate color+ // Iterate through pixelData, setting colors directly on outputDatafor (let i = 0; i < pixelData.length; i += 1) {- const arrBuffIndex = i * 4;- const lookupIndex = pixelData[i] * 4;+ const outputIndex = i * 4;+ // Make sure the index is valid+ const index = Math.min(pixelData[i], (colorMapArr.length / 4) - 1);+ const lookupIndex = index * 4;// Determine desired RGBA for this pixelconst r = colorMapArr[lookupIndex];const g = colorMapArr[lookupIndex + 1];const b = colorMapArr[lookupIndex + 2];- const a = 255;- // Concatentate to 'r,g,b,a' string & check if that color is in the pixelsToDisplay array+ const a = colorMapArr[lookupIndex + 3];++ // Concatenate to 'r,g,b,a' string & check if that color is in pixelsToDisplayconst rgbaStr = `${r},${g},${b},${a}`;const drawThisColor = pixelsToDisplay[rgbaStr];- // If the intended color exists in pixelsToDisplay obj, draw that color, otherwise draw transparent+ // If the intended color exists in pixelsToDisplay obj, draw that color, otherwise transparentif (drawThisColor !== undefined) {- arrBuffer[arrBuffIndex + 0] = r;- arrBuffer[arrBuffIndex + 1] = g;- arrBuffer[arrBuffIndex + 2] = b;- arrBuffer[arrBuffIndex + 3] = a;+ outputData[outputIndex] = r;+ outputData[outputIndex + 1] = g;+ outputData[outputIndex + 2] = b;+ outputData[outputIndex + 3] = a;} else {- // console.log('drawThisColor undefined, rgbaStr:', rgbaStr);- arrBuffer[arrBuffIndex] = 0;- arrBuffer[arrBuffIndex + 1] = 0;- arrBuffer[arrBuffIndex + 2] = 0;- arrBuffer[arrBuffIndex + 3] = 0;+ outputData[outputIndex] = 0;+ outputData[outputIndex + 1] = 0;+ outputData[outputIndex + 2] = 0;+ outputData[outputIndex + 3] = 0;}}-- // Encode the image, creating a new PNG file- encodedBufferImage = UPNG.encode([arrBuffer], decodedPNG.width, decodedPNG.height, decodedPNG.depth);} else {+ // For non-indexed PNG, copy pixel data to output, this will throw an error if the lengths differ+ outputData.set(pixelData);++ // Apply transparency based on color proximityfor (let i = 0; i < pixelData.length; i += 4) {const r = pixelData[i];const g = pixelData[i + 1];const b = pixelData[i + 2];- // If the intended color exists in pixelsToDisplay obj, draw that color, otherwise draw transparent+ // Calculate color differencelet smallestDiff = 765; // Maximum differenceObject.keys(pixelsToDisplay).forEach((pix) => {const pixSplit = pix.split(',');- const biggestDiff = Math.max(Math.abs(parseInt(r, 10) - parseInt(pixSplit[0], 10)), Math.abs(parseInt(g, 10) - parseInt(pixSplit[1], 10)), Math.abs(parseInt(b, 10) - parseInt(pixSplit[2], 10)));+ const biggestDiff = Math.max(+ Math.abs(parseInt(r, 10) - parseInt(pixSplit[0], 10)),+ Math.abs(parseInt(g, 10) - parseInt(pixSplit[1], 10)),+ Math.abs(parseInt(b, 10) - parseInt(pixSplit[2], 10)),+ );if (smallestDiff > biggestDiff) {smallestDiff = biggestDiff;}});- // Use difference to see how far away a color is from any pixelsToDisplay+// If difference is large enough, don't display the color- // This helps prevent edges of color dots from not being displayed accidentallyif (smallestDiff > 10) {- pixelData[i + 3] = 0;+ outputData[i + 3] = 0;}}+ }- // Encode the image, creating a new PNG file- encodedBufferImage = UPNG.encode([pixelData], decodedPNG.width, decodedPNG.height, 32);+ const newImageData = new ImageData(outputData, width, height);++ if (imageProcessed) {+ // Put imageData directly to canvas+ ctx.putImageData(newImageData, 0, 0);+ } else {+ processImage(that.canvas_, that.lookup_);+ imageProcessed = true;}- const blob = new Blob([encodedBufferImage], { type: 'image/png' });- const dataURL = `${URL.createObjectURL(blob)}`;- this.image_.src = dataURL;- this.image_.addEventListener('load', onImageLoad);+ // Mark as loaded+ that.state = OlTileState.LOADED;+ that.changed();++ // Call custom tile load function if provided+ if (that.customTileLoadFunction_) {+ that.customTileLoadFunction_(that, that.src_);+ }} catch (error) {that.state = OlTileState.ERROR;that.changed();+ // eslint-disable-next-line no-consoleconsole.error('Error:', error);}} else {@@ -227,7 +196,6 @@ LookupImageTile.prototype.load = async function() {}}};-export default function lookupFactory(lookup, sourceOptions) {return function(tileCoord, state, src, crossOrigin, tileLoadFunction) {return new LookupImageTile(diff --git a/web/js/ol/util.js b/web/js/ol/util.jsnew file mode 100644index 000000000..0f95d59b2--- /dev/null+++ b/web/js/ol/util.js@@ -0,0 +1,281 @@+/**+ * @param {Canvas} canvas - Canvas to process+ * @param {Object} lookup - Color lookup object+ * @returns {void}+ * @description Processes the canvas to swap colors based on the provided lookup object.+ */+function processImage(canvas, lookup) {+ // If pixels were processed already, we skip this transformation+ const octets = canvas.width * canvas.height * 4;+ const context = canvas.getContext('2d');+ const imageData = context.getImageData(+ 0,+ 0,+ canvas.width,+ canvas.height,+ );++ // Process each pixel to color-swap single color palettes+ const pixels = imageData.data;+ const colorLookupObj = structuredClone(lookup);+ const defaultColor = Object.keys(lookup)[0];+ const paletteColor = lookup[Object.keys(lookup)[0]];++ // Load black/transparent into the lookup object+ colorLookupObj['0,0,0,0'] = {+ r: 0,+ g: 0,+ b: 0,+ a: 0,+ };++ for (let i = 0; i < octets; i += 4) {+ const pixelColor = `${pixels[i + 0]},${pixels[i + 1]},${pixels[i + 2]},${pixels[i + 3]}`;++ if (!colorLookupObj[pixelColor]) {+ // Handle non-transparent pixels that do not match the palette exactly+ const defaultColorArr = defaultColor.split(',');+ const pixelColorArr = pixelColor.split(',');++ // Determine difference of pixel from default to replicate anti-aliasing+ const rDifference = pixelColorArr[0] - defaultColorArr[0];+ const gDifference = pixelColorArr[1] - defaultColorArr[1];+ const bDifference = pixelColorArr[2] - defaultColorArr[2];+ const alphaValue = pixelColorArr[3];++ // Store the resulting pair of pixel color & anti-aliased adjusted color+ colorLookupObj[pixelColor] = {+ r: paletteColor.r + rDifference,+ g: paletteColor.g + gDifference,+ b: paletteColor.b + bDifference,+ a: alphaValue,+ };+ }++ // set the pixel color+ pixels[i + 0] = colorLookupObj[pixelColor].r;+ pixels[i + 1] = colorLookupObj[pixelColor].g;+ pixels[i + 2] = colorLookupObj[pixelColor].b;+ pixels[i + 3] = colorLookupObj[pixelColor].a;+ }+ context.putImageData(imageData, 0, 0);+}++/**+ * Extract indexed data for palette-based PNGs+ * @param {Uint8ClampedArray} pixels - RGBA pixel data from canvas+ * @param {Uint8Array} palette - PLTE data+ * @param {Number} width - Image width+ * @param {Number} height - Image height+ * @param {Uint8Array} alphaValues - tRNS data (optional)+ * @returns {Uint8Array} - Array of palette indices+ */+const extractIndexedData = (pixels, palette, width, height, alphaValues) => {+ // Create a map of RGB values to palette indices for lookup+ const paletteMap = new Map();+ const indexedData = new Uint8Array(width * height);++ // Build palette map+ for (let i = 0; i < palette.length; i += 3) {+ const r = palette[i];+ const g = palette[i + 1];+ const b = palette[i + 2];+ // Alpha is 255 unless specified in tRNS+ const a = alphaValues && (i / 3) < alphaValues.length ? alphaValues[i / 3] : 255;+ const key = `${r},${g},${b},${a}`;+ paletteMap.set(key, i / 3);+ }++ // Map each pixel to its palette index+ for (let i = 0, j = 0; i < pixels.length; i += 4, j += 1) {+ const r = pixels[i];+ const g = pixels[i + 1];+ const b = pixels[i + 2];+ const a = pixels[i + 3];++ // Try to find this color in the palette+ const key = `${r},${g},${b},${a}`;++ if (paletteMap.has(key)) {+ indexedData[j] = paletteMap.get(key);+ } else if (a === 0) {+ // Fully transparent pixel - use index 0+ indexedData[j] = 0;+ } else {+ // Find closest color in palette+ let bestIndex = 0;+ let bestDiff = Infinity;++ for (let p = 0; p < palette.length; p += 3) {+ const pr = palette[p];+ const pg = palette[p + 1];+ const pb = palette[p + 2];+ const pa = alphaValues && p / 3 < alphaValues.length ? alphaValues[p / 3] : 255;++ // Skip transparent entries when looking for opaque colors+ if (!(a > 0 && pa === 0)) {+ const diff = Math.abs(r - pr) + Math.abs(g - pg) + Math.abs(b - pb) + Math.abs(a - pa);++ if (diff < bestDiff) {+ bestDiff = diff;+ bestIndex = p / 3;+ }+ }+ }++ indexedData[j] = bestIndex;+ }+ }++ return indexedData;+};++/**+ * Use Canvas to decode the PNG pixel data+ * @param {ArrayBuffer} buffer - PNG data+ * @returns {Promise<Object>} - Canvas and pixel data+ */+const getPixelDataFromCanvas = async (buffer) => {+ const blob = new Blob([buffer], { type: 'image/png' });+ const blobUrl = URL.createObjectURL(blob);++ try {+ // Create an image and wait for it to load+ const img = await new Promise((resolve, reject) => {+ const image = new Image();+ image.onload = () => resolve(image);+ image.onerror = (err) => reject(new Error(`Failed to load PNG: ${err}`));+ image.src = blobUrl;+ });++ // Draw the image to canvas+ const canvas = document.createElement('canvas');+ canvas.width = img.width;+ canvas.height = img.height;+ const ctx = canvas.getContext('2d');+ ctx.drawImage(img, 0, 0);++ // Get pixel data+ const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);++ return { pixels: imageData.data, canvas };+ } finally {+ URL.revokeObjectURL(blobUrl);+ }+};++/**+ * PNG Chunk Parser - Reads PNG binary format and extracts key chunks+ * @param {ArrayBuffer} buffer - Raw PNG data as ArrayBuffer+ * @returns {Promise<Object>} Decoded PNG information+ */+const parsePNG = async (buffer) => {+ // Function to read a PNG chunk+ const readChunk = (dataView, offset) => {+ const length = dataView.getUint32(offset, false); // Big-endian+ const type = String.fromCharCode(+ dataView.getUint8(offset + 4),+ dataView.getUint8(offset + 5),+ dataView.getUint8(offset + 6),+ dataView.getUint8(offset + 7),+ );++ // Extract chunk data+ const data = new Uint8Array(buffer, offset + 8, length);++ // Calculate next chunk position (length + type + data + CRC)+ const nextChunkOffset = offset + 8 + length + 4;++ return {+ type,+ data,+ length,+ nextOffset: nextChunkOffset,+ };+ };++ // Verify PNG signature+ const dataView = new DataView(buffer);+ const signature = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];++ if (signature.some((val, i) => dataView.getUint8(i) !== val)) throw new Error('Invalid PNG signature');++ // Start parsing chunks after signature+ let offset = 8;+ let ihdrChunk = null;+ let plteChunk = null;+ let trnsChunk = null;++ // Read chunks until we find what we need or reach the end+ while (offset < buffer.byteLength) {+ const chunk = readChunk(dataView, offset);++ if (chunk.type === 'IHDR') {+ ihdrChunk = chunk;+ } else if (chunk.type === 'PLTE') {+ plteChunk = chunk;+ } else if (chunk.type === 'tRNS') {+ trnsChunk = chunk;+ } else if (chunk.type === 'IEND') {+ break;+ }++ offset = chunk.nextOffset;+ }++ // We need IHDR to proceed+ if (!ihdrChunk) throw new Error('Missing IHDR chunk');++ // Parse IHDR data+ const ihdrView = new DataView(ihdrChunk.data.buffer, ihdrChunk.data.byteOffset, ihdrChunk.data.byteLength);+ const width = ihdrView.getUint32(0, false);+ const height = ihdrView.getUint32(4, false);+ const bitDepth = ihdrView.getUint8(8);+ const colorType = ihdrView.getUint8(9);++ // Get actual pixel data using canvas (more reliable than trying to implement decompression)+ const { pixels, canvas } = await getPixelDataFromCanvas(buffer);++ // Process palette if available+ let palette = null;+ let alphaValues = null;+ const isIndexed = colorType === 3; // Color type 3 is indexed color++ if (plteChunk) palette = new Uint8Array(plteChunk.data);++ // Process transparency if available+ // For indexed color, tRNS contains alpha values for palette entries+ if (trnsChunk && plteChunk) alphaValues = new Uint8Array(trnsChunk.data);++ // Create indexed data if this is an indexed color PNG+ let indexedData = null;+ // For true indexed PNGs, the pixel data is already indices. We'll need to extract it from the canvas data+ if (isIndexed && palette) indexedData = extractIndexedData(pixels, palette, width, height, alphaValues);++ const tabsObj = {+ PLTE: palette,+ tRNS: alphaValues || undefined,+ };+ const tabs = palette ? tabsObj : undefined;++ return {+ width,+ height,+ bitDepth,+ colorType,+ isIndexed,+ data: isIndexed ? indexedData : new Uint8Array(pixels.buffer),+ rgba: new Uint8Array(pixels.buffer),+ tabs,+ canvas,+ };+};++const decodePNG = async (buffer) => parsePNG(buffer);++export {+ decodePNG,+ extractIndexedData,+ getPixelDataFromCanvas,+ processImage,+};