Skip to content

Commit

Permalink
Fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zk-phi committed Jun 24, 2024
1 parent 4e46d7d commit eb26420
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/utils/binarize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ export const binarizeTransparency = (
data: Uint8ClampedArray,
width: number,
height: number,
) => {
): void => {
const err: number[] = new Array(width * height);
for (let y = 0; y < height; y++) {
for (let y = 0; y < height; y += 1) {
if (y % 2 === 0) { // iterate from left to right
for (let x = 0; x < width; x++) {
for (let x = 0; x < width; x += 1) {
const index = y * width + x;
const value = data[index * 4 + 3] + (err[index] ?? 0);
let newErr = 0;
/* eslint-disable no-param-reassign */
if (value < THRESHOLD) {
data[index * 4 + 0] = 0;
data[index * 4 + 1] = 0;
Expand All @@ -27,19 +28,21 @@ export const binarizeTransparency = (
data[index * 4 + 3] = 255;
newErr = value - 255;
}
/* eslint-enable no-param-reassign */
// Floyd-steinberg dithering
if (x + 1 < width) err[y * width + (x + 1)] = newErr * 7/16;
if (x + 1 < width) err[y * width + (x + 1)] = newErr * 7 / 16;
if (y + 1 < height) {
if (x - 1 >= 0) err[(y + 1) * width + (x - 1)] = newErr * 3/16;
err[(y + 1) * width + x] = newErr * 5/16;
if (x + 1 < width) err[(y + 1) * width + (x + 1)] = newErr * 1/16;
if (x - 1 >= 0) err[(y + 1) * width + (x - 1)] = newErr * 3 / 16;
err[(y + 1) * width + x] = newErr * 5 / 16;
if (x + 1 < width) err[(y + 1) * width + (x + 1)] = newErr * 1 / 16;
}
}
} else { // iterate from right to left
for (let x = width - 1; x >= 0; x--) {
for (let x = width - 1; x >= 0; x -= 1) {
const index = y * width + x;
const value = data[index * 4 + 3] + (err[index] ?? 0);
let newErr = 0;
/* eslint-disable no-param-reassign */
if (value < THRESHOLD) {
data[index * 4 + 0] = 0;
data[index * 4 + 1] = 0;
Expand All @@ -50,12 +53,13 @@ export const binarizeTransparency = (
data[index * 4 + 3] = 255;
newErr = value - 255;
}
/* eslint-enable no-param-reassign */
// Floyd-steinberg dithering
if (x - 1 >= 0) err[y * width + (x - 1)] = newErr * 7/16;
if (x - 1 >= 0) err[y * width + (x - 1)] = newErr * 7 / 16;
if (y + 1 < height) {
if (x + 1 < width) err[(y + 1) * width + (x + 1)] = newErr * 3/16;
err[(y + 1) * width + x] = newErr * 5/16;
if (x - 1 >= 0) err[(y + 1) * width + (x - 1)] = newErr * 1/16;
if (x + 1 < width) err[(y + 1) * width + (x + 1)] = newErr * 3 / 16;
err[(y + 1) * width + x] = newErr * 5 / 16;
if (x - 1 >= 0) err[(y + 1) * width + (x - 1)] = newErr * 1 / 16;
}
}
}
Expand Down

0 comments on commit eb26420

Please sign in to comment.