Skip to content

Commit

Permalink
update: added choice between k-means and k-means++
Browse files Browse the repository at this point in the history
  • Loading branch information
f-aguzzi committed Mar 7, 2024
1 parent fdde3e2 commit f2f934e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
24 changes: 3 additions & 21 deletions demo/scripts/color_palette_wasm_demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import EditPix from "../../src/editpix.js";

const editpix = new EditPix();

const url = "images/img5.jpeg";
const url = "images/img1.jpg";

var image = new Image();
image.src = url;
Expand All @@ -12,31 +12,13 @@ container.classList.add("container")

//waiting image load
image.onload = () => {
console.log("Perché non va?");
let colorNumber = 5;


let t2 = Date.now();
editpix.getColorPaletteWasm2(image, colorNumber)
.then(colorPalette => {
let t3 = Date.now();
console.log(colorPalette)
displayPalette(colorPalette);
console.log("Ottimizzato: " + (t3-t2));
});
console.log("Image loaded.");


/*
let t2 = Date.now();
editpix.getColorPaletteWasm(image, colorNumber)
editpix.getColorPaletteWasm(image, 15, 2)
.then(colorPalette => {
let t3 = Date.now();
console.log(colorPalette)
displayPalette(colorPalette);
console.log("Originale: " + (t3-t2));
});
*/

};


Expand Down
35 changes: 20 additions & 15 deletions src/editpix.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import convertToBW from "./core/black_and_white.js";
import kMeans from "./core/kmean.js";
import imageManager from "./image_manager.js";
import higherColorContrast from "./core/higher_contrast.js";
import init, { k_means, k_means2 } from "./core/editpix_wasm.js"
import init, { k_means, k_means_pp } from "./core/editpix_wasm.js"
import optimizeContrast from "./core/optimize_contrast.js";
import changeContrast from "./core/change_contrast.js";
import changeTemperature from "./core/change_temperature.js";
Expand All @@ -27,20 +27,25 @@ EditPix.prototype.getColorPalette = (image, colorNumber = 5, quality = 1) => {
})
}

EditPix.prototype.getColorPaletteWasm = async (image, colorNumber = 5) => {
utils.validate(1, colorNumber);
const pixelArray = utils.removeAlphaSerialized(imageManager.getPixelArray(image));
await init();
let a = k_means(pixelArray, colorNumber, 100);
return utils.deserializeArray(a);
}

EditPix.prototype.getColorPaletteWasm2 = async (image, colorNumber = 5) => {
utils.validate(1, colorNumber);
const pixelArray = utils.removeAlphaSerialized(imageManager.getPixelArray(image));
await init();
let a = k_means2(pixelArray, colorNumber, 100);
return utils.deserializeArray(a);
EditPix.prototype.getColorPaletteWasm = async (image, colorNumber = 5, quality = 1, algorithm = "k-means++") => {
return new Promise((resolve, reject) => {
utils.validate(quality, colorNumber);
imageManager.resizeByPercentage(image, quality * 10)
.then(resizedImage => {
const pixelArray = utils.removeAlphaSerialized(imageManager.getPixelArray(resizedImage));
if (algorithm === "k-means") {
init().then(() => {
resolve(utils.deserializeArray(k_means(pixelArray, colorNumber, 100)));
})
} else if (algorithm === "k-means++") {
init().then(() => {
resolve(utils.deserializeArray(k_means_pp(pixelArray, colorNumber, 100)));
})
} else {
throw new Error("Non-existent algorithm.");
}
})
}).catch(error => { reject(error) })
}

EditPix.prototype.getDominantColor = function (image, quality = 1) {
Expand Down

0 comments on commit f2f934e

Please sign in to comment.