From 3e147643dfa661c6e3f036483a4e2bb069550e4f Mon Sep 17 00:00:00 2001 From: Haroldo de Oliveira Pinheiro Date: Thu, 17 Aug 2023 17:29:49 -0300 Subject: [PATCH 1/3] Test optimization of color quant, for single pal. --- src/rgbquant-sms.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/rgbquant-sms.js b/src/rgbquant-sms.js index 9dc61ac..34ac32d 100644 --- a/src/rgbquant-sms.js +++ b/src/rgbquant-sms.js @@ -75,6 +75,29 @@ }; }); })); + + // FIXME: Test 2 + var pixelIndexes = _.flatten(tilesToClusterize.map(o => o.tile.pixels)); + + // Convert pixel palette indexes into the actual colors + const uint32pixels = new Uint32Array(pixelIndexes); + for (let i = 0; i < uint32pixels.length; i++) { + const pixelIndex = uint32pixels[i]; + const rgb = palette[pixelIndex]; + uint32pixels[i] = + (255 << 24) | // alpha + (rgb[2] << 16) | // blue + (rgb[1] << 8) | // green + rgb[0]; + } + pixelIndexes = null; + + var quant = new RgbQuant(self.quantizerOpts); + quant.sample(uint32pixels, 8); + + this.quants = [quant]; + + return; var clusters = clusterfck.kmeans(_.pluck(tilesToClusterize, 'histogram'), this.paletteCount); From 397b592b0c957b7819fd9a646de38edf601f84ec Mon Sep 17 00:00:00 2001 From: Haroldo de Oliveira Pinheiro Date: Thu, 17 Aug 2023 17:54:31 -0300 Subject: [PATCH 2/3] Use simpler conversion if using single palette. --- src/rgbquant-sms.js | 89 +++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 48 deletions(-) diff --git a/src/rgbquant-sms.js b/src/rgbquant-sms.js index 34ac32d..c20c4a6 100644 --- a/src/rgbquant-sms.js +++ b/src/rgbquant-sms.js @@ -76,52 +76,7 @@ }); })); - // FIXME: Test 2 - var pixelIndexes = _.flatten(tilesToClusterize.map(o => o.tile.pixels)); - - // Convert pixel palette indexes into the actual colors - const uint32pixels = new Uint32Array(pixelIndexes); - for (let i = 0; i < uint32pixels.length; i++) { - const pixelIndex = uint32pixels[i]; - const rgb = palette[pixelIndex]; - uint32pixels[i] = - (255 << 24) | // alpha - (rgb[2] << 16) | // blue - (rgb[1] << 8) | // green - rgb[0]; - } - pixelIndexes = null; - - var quant = new RgbQuant(self.quantizerOpts); - quant.sample(uint32pixels, 8); - - this.quants = [quant]; - - return; - - var clusters = clusterfck.kmeans(_.pluck(tilesToClusterize, 'histogram'), this.paletteCount); - - function buildKey(histogram) { - return Array.prototype.slice.call(histogram).join(','); - } - - var index = _.groupBy(tilesToClusterize, function(data){ return buildKey(data.histogram) }); - tilesToClusterize = null; - for (const k in index) { - if (index.hasOwnProperty(k)) { - index[k] = index[k].map(o => o.tile.pixels); - } - } - - this.quants = clusters.map(function(cluster){ - var pixelIndexes = _.flatten(cluster.map(histogram => { - // Finds the tileset corresponding to the cluster element - return index[buildKey(histogram)]; - })); - - // Free up memory - cluster.length = 0; - + const pixelIndexesToQuant = pixelIndexes => { // Convert pixel palette indexes into the actual colors const uint32pixels = new Uint32Array(pixelIndexes); for (let i = 0; i < uint32pixels.length; i++) { @@ -135,10 +90,48 @@ } pixelIndexes = null; - var quant = new RgbQuant(self.quantizerOpts); + const quant = new RgbQuant(self.quantizerOpts); quant.sample(uint32pixels, 8); + return quant; - }); + } + + const quantizeSinglePalette = tilesToClusterize => { + var pixelIndexes = _.flatten(tilesToClusterize.map(o => o.tile.pixels)); + return [pixelIndexesToQuant(pixelIndexes)]; + } + + const quantizeMultiPalette = tilesToClusterize => { + var clusters = clusterfck.kmeans(_.pluck(tilesToClusterize, 'histogram'), this.paletteCount); + + function buildKey(histogram) { + return Array.prototype.slice.call(histogram).join(','); + } + + var index = _.groupBy(tilesToClusterize, function(data){ return buildKey(data.histogram) }); + tilesToClusterize = null; + for (const k in index) { + if (index.hasOwnProperty(k)) { + index[k] = index[k].map(o => o.tile.pixels); + } + } + + return clusters.map(function(cluster){ + var pixelIndexes = _.flatten(cluster.map(histogram => { + // Finds the tileset corresponding to the cluster element + return index[buildKey(histogram)]; + })); + + // Free up memory + cluster.length = 0; + + return pixelIndexesToQuant(pixelIndexes); + }); + } + + this.quants = this.paletteCount > 1 ? + quantizeMultiPalette(tilesToClusterize) : + quantizeSinglePalette(tilesToClusterize); } /** From 0c79edfdf1cec1a445daf6dc4b4a25a4898369cb Mon Sep 17 00:00:00 2001 From: Haroldo de Oliveira Pinheiro Date: Thu, 17 Aug 2023 18:02:50 -0300 Subject: [PATCH 3/3] Bump version to 0.2.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b83fe94..b5ab73d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rgbquant-sms", - "version": "0.2.1", + "version": "0.2.2", "description": "RgbQuant.js adapted for quantizing images for the Sega Master System hardware", "main": "index.js", "scripts": {