wip dither tweaks

This commit is contained in:
Brooke Vibber 2022-12-04 15:03:36 -08:00
parent ece161d6d6
commit e460fcefb0

View file

@ -78,14 +78,18 @@ class RGB {
return this; return this;
} }
add(other) { static add(a, b) {
return new RGB( return new RGB(
this.r + other.r, a.r + b.r,
this.g + other.g, a.g + b.g,
this.b + other.b a.b + b.b
); );
} }
add(other) {
return RGB.add(this, other);
}
difference(other) { difference(other) {
return new RGB( return new RGB(
this.r - other.r, this.r - other.r,
@ -407,10 +411,11 @@ function decimate(input, palette, n, inputError) {
let dither = (palette) => { let dither = (palette) => {
let fitness = new Float64Array(width); let fitness = new Float64Array(width);
let error = { let error = {
right: new RGB(0, 0, 0), cur: [],
next: [], next: [],
}; };
for (let i = 0; i < width; i++) { for (let i = 0; i < width; i++) {
error.cur[i] = new RGB(0, 0, 0);
error.next[i] = new RGB(0, 0, 0); error.next[i] = new RGB(0, 0, 0);
} }
@ -421,8 +426,7 @@ function decimate(input, palette, n, inputError) {
// Try dithering with this palette. // Try dithering with this palette.
for (let x = 0; x < width; x++) { for (let x = 0; x < width; x++) {
let rgb = input[x]; let rgb = RGB.add(input[x], error.cur[x]);
rgb = rgb.add(error.right);
if (inputError) { if (inputError) {
rgb.inc(inputError[x]); rgb.inc(inputError[x]);
} }
@ -445,18 +449,14 @@ function decimate(input, palette, n, inputError) {
output[x] = pick; output[x] = pick;
popularity[pick]++; popularity[pick]++;
if (x == width - 1) { let shares = 8;
let half = nextError.divide(4); let single = nextError.divide(shares);
error.next[x - 1].inc(half); let double = nextError.divide(shares / 2);
error.next[x].inc(half); error.cur[x + 1]?.inc(double);
// drop half the error on the floor at the edge ;_; error.cur[x + 2]?.inc(single);
} else { error.next[x - 1]?.inc(double);
let quarter = nextError.divide(4); error.next[x]?.inc(double);
error.right = quarter; error.next[x + 1]?.inc(single);
error.next[x - 1]?.inc(quarter); // @fixme should we change the amount?
error.next[x].inc(quarter);
error.next[x + 1].inc(quarter);
}
// 442 is the 3d distance across the rgb cube // 442 is the 3d distance across the rgb cube
//fitness[x] = 442 - (nextError.magnitude()); //fitness[x] = 442 - (nextError.magnitude());