Skip to content

Commit

Permalink
Improve memory consumption
Browse files Browse the repository at this point in the history
The palette reduction step is taking up an inordinate amount of memory. With a few optimizations, the conversion of the test image went from 1.6 GB down to 0.5 GB.

Note that this still isn't efficient enough to handle large images.

Merge pull request #14 from haroldo-ok/improve-memory
This fixes #12
  • Loading branch information
haroldo-ok authored Aug 13, 2023
2 parents eb056b2 + 658cd8c commit eb2b039
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
/*.png
/*.jpg
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.1.0",
"version": "0.1.1",
"description": "RgbQuant.js adapted for quantizing images for the Sega Master System hardware",
"main": "index.js",
"scripts": {
Expand Down
40 changes: 31 additions & 9 deletions src/rgbquant-sms.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,46 @@
function buildKey(histogram) {
return Array.prototype.slice.call(histogram).join(',');
}

var index = _.groupBy(tilesToClusterize, function(data){ return buildKey(data.histogram) });
this.quants = clusters.map(function(cluster){
var tiles = _.chain(cluster).map(function(histogram){
tilesToClusterize = null;
for (const k in index) {
if (index.hasOwnProperty(k)) {
index[k] = index[k].map(o => o.tile.pixels);
}
}

var pixelIndexesForClusters = clusters.map(function(cluster){
var pixelIndexes = _.chain(cluster).map(function(histogram){
// Finds the tile corresponding to the cluster element
return index[buildKey(histogram)];
}).flatten().pluck('tile').value();
}).flatten().value();

// Free up memory
cluster.length = 0;

var pixelIndexes = _.chain(tiles).pluck('pixels').flatten().value();
return pixelIndexes;
});
clusters = null;
index = null;

this.quants = pixelIndexesForClusters.map(function(pixelIndexes){
var pixelValues = pixelIndexes.map(function(pixel){
var rgb = palette[pixel];
return (255 << 24) | // alpha
(rgb[2] << 16) | // blue
(rgb[1] << 8) | // green
rgb[0];
});
pixelIndexes = null;

var uint32pixels = new Uint32Array(pixelValues);
pixelValues = null;

var quant = new RgbQuant(self.quantizerOpts);
quant.sample(new Uint32Array(pixelValues), 8);
quant.sample(uint32pixels, 8);
return quant;
});
});
}

/**
Expand Down Expand Up @@ -617,10 +637,12 @@

RgbQuantSMS.prototype.convert = function(img) {
this.sample(img);
const palettes = this.palettes();
this.palettes();

const unoptimizedTileMap = this.reduceToTileMap(img);
let unoptimizedTileMap = this.reduceToTileMap(img);
const optimizedTileMap = this.normalizeTiles(unoptimizedTileMap);
unoptimizedTileMap = null;

this.updateTileEntropy(optimizedTileMap.tiles);
const similarTiles = this.groupBySimilarity(optimizedTileMap);
const reducedTileMap = this.removeSimilarTiles(optimizedTileMap, similarTiles);
Expand Down

0 comments on commit eb2b039

Please sign in to comment.