Skip to content

Commit

Permalink
refactor(pixel-dither): minor update
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 2, 2021
1 parent e7b3c4c commit 69b3028
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 deletions packages/pixel-dither/src/dither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ export const ditherWith = (
const chan = format.channels[cid];
const $thresh = chan.num * threshold;
const $max = chan.mask0;
const pixels = new Int32Array(cimg.pixels);
const data = new Int32Array(cimg.data);
for (let y = 0; y < height; y++) {
for (let x = 0, i = x + y * width; x < width; x++, i++) {
p = pixels[i] < $thresh ? 0 : $max;
err = (pixels[i] - p) * bleed;
pixels[i] = p;
p = data[i] < $thresh ? 0 : $max;
err = (data[i] - p) * bleed;
data[i] = p;
if (!err) continue;
for (let j = ox.length; j-- > 0; ) {
const xx = x + ox[j];
const yy = y + oy[j];
if (yy >= 0 && yy < height && xx >= 0 && xx < width) {
pixels[yy * width + xx] += (err * weights[j]) >> shift;
data[yy * width + xx] += (err * weights[j]) >> shift;
}
}
}
}
cimg.pixels.set(pixels);
cimg.data.set(data);
img.setChannel(cid, cimg);
}
return img;
Expand Down
8 changes: 4 additions & 4 deletions packages/pixel-dither/src/ordered.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ export const orderedDither = (
size: BayerSize | BayerMatrix,
numColors: number | number[]
) => {
const { pixels, format, width } = img;
const { data, format, width } = img;
const steps = isNumber(numColors)
? new Array<number>(format.channels.length).fill(numColors)
: numColors;
const mat = isNumber(size) ? defBayer(size) : size;
for (
let i = 0, n = pixels.length, nc = format.channels.length, x = 0, y = 0;
let i = 0, n = data.length, nc = format.channels.length, x = 0, y = 0;
i < n;
i++
) {
let col = pixels[i];
let col = data[i];
for (let j = 0; j < nc; j++) {
const ch = format.channels[j];
const num = ch.num;
Expand All @@ -113,7 +113,7 @@ export const orderedDither = (
orderedDither1(mat, cs, num, num, x, y, ch.int(col))
));
}
pixels[i] = col;
data[i] = col;
if (++x === width) {
x = 0;
y++;
Expand Down

0 comments on commit 69b3028

Please sign in to comment.