This commit is contained in:
Brooke Vibber 2023-03-18 20:17:01 -07:00
parent 7c4b118bed
commit 11325a16d8

View file

@ -485,6 +485,15 @@ function decimate(input, palette, n, inputError) {
// force to rWb // force to rWb
//decimated = [0, 0x36, 0x0f, 0x86]; //decimated = [0, 0x36, 0x0f, 0x86];
let reserved = [0]; // black
//let reserved = [0, 15]; // black, white
//let reserved = [0, 5, 10, 15]; // grayscale
let keepers = new Uint8Array(256);
for (let i of reserved) {
keepers[i & 0xfe] = 1; // drop that 0 luminance bit!
}
while (decimated.length > n) { while (decimated.length > n) {
let {popularity, fitness, output} = dither(decimated); let {popularity, fitness, output} = dither(decimated);
@ -492,7 +501,11 @@ function decimate(input, palette, n, inputError) {
// Try dropping least used color on each iteration // Try dropping least used color on each iteration
let least = Infinity; let least = Infinity;
let pick = -1; let pick = -1;
for (let i = 1; i < decimated.length; i++) { for (let i = 0; i < decimated.length; i++) {
let color = decimated[i];
if (keepers[color]) {
continue;
}
//let coolFactor = popularity[i]; //let coolFactor = popularity[i];
@ -513,22 +526,22 @@ function decimate(input, palette, n, inputError) {
least = coolFactor; least = coolFactor;
} }
} }
let last = decimated.length;
decimated = decimated.filter((color, i) => { decimated = decimated.filter((color, i) => {
if (palette[i] == 0) { if (keepers[color]) {
// solid black
return true; return true;
} }
if (i == pick) { if (i == pick) {
return false; return false;
} }
// Also drop any non-black unused colors to save trouble.
// However -- this may change dither results.
// Try this with/without later.
if (popularity[i] == 0) {
return false;
}
return true; return true;
}); });
if (decimated.length == last && last > n) {
console.log(decimated);
throw new Error('this should not happen');
}
} }
// Palette fits // Palette fits