This commit is contained in:
Brooke Vibber 2022-11-05 18:24:01 -07:00
parent ed9538ecc5
commit 764ce9a481

View file

@ -321,7 +321,7 @@ function decimate(input, palette, n) {
// Apply dithering with given palette // Apply dithering with given palette
let dither = (palette) => { let dither = (palette) => {
let fitness = 0; let fitness = new Float64Array(line.length);
let error = { let error = {
right: new RGB(0, 0, 0), right: new RGB(0, 0, 0),
red: new Float64Array(line.length), red: new Float64Array(line.length),
@ -354,6 +354,13 @@ function decimate(input, palette, n) {
output[x] = pick; output[x] = pick;
popularity[pick]++; popularity[pick]++;
/*
// horiz only
error.right.r = nextError.r;
error.right.g = nextError.g;
error.right.b = nextError.b;
*/
error.right.r = nextError.r / 2; error.right.r = nextError.r / 2;
error.right.g = nextError.g / 2; error.right.g = nextError.g / 2;
error.right.b = nextError.b / 2; error.right.b = nextError.b / 2;
@ -376,9 +383,7 @@ function decimate(input, palette, n) {
error.green[x + 1] += nextError.g / 8; error.green[x + 1] += nextError.g / 8;
error.blue[x + 1] += nextError.b / 8; error.blue[x + 1] += nextError.b / 8;
//fitness += error.r; fitness[x] = 1 / nextError.magnitude();
//fitness += error.g;
//fitness += error.b;
} }
return { return {
output, output,
@ -392,7 +397,7 @@ function decimate(input, palette, n) {
let start = Date.now(); let start = Date.now();
let decimated = palette.slice(); let decimated = palette.slice();
while (decimated.length > n) { while (decimated.length > n) {
let {popularity} = dither(decimated); let {popularity, fitness, output} = dither(decimated);
// Try dropping least used color on each iteration // Try dropping least used color on each iteration
let least = Infinity; let least = Infinity;
@ -402,11 +407,19 @@ function decimate(input, palette, n) {
continue; // keep black always continue; // keep black always
} }
if (decimated[i].r == 255 && decimated[i].g == 255 && decimated[i].b == 255) { if (decimated[i].r == 255 && decimated[i].g == 255 && decimated[i].b == 255) {
continue; // keep white always //continue; // keep white always
} }
if (popularity[i] < least) { let coolFactor = popularity[i];
/*
for (let x = 0; x < line.length; x++) {
if (output[x] == i) {
coolFactor *= fitness[i];
}
}
*/
if (coolFactor < least) {
pick = i; pick = i;
least = popularity[i]; least = coolFactor;
} }
} }
let old = decimated.length; let old = decimated.length;