improve palletizing algorithm

This commit is contained in:
Matt Walsh committed 2026-08-12 00:17:21 -05:00
1 parent 53857aabd9
commit ea23ea2e68
2 files changed
+15 -12

No files matched your search

+8 -8
View File
@@ -57,20 +57,20 @@ class Palette {
return `rgba(${this.palette[index * 4]},${this.palette[index * 4 + 1]},${this.palette[index * 4 + 2]},${this.palette[(index * 4 + 3)] / 255})`;
}
// match = [r,g,b[,a]]
closestIndex(match) {
// r,g,b = individual color components, passed separately to avoid allocating
// a temporary array for every pixel (this is called once per output pixel)
closestIndex(r, g, b) {
// short circuit for transparent (black)
if (match[0] <= 2 && match[1] <= 2 && match[2] <= 2) return this.transparentIndex;
if (r <= 2 && g <= 2 && b <= 2) return this.transparentIndex;
// short circuit previously calculated matches
const asHex = hexLookup[match[0]] + hexLookup[match[1]] + hexLookup[match[2]];
// const asHex = match[0].toString(16).padStart(2, '0') + match[1].toString(16).padStart(2, '0') + match[2].toString(16).padStart(2, '0');
const asHex = hexLookup[r] + hexLookup[g] + hexLookup[b];
if (this.closest[asHex]) return this.closest[asHex];
// initial conditions
let closestIndex = 0;
let closest = Infinity;
// loop through array
for (let i = 0; i < this.palette.length; i += 4) {
const dist = Palette.geometricDistance(match, this.palette.slice(i, i + match.length));
const dist = Palette.geometricDistance(r, g, b, this.palette[i], this.palette[i + 1], this.palette[i + 2]);
// test for closer
if (dist < closest) {
closest = dist;
@@ -85,8 +85,8 @@ class Palette {
// geometric distance
// square root is intentionally not taken for performance reasons
static geometricDistance(a, b) {
return a.reduce((acc, val, idx) => acc + (val - b[idx]) ** 2, 0);
static geometricDistance(r1, g1, b1, r2, g2, b2) {
return (r1 - r2) ** 2 + (g1 - g2) ** 2 + (b1 - b2) ** 2;
}
}
+7 -4
View File
@@ -13,14 +13,17 @@ const palettizeImage = (sourceCtx, palette) => {
const indexedCtx = indexedCanvas.getContext('2d', { pixelFormat: 'A8' });
// get the source image data
const sourceImageData = sourceCtx.getImageData(0, 0, dim.x, dim.y);
const { data: sourceData } = sourceCtx.getImageData(0, 0, dim.x, dim.y);
// get the indexed image data (destination)
const indexedImageData = indexedCtx.getImageData(0, 0, dim.x, dim.y);
const { data: indexedData } = indexedImageData;
// loop through each pixel
indexedImageData.data.forEach((val, idx) => {
indexedImageData.data[idx] = palette.closestIndex(sourceImageData.data.slice(idx * 4, idx * 4 + 3));
});
// (a plain for loop indexing directly into the source data avoids allocating
// a new typed array per pixel, which a .slice()-based approach would require)
for (let idx = 0, srcIdx = 0; idx < indexedData.length; idx += 1, srcIdx += 4) {
indexedData[idx] = palette.closestIndex(sourceData[srcIdx], sourceData[srcIdx + 1], sourceData[srcIdx + 2]);
}
// write the new image data
indexedCtx.putImageData(indexedImageData, 0, 0);