diff --git a/nexrad-level-3-plot.code-workspace b/nexrad-level-3-plot.code-workspace index d25b670..9f54ee1 100644 --- a/nexrad-level-3-plot.code-workspace +++ b/nexrad-level-3-plot.code-workspace @@ -16,6 +16,7 @@ "palettized", "palletize", "palletized" + "palletize" ] } } \ No newline at end of file diff --git a/src/palletize/closest.js b/src/palletize/closest.js new file mode 100644 index 0000000..4ba224f --- /dev/null +++ b/src/palletize/closest.js @@ -0,0 +1,35 @@ +// return the index of the closest color match in the palette + +// memoize results by provided key. +const cache = {}; + +const closest = (match, palette, key) => { + // generate cache key + if (!cache[key]) cache[key] = {}; + + // short circuit previously calculated matches + const asHex = match[0].toString(16).padStart(2, '0') + match[1].toString(16).padStart(2, '0') + match[2].toString(16).padStart(2, '0'); + if (cache[key][asHex]) return cache[key][asHex]; + + // initial conditions + let closestIndex = 0; + let closestDist = Infinity; + // loop through array + for (let i = 0; i < palette.length; i += 4) { + const dist = geometricDistance(match, palette.slice(i, i + match.length)); + // test for closer + if (dist < closestDist) { + closestDist = dist; + closestIndex = i / 4; + // short circuit on exact match + if (dist === 0) break; + } + } + // store closest match to speed up next iteration + cache[asHex] = closestIndex; + return closestIndex; +}; + +const geometricDistance = (a, b) => a.reduce((acc, val, idx) => acc + (val - b[idx]) ** 2, 0); + +module.exports = closest(); diff --git a/src/palletize/index.js b/src/palletize/index.js index ebb0b99..7080443 100644 --- a/src/palletize/index.js +++ b/src/palletize/index.js @@ -1,5 +1,7 @@ // palletize an image +const { createCanvas } = require('canvas'); const generatePalette = require('./generatepalette'); +const closest = require('./closest'); // default options const DEFAULT_OPTIONS = { @@ -7,15 +9,33 @@ const DEFAULT_OPTIONS = { palette: [], }; -const cache = {}; - -const palletize = (image, product, _options) => { +const palletize = (sourceCtx, product, _options) => { // combine options and defaults const options = combineOptions(_options, product); // always call the generate palette function // it will return a key for caching purposes and add the necessary transparent index const { palette, key } = generatePalette(options.palette, options.generate, _options.background, product); + + // get original image data + const sourceImageData = sourceCtx.getImageData(0, 0, _options.size, _options.size); + + // create indexed canvas + const indexedCanvas = createCanvas(_options.size, _options.size); + const indexedCtx = indexedCanvas.getContext('2d', { pixelFormat: 'A8' }); + const indexedImageData = indexedCtx.getImageData(0, 0, _options.size, _options.size); + + // loop through each pixel + indexedImageData.data.forEach((val, idx) => { + indexedImageData.data[idx] = closest(sourceImageData.data.slice(idx * 4, idx * 4 + 3), palette, key); + }); + + // write new data + indexedCtx.putImageData(indexedImageData, 0, 0); + + // attach palette and return + indexedCanvas.palette = palette; + return indexedCanvas; }; // manually combine options