Skip to content

Commit

Permalink
Optimized to run faster and use much less memory if using single palette
Browse files Browse the repository at this point in the history
Merge pull request #18 from haroldo-ok/optimize-single-palette
  • Loading branch information
haroldo-ok authored Aug 17, 2023
2 parents dad6cbf + 0c79edf commit 65aac90
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
66 changes: 41 additions & 25 deletions src/rgbquant-sms.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,8 @@
};
});
}));

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++) {
Expand All @@ -112,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);
}

/**
Expand Down

0 comments on commit 65aac90

Please sign in to comment.