mirror of
https://github.com/netbymatt/nexrad-level-3-plot.git
synced 2026-11-28 10:48:04 -08:00
merge
This commit is contained in:
1 parent
fc79c0af91
commit
df0279a4e0
3 files changed
+59
-3
No files matched your search
@@ -16,6 +16,7 @@
|
||||
"palettized",
|
||||
"palletize",
|
||||
"palletized"
|
||||
"palletize"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
+23
-3
@@ -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
|
||||
|
||||
Reference in new issue
Block a user