From 764ce9a481fbda8dc959fb80b41f1319341c7a42 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sat, 5 Nov 2022 18:24:01 -0700 Subject: [PATCH] wip --- dither4.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/dither4.js b/dither4.js index f0f38e5..9d40627 100644 --- a/dither4.js +++ b/dither4.js @@ -321,7 +321,7 @@ function decimate(input, palette, n) { // Apply dithering with given palette let dither = (palette) => { - let fitness = 0; + let fitness = new Float64Array(line.length); let error = { right: new RGB(0, 0, 0), red: new Float64Array(line.length), @@ -354,6 +354,13 @@ function decimate(input, palette, n) { output[x] = 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.g = nextError.g / 2; error.right.b = nextError.b / 2; @@ -376,9 +383,7 @@ function decimate(input, palette, n) { error.green[x + 1] += nextError.g / 8; error.blue[x + 1] += nextError.b / 8; - //fitness += error.r; - //fitness += error.g; - //fitness += error.b; + fitness[x] = 1 / nextError.magnitude(); } return { output, @@ -392,7 +397,7 @@ function decimate(input, palette, n) { let start = Date.now(); let decimated = palette.slice(); while (decimated.length > n) { - let {popularity} = dither(decimated); + let {popularity, fitness, output} = dither(decimated); // Try dropping least used color on each iteration let least = Infinity; @@ -402,11 +407,19 @@ function decimate(input, palette, n) { continue; // keep black always } 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; - least = popularity[i]; + least = coolFactor; } } let old = decimated.length;