emf-converter 4.6.4 → 4.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.js +66 -31
- package/dist/browser.mjs +66 -31
- package/dist/index.js +66 -31
- package/dist/index.mjs +66 -31
- package/package.json +1 -1
package/dist/browser.js
CHANGED
|
@@ -31372,42 +31372,77 @@ function halftoneDespeckle(px, w, h) {
|
|
|
31372
31372
|
const src = px.slice();
|
|
31373
31373
|
const at = (x, y, c) => src[(y * w + x) * 3 + c];
|
|
31374
31374
|
const over = (v) => v < -SPECKLE_OVERSHOOT * 4 || v > (255 + SPECKLE_OVERSHOOT) * 4;
|
|
31375
|
+
const loneX = w > 2 && w % 2 === 1 ? w - 1 : -1;
|
|
31376
|
+
const loneY = h > 2 && h % 2 === 1 ? h - 1 : -1;
|
|
31377
|
+
const mirror2 = (i, n, d) => {
|
|
31378
|
+
const j = i + d;
|
|
31379
|
+
return j < 0 || j >= n ? i - d : j;
|
|
31380
|
+
};
|
|
31381
|
+
const vBlur = (x, y, c) => at(x, mirror2(y, h, -1), c) + 2 * at(x, y, c) + at(x, mirror2(y, h, 1), c) + 2 >> 2;
|
|
31382
|
+
const hBlur = (x, y, c) => at(mirror2(x, w, -1), y, c) + 2 * at(x, y, c) + at(mirror2(x, w, 1), y, c) + 2 >> 2;
|
|
31383
|
+
const differs = (x0, y0, x1, y1) => at(x0, y0, 0) !== at(x1, y1, 0) || at(x0, y0, 1) !== at(x1, y1, 1) || at(x0, y0, 2) !== at(x1, y1, 2);
|
|
31384
|
+
const loneColumn = (y) => loneX > 0 && differs(loneX, y, loneX - 1, y);
|
|
31385
|
+
const loneRow = (x) => loneY > 0 && differs(x, loneY, x, loneY - 1);
|
|
31386
|
+
const loneColumnValue = (y, c) => y === loneY && loneRow(loneX) ? at(loneX, y - 1, c) + 3 * at(loneX, y, c) + 2 >> 2 : vBlur(loneX, y, c);
|
|
31387
|
+
const loneRowValue = (x, c) => x === loneX && loneColumn(loneY) ? at(x - 1, loneY, c) + 3 * at(x, loneY, c) + 2 >> 2 : hBlur(x, loneY, c);
|
|
31388
|
+
const speckled = (x, y) => {
|
|
31389
|
+
const up = mirror2(y, h, -1);
|
|
31390
|
+
const down = mirror2(y, h, 1);
|
|
31391
|
+
const left = mirror2(x, w, -1);
|
|
31392
|
+
const right = mirror2(x, w, 1);
|
|
31393
|
+
let hClip = 0;
|
|
31394
|
+
let vClip = 0;
|
|
31395
|
+
let alternating = 0;
|
|
31396
|
+
for (let c = 0; c < 3; c++) {
|
|
31397
|
+
const v = at(x, y, c);
|
|
31398
|
+
const axial = Math.max(
|
|
31399
|
+
Math.abs(v - at(left, y, c)),
|
|
31400
|
+
Math.abs(v - at(right, y, c)),
|
|
31401
|
+
Math.abs(v - at(x, up, c)),
|
|
31402
|
+
Math.abs(v - at(x, down, c))
|
|
31403
|
+
);
|
|
31404
|
+
const diagonal = Math.min(
|
|
31405
|
+
Math.abs(v - at(left, up, c)),
|
|
31406
|
+
Math.abs(v - at(right, up, c)),
|
|
31407
|
+
Math.abs(v - at(left, down, c)),
|
|
31408
|
+
Math.abs(v - at(right, down, c))
|
|
31409
|
+
);
|
|
31410
|
+
if (diagonal < axial) alternating++;
|
|
31411
|
+
if (over(6 * v - at(left, y, c) - at(right, y, c))) {
|
|
31412
|
+
hClip++;
|
|
31413
|
+
}
|
|
31414
|
+
if (over(6 * v - at(x, up, c) - at(x, down, c))) {
|
|
31415
|
+
vClip++;
|
|
31416
|
+
}
|
|
31417
|
+
}
|
|
31418
|
+
return hClip >= 2 && vClip >= 2 && alternating >= 2;
|
|
31419
|
+
};
|
|
31375
31420
|
for (let y = 0; y < h; y++) {
|
|
31376
|
-
const up = y > 0 ? y - 1 : Math.min(h - 1, y + 1);
|
|
31377
|
-
const down = y < h - 1 ? y + 1 : Math.max(0, y - 1);
|
|
31378
31421
|
for (let x = 0; x < w; x++) {
|
|
31379
|
-
|
|
31380
|
-
const right = x < w - 1 ? x + 1 : Math.max(0, x - 1);
|
|
31381
|
-
let hClip = 0;
|
|
31382
|
-
let vClip = 0;
|
|
31383
|
-
let alternating = 0;
|
|
31384
|
-
for (let c = 0; c < 3; c++) {
|
|
31385
|
-
const v = at(x, y, c);
|
|
31386
|
-
const axial = Math.max(
|
|
31387
|
-
Math.abs(v - at(left, y, c)),
|
|
31388
|
-
Math.abs(v - at(right, y, c)),
|
|
31389
|
-
Math.abs(v - at(x, up, c)),
|
|
31390
|
-
Math.abs(v - at(x, down, c))
|
|
31391
|
-
);
|
|
31392
|
-
const diagonal = Math.min(
|
|
31393
|
-
Math.abs(v - at(left, up, c)),
|
|
31394
|
-
Math.abs(v - at(right, up, c)),
|
|
31395
|
-
Math.abs(v - at(left, down, c)),
|
|
31396
|
-
Math.abs(v - at(right, down, c))
|
|
31397
|
-
);
|
|
31398
|
-
if (diagonal < axial) alternating++;
|
|
31399
|
-
if (over(6 * v - at(left, y, c) - at(right, y, c))) {
|
|
31400
|
-
hClip++;
|
|
31401
|
-
}
|
|
31402
|
-
if (over(6 * v - at(x, up, c) - at(x, down, c))) {
|
|
31403
|
-
vClip++;
|
|
31404
|
-
}
|
|
31405
|
-
}
|
|
31406
|
-
if (hClip < 2 || vClip < 2 || alternating < 2) {
|
|
31422
|
+
if (!speckled(x, y)) {
|
|
31407
31423
|
continue;
|
|
31408
31424
|
}
|
|
31425
|
+
const left = mirror2(x, w, -1);
|
|
31426
|
+
const right = mirror2(x, w, 1);
|
|
31427
|
+
const up = mirror2(y, h, -1);
|
|
31428
|
+
const down = mirror2(y, h, 1);
|
|
31429
|
+
const lonelyColumn = x === loneX && loneColumn(y);
|
|
31430
|
+
const lonelyRow = y === loneY && loneRow(x);
|
|
31431
|
+
const nearLoneX = x === loneX - 1 && loneX > 0 && loneColumn(y) && !speckled(loneX, y);
|
|
31432
|
+
const nearLoneY = y === loneY - 1 && loneY > 0 && loneRow(x) && !speckled(x, loneY);
|
|
31433
|
+
const cornerRepeats = lonelyColumn && lonelyRow && (!speckled(x, y - 1) || !speckled(x - 1, y));
|
|
31409
31434
|
for (let c = 0; c < 3; c++) {
|
|
31410
|
-
|
|
31435
|
+
const v = at(x, y, c);
|
|
31436
|
+
let r;
|
|
31437
|
+
if (lonelyColumn && lonelyRow) {
|
|
31438
|
+
r = cornerRepeats ? at(left, y, c) + 3 * v + 2 >> 2 : hBlur(x, y, c);
|
|
31439
|
+
} else {
|
|
31440
|
+
let sum = lonelyColumn ? at(x, up, c) + 2 * v + at(x, down, c) : at(left, y, c) + 2 * v + at(right, y, c);
|
|
31441
|
+
if (nearLoneX) sum += loneColumnValue(y, c) - at(loneX, y, c);
|
|
31442
|
+
if (nearLoneY) sum += loneRowValue(x, c) - at(x, loneY, c);
|
|
31443
|
+
r = sum + 2 >> 2;
|
|
31444
|
+
}
|
|
31445
|
+
px[(y * w + x) * 3 + c] = r;
|
|
31411
31446
|
}
|
|
31412
31447
|
}
|
|
31413
31448
|
}
|
package/dist/browser.mjs
CHANGED
|
@@ -31370,42 +31370,77 @@ function halftoneDespeckle(px, w, h) {
|
|
|
31370
31370
|
const src = px.slice();
|
|
31371
31371
|
const at = (x, y, c) => src[(y * w + x) * 3 + c];
|
|
31372
31372
|
const over = (v) => v < -SPECKLE_OVERSHOOT * 4 || v > (255 + SPECKLE_OVERSHOOT) * 4;
|
|
31373
|
+
const loneX = w > 2 && w % 2 === 1 ? w - 1 : -1;
|
|
31374
|
+
const loneY = h > 2 && h % 2 === 1 ? h - 1 : -1;
|
|
31375
|
+
const mirror2 = (i, n, d) => {
|
|
31376
|
+
const j = i + d;
|
|
31377
|
+
return j < 0 || j >= n ? i - d : j;
|
|
31378
|
+
};
|
|
31379
|
+
const vBlur = (x, y, c) => at(x, mirror2(y, h, -1), c) + 2 * at(x, y, c) + at(x, mirror2(y, h, 1), c) + 2 >> 2;
|
|
31380
|
+
const hBlur = (x, y, c) => at(mirror2(x, w, -1), y, c) + 2 * at(x, y, c) + at(mirror2(x, w, 1), y, c) + 2 >> 2;
|
|
31381
|
+
const differs = (x0, y0, x1, y1) => at(x0, y0, 0) !== at(x1, y1, 0) || at(x0, y0, 1) !== at(x1, y1, 1) || at(x0, y0, 2) !== at(x1, y1, 2);
|
|
31382
|
+
const loneColumn = (y) => loneX > 0 && differs(loneX, y, loneX - 1, y);
|
|
31383
|
+
const loneRow = (x) => loneY > 0 && differs(x, loneY, x, loneY - 1);
|
|
31384
|
+
const loneColumnValue = (y, c) => y === loneY && loneRow(loneX) ? at(loneX, y - 1, c) + 3 * at(loneX, y, c) + 2 >> 2 : vBlur(loneX, y, c);
|
|
31385
|
+
const loneRowValue = (x, c) => x === loneX && loneColumn(loneY) ? at(x - 1, loneY, c) + 3 * at(x, loneY, c) + 2 >> 2 : hBlur(x, loneY, c);
|
|
31386
|
+
const speckled = (x, y) => {
|
|
31387
|
+
const up = mirror2(y, h, -1);
|
|
31388
|
+
const down = mirror2(y, h, 1);
|
|
31389
|
+
const left = mirror2(x, w, -1);
|
|
31390
|
+
const right = mirror2(x, w, 1);
|
|
31391
|
+
let hClip = 0;
|
|
31392
|
+
let vClip = 0;
|
|
31393
|
+
let alternating = 0;
|
|
31394
|
+
for (let c = 0; c < 3; c++) {
|
|
31395
|
+
const v = at(x, y, c);
|
|
31396
|
+
const axial = Math.max(
|
|
31397
|
+
Math.abs(v - at(left, y, c)),
|
|
31398
|
+
Math.abs(v - at(right, y, c)),
|
|
31399
|
+
Math.abs(v - at(x, up, c)),
|
|
31400
|
+
Math.abs(v - at(x, down, c))
|
|
31401
|
+
);
|
|
31402
|
+
const diagonal = Math.min(
|
|
31403
|
+
Math.abs(v - at(left, up, c)),
|
|
31404
|
+
Math.abs(v - at(right, up, c)),
|
|
31405
|
+
Math.abs(v - at(left, down, c)),
|
|
31406
|
+
Math.abs(v - at(right, down, c))
|
|
31407
|
+
);
|
|
31408
|
+
if (diagonal < axial) alternating++;
|
|
31409
|
+
if (over(6 * v - at(left, y, c) - at(right, y, c))) {
|
|
31410
|
+
hClip++;
|
|
31411
|
+
}
|
|
31412
|
+
if (over(6 * v - at(x, up, c) - at(x, down, c))) {
|
|
31413
|
+
vClip++;
|
|
31414
|
+
}
|
|
31415
|
+
}
|
|
31416
|
+
return hClip >= 2 && vClip >= 2 && alternating >= 2;
|
|
31417
|
+
};
|
|
31373
31418
|
for (let y = 0; y < h; y++) {
|
|
31374
|
-
const up = y > 0 ? y - 1 : Math.min(h - 1, y + 1);
|
|
31375
|
-
const down = y < h - 1 ? y + 1 : Math.max(0, y - 1);
|
|
31376
31419
|
for (let x = 0; x < w; x++) {
|
|
31377
|
-
|
|
31378
|
-
const right = x < w - 1 ? x + 1 : Math.max(0, x - 1);
|
|
31379
|
-
let hClip = 0;
|
|
31380
|
-
let vClip = 0;
|
|
31381
|
-
let alternating = 0;
|
|
31382
|
-
for (let c = 0; c < 3; c++) {
|
|
31383
|
-
const v = at(x, y, c);
|
|
31384
|
-
const axial = Math.max(
|
|
31385
|
-
Math.abs(v - at(left, y, c)),
|
|
31386
|
-
Math.abs(v - at(right, y, c)),
|
|
31387
|
-
Math.abs(v - at(x, up, c)),
|
|
31388
|
-
Math.abs(v - at(x, down, c))
|
|
31389
|
-
);
|
|
31390
|
-
const diagonal = Math.min(
|
|
31391
|
-
Math.abs(v - at(left, up, c)),
|
|
31392
|
-
Math.abs(v - at(right, up, c)),
|
|
31393
|
-
Math.abs(v - at(left, down, c)),
|
|
31394
|
-
Math.abs(v - at(right, down, c))
|
|
31395
|
-
);
|
|
31396
|
-
if (diagonal < axial) alternating++;
|
|
31397
|
-
if (over(6 * v - at(left, y, c) - at(right, y, c))) {
|
|
31398
|
-
hClip++;
|
|
31399
|
-
}
|
|
31400
|
-
if (over(6 * v - at(x, up, c) - at(x, down, c))) {
|
|
31401
|
-
vClip++;
|
|
31402
|
-
}
|
|
31403
|
-
}
|
|
31404
|
-
if (hClip < 2 || vClip < 2 || alternating < 2) {
|
|
31420
|
+
if (!speckled(x, y)) {
|
|
31405
31421
|
continue;
|
|
31406
31422
|
}
|
|
31423
|
+
const left = mirror2(x, w, -1);
|
|
31424
|
+
const right = mirror2(x, w, 1);
|
|
31425
|
+
const up = mirror2(y, h, -1);
|
|
31426
|
+
const down = mirror2(y, h, 1);
|
|
31427
|
+
const lonelyColumn = x === loneX && loneColumn(y);
|
|
31428
|
+
const lonelyRow = y === loneY && loneRow(x);
|
|
31429
|
+
const nearLoneX = x === loneX - 1 && loneX > 0 && loneColumn(y) && !speckled(loneX, y);
|
|
31430
|
+
const nearLoneY = y === loneY - 1 && loneY > 0 && loneRow(x) && !speckled(x, loneY);
|
|
31431
|
+
const cornerRepeats = lonelyColumn && lonelyRow && (!speckled(x, y - 1) || !speckled(x - 1, y));
|
|
31407
31432
|
for (let c = 0; c < 3; c++) {
|
|
31408
|
-
|
|
31433
|
+
const v = at(x, y, c);
|
|
31434
|
+
let r;
|
|
31435
|
+
if (lonelyColumn && lonelyRow) {
|
|
31436
|
+
r = cornerRepeats ? at(left, y, c) + 3 * v + 2 >> 2 : hBlur(x, y, c);
|
|
31437
|
+
} else {
|
|
31438
|
+
let sum = lonelyColumn ? at(x, up, c) + 2 * v + at(x, down, c) : at(left, y, c) + 2 * v + at(right, y, c);
|
|
31439
|
+
if (nearLoneX) sum += loneColumnValue(y, c) - at(loneX, y, c);
|
|
31440
|
+
if (nearLoneY) sum += loneRowValue(x, c) - at(x, loneY, c);
|
|
31441
|
+
r = sum + 2 >> 2;
|
|
31442
|
+
}
|
|
31443
|
+
px[(y * w + x) * 3 + c] = r;
|
|
31409
31444
|
}
|
|
31410
31445
|
}
|
|
31411
31446
|
}
|
package/dist/index.js
CHANGED
|
@@ -31373,42 +31373,77 @@ function halftoneDespeckle(px, w, h) {
|
|
|
31373
31373
|
const src = px.slice();
|
|
31374
31374
|
const at = (x, y, c) => src[(y * w + x) * 3 + c];
|
|
31375
31375
|
const over = (v) => v < -SPECKLE_OVERSHOOT * 4 || v > (255 + SPECKLE_OVERSHOOT) * 4;
|
|
31376
|
+
const loneX = w > 2 && w % 2 === 1 ? w - 1 : -1;
|
|
31377
|
+
const loneY = h > 2 && h % 2 === 1 ? h - 1 : -1;
|
|
31378
|
+
const mirror2 = (i, n, d) => {
|
|
31379
|
+
const j = i + d;
|
|
31380
|
+
return j < 0 || j >= n ? i - d : j;
|
|
31381
|
+
};
|
|
31382
|
+
const vBlur = (x, y, c) => at(x, mirror2(y, h, -1), c) + 2 * at(x, y, c) + at(x, mirror2(y, h, 1), c) + 2 >> 2;
|
|
31383
|
+
const hBlur = (x, y, c) => at(mirror2(x, w, -1), y, c) + 2 * at(x, y, c) + at(mirror2(x, w, 1), y, c) + 2 >> 2;
|
|
31384
|
+
const differs = (x0, y0, x1, y1) => at(x0, y0, 0) !== at(x1, y1, 0) || at(x0, y0, 1) !== at(x1, y1, 1) || at(x0, y0, 2) !== at(x1, y1, 2);
|
|
31385
|
+
const loneColumn = (y) => loneX > 0 && differs(loneX, y, loneX - 1, y);
|
|
31386
|
+
const loneRow = (x) => loneY > 0 && differs(x, loneY, x, loneY - 1);
|
|
31387
|
+
const loneColumnValue = (y, c) => y === loneY && loneRow(loneX) ? at(loneX, y - 1, c) + 3 * at(loneX, y, c) + 2 >> 2 : vBlur(loneX, y, c);
|
|
31388
|
+
const loneRowValue = (x, c) => x === loneX && loneColumn(loneY) ? at(x - 1, loneY, c) + 3 * at(x, loneY, c) + 2 >> 2 : hBlur(x, loneY, c);
|
|
31389
|
+
const speckled = (x, y) => {
|
|
31390
|
+
const up = mirror2(y, h, -1);
|
|
31391
|
+
const down = mirror2(y, h, 1);
|
|
31392
|
+
const left = mirror2(x, w, -1);
|
|
31393
|
+
const right = mirror2(x, w, 1);
|
|
31394
|
+
let hClip = 0;
|
|
31395
|
+
let vClip = 0;
|
|
31396
|
+
let alternating = 0;
|
|
31397
|
+
for (let c = 0; c < 3; c++) {
|
|
31398
|
+
const v = at(x, y, c);
|
|
31399
|
+
const axial = Math.max(
|
|
31400
|
+
Math.abs(v - at(left, y, c)),
|
|
31401
|
+
Math.abs(v - at(right, y, c)),
|
|
31402
|
+
Math.abs(v - at(x, up, c)),
|
|
31403
|
+
Math.abs(v - at(x, down, c))
|
|
31404
|
+
);
|
|
31405
|
+
const diagonal = Math.min(
|
|
31406
|
+
Math.abs(v - at(left, up, c)),
|
|
31407
|
+
Math.abs(v - at(right, up, c)),
|
|
31408
|
+
Math.abs(v - at(left, down, c)),
|
|
31409
|
+
Math.abs(v - at(right, down, c))
|
|
31410
|
+
);
|
|
31411
|
+
if (diagonal < axial) alternating++;
|
|
31412
|
+
if (over(6 * v - at(left, y, c) - at(right, y, c))) {
|
|
31413
|
+
hClip++;
|
|
31414
|
+
}
|
|
31415
|
+
if (over(6 * v - at(x, up, c) - at(x, down, c))) {
|
|
31416
|
+
vClip++;
|
|
31417
|
+
}
|
|
31418
|
+
}
|
|
31419
|
+
return hClip >= 2 && vClip >= 2 && alternating >= 2;
|
|
31420
|
+
};
|
|
31376
31421
|
for (let y = 0; y < h; y++) {
|
|
31377
|
-
const up = y > 0 ? y - 1 : Math.min(h - 1, y + 1);
|
|
31378
|
-
const down = y < h - 1 ? y + 1 : Math.max(0, y - 1);
|
|
31379
31422
|
for (let x = 0; x < w; x++) {
|
|
31380
|
-
|
|
31381
|
-
const right = x < w - 1 ? x + 1 : Math.max(0, x - 1);
|
|
31382
|
-
let hClip = 0;
|
|
31383
|
-
let vClip = 0;
|
|
31384
|
-
let alternating = 0;
|
|
31385
|
-
for (let c = 0; c < 3; c++) {
|
|
31386
|
-
const v = at(x, y, c);
|
|
31387
|
-
const axial = Math.max(
|
|
31388
|
-
Math.abs(v - at(left, y, c)),
|
|
31389
|
-
Math.abs(v - at(right, y, c)),
|
|
31390
|
-
Math.abs(v - at(x, up, c)),
|
|
31391
|
-
Math.abs(v - at(x, down, c))
|
|
31392
|
-
);
|
|
31393
|
-
const diagonal = Math.min(
|
|
31394
|
-
Math.abs(v - at(left, up, c)),
|
|
31395
|
-
Math.abs(v - at(right, up, c)),
|
|
31396
|
-
Math.abs(v - at(left, down, c)),
|
|
31397
|
-
Math.abs(v - at(right, down, c))
|
|
31398
|
-
);
|
|
31399
|
-
if (diagonal < axial) alternating++;
|
|
31400
|
-
if (over(6 * v - at(left, y, c) - at(right, y, c))) {
|
|
31401
|
-
hClip++;
|
|
31402
|
-
}
|
|
31403
|
-
if (over(6 * v - at(x, up, c) - at(x, down, c))) {
|
|
31404
|
-
vClip++;
|
|
31405
|
-
}
|
|
31406
|
-
}
|
|
31407
|
-
if (hClip < 2 || vClip < 2 || alternating < 2) {
|
|
31423
|
+
if (!speckled(x, y)) {
|
|
31408
31424
|
continue;
|
|
31409
31425
|
}
|
|
31426
|
+
const left = mirror2(x, w, -1);
|
|
31427
|
+
const right = mirror2(x, w, 1);
|
|
31428
|
+
const up = mirror2(y, h, -1);
|
|
31429
|
+
const down = mirror2(y, h, 1);
|
|
31430
|
+
const lonelyColumn = x === loneX && loneColumn(y);
|
|
31431
|
+
const lonelyRow = y === loneY && loneRow(x);
|
|
31432
|
+
const nearLoneX = x === loneX - 1 && loneX > 0 && loneColumn(y) && !speckled(loneX, y);
|
|
31433
|
+
const nearLoneY = y === loneY - 1 && loneY > 0 && loneRow(x) && !speckled(x, loneY);
|
|
31434
|
+
const cornerRepeats = lonelyColumn && lonelyRow && (!speckled(x, y - 1) || !speckled(x - 1, y));
|
|
31410
31435
|
for (let c = 0; c < 3; c++) {
|
|
31411
|
-
|
|
31436
|
+
const v = at(x, y, c);
|
|
31437
|
+
let r;
|
|
31438
|
+
if (lonelyColumn && lonelyRow) {
|
|
31439
|
+
r = cornerRepeats ? at(left, y, c) + 3 * v + 2 >> 2 : hBlur(x, y, c);
|
|
31440
|
+
} else {
|
|
31441
|
+
let sum = lonelyColumn ? at(x, up, c) + 2 * v + at(x, down, c) : at(left, y, c) + 2 * v + at(right, y, c);
|
|
31442
|
+
if (nearLoneX) sum += loneColumnValue(y, c) - at(loneX, y, c);
|
|
31443
|
+
if (nearLoneY) sum += loneRowValue(x, c) - at(x, loneY, c);
|
|
31444
|
+
r = sum + 2 >> 2;
|
|
31445
|
+
}
|
|
31446
|
+
px[(y * w + x) * 3 + c] = r;
|
|
31412
31447
|
}
|
|
31413
31448
|
}
|
|
31414
31449
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -31371,42 +31371,77 @@ function halftoneDespeckle(px, w, h) {
|
|
|
31371
31371
|
const src = px.slice();
|
|
31372
31372
|
const at = (x, y, c) => src[(y * w + x) * 3 + c];
|
|
31373
31373
|
const over = (v) => v < -SPECKLE_OVERSHOOT * 4 || v > (255 + SPECKLE_OVERSHOOT) * 4;
|
|
31374
|
+
const loneX = w > 2 && w % 2 === 1 ? w - 1 : -1;
|
|
31375
|
+
const loneY = h > 2 && h % 2 === 1 ? h - 1 : -1;
|
|
31376
|
+
const mirror2 = (i, n, d) => {
|
|
31377
|
+
const j = i + d;
|
|
31378
|
+
return j < 0 || j >= n ? i - d : j;
|
|
31379
|
+
};
|
|
31380
|
+
const vBlur = (x, y, c) => at(x, mirror2(y, h, -1), c) + 2 * at(x, y, c) + at(x, mirror2(y, h, 1), c) + 2 >> 2;
|
|
31381
|
+
const hBlur = (x, y, c) => at(mirror2(x, w, -1), y, c) + 2 * at(x, y, c) + at(mirror2(x, w, 1), y, c) + 2 >> 2;
|
|
31382
|
+
const differs = (x0, y0, x1, y1) => at(x0, y0, 0) !== at(x1, y1, 0) || at(x0, y0, 1) !== at(x1, y1, 1) || at(x0, y0, 2) !== at(x1, y1, 2);
|
|
31383
|
+
const loneColumn = (y) => loneX > 0 && differs(loneX, y, loneX - 1, y);
|
|
31384
|
+
const loneRow = (x) => loneY > 0 && differs(x, loneY, x, loneY - 1);
|
|
31385
|
+
const loneColumnValue = (y, c) => y === loneY && loneRow(loneX) ? at(loneX, y - 1, c) + 3 * at(loneX, y, c) + 2 >> 2 : vBlur(loneX, y, c);
|
|
31386
|
+
const loneRowValue = (x, c) => x === loneX && loneColumn(loneY) ? at(x - 1, loneY, c) + 3 * at(x, loneY, c) + 2 >> 2 : hBlur(x, loneY, c);
|
|
31387
|
+
const speckled = (x, y) => {
|
|
31388
|
+
const up = mirror2(y, h, -1);
|
|
31389
|
+
const down = mirror2(y, h, 1);
|
|
31390
|
+
const left = mirror2(x, w, -1);
|
|
31391
|
+
const right = mirror2(x, w, 1);
|
|
31392
|
+
let hClip = 0;
|
|
31393
|
+
let vClip = 0;
|
|
31394
|
+
let alternating = 0;
|
|
31395
|
+
for (let c = 0; c < 3; c++) {
|
|
31396
|
+
const v = at(x, y, c);
|
|
31397
|
+
const axial = Math.max(
|
|
31398
|
+
Math.abs(v - at(left, y, c)),
|
|
31399
|
+
Math.abs(v - at(right, y, c)),
|
|
31400
|
+
Math.abs(v - at(x, up, c)),
|
|
31401
|
+
Math.abs(v - at(x, down, c))
|
|
31402
|
+
);
|
|
31403
|
+
const diagonal = Math.min(
|
|
31404
|
+
Math.abs(v - at(left, up, c)),
|
|
31405
|
+
Math.abs(v - at(right, up, c)),
|
|
31406
|
+
Math.abs(v - at(left, down, c)),
|
|
31407
|
+
Math.abs(v - at(right, down, c))
|
|
31408
|
+
);
|
|
31409
|
+
if (diagonal < axial) alternating++;
|
|
31410
|
+
if (over(6 * v - at(left, y, c) - at(right, y, c))) {
|
|
31411
|
+
hClip++;
|
|
31412
|
+
}
|
|
31413
|
+
if (over(6 * v - at(x, up, c) - at(x, down, c))) {
|
|
31414
|
+
vClip++;
|
|
31415
|
+
}
|
|
31416
|
+
}
|
|
31417
|
+
return hClip >= 2 && vClip >= 2 && alternating >= 2;
|
|
31418
|
+
};
|
|
31374
31419
|
for (let y = 0; y < h; y++) {
|
|
31375
|
-
const up = y > 0 ? y - 1 : Math.min(h - 1, y + 1);
|
|
31376
|
-
const down = y < h - 1 ? y + 1 : Math.max(0, y - 1);
|
|
31377
31420
|
for (let x = 0; x < w; x++) {
|
|
31378
|
-
|
|
31379
|
-
const right = x < w - 1 ? x + 1 : Math.max(0, x - 1);
|
|
31380
|
-
let hClip = 0;
|
|
31381
|
-
let vClip = 0;
|
|
31382
|
-
let alternating = 0;
|
|
31383
|
-
for (let c = 0; c < 3; c++) {
|
|
31384
|
-
const v = at(x, y, c);
|
|
31385
|
-
const axial = Math.max(
|
|
31386
|
-
Math.abs(v - at(left, y, c)),
|
|
31387
|
-
Math.abs(v - at(right, y, c)),
|
|
31388
|
-
Math.abs(v - at(x, up, c)),
|
|
31389
|
-
Math.abs(v - at(x, down, c))
|
|
31390
|
-
);
|
|
31391
|
-
const diagonal = Math.min(
|
|
31392
|
-
Math.abs(v - at(left, up, c)),
|
|
31393
|
-
Math.abs(v - at(right, up, c)),
|
|
31394
|
-
Math.abs(v - at(left, down, c)),
|
|
31395
|
-
Math.abs(v - at(right, down, c))
|
|
31396
|
-
);
|
|
31397
|
-
if (diagonal < axial) alternating++;
|
|
31398
|
-
if (over(6 * v - at(left, y, c) - at(right, y, c))) {
|
|
31399
|
-
hClip++;
|
|
31400
|
-
}
|
|
31401
|
-
if (over(6 * v - at(x, up, c) - at(x, down, c))) {
|
|
31402
|
-
vClip++;
|
|
31403
|
-
}
|
|
31404
|
-
}
|
|
31405
|
-
if (hClip < 2 || vClip < 2 || alternating < 2) {
|
|
31421
|
+
if (!speckled(x, y)) {
|
|
31406
31422
|
continue;
|
|
31407
31423
|
}
|
|
31424
|
+
const left = mirror2(x, w, -1);
|
|
31425
|
+
const right = mirror2(x, w, 1);
|
|
31426
|
+
const up = mirror2(y, h, -1);
|
|
31427
|
+
const down = mirror2(y, h, 1);
|
|
31428
|
+
const lonelyColumn = x === loneX && loneColumn(y);
|
|
31429
|
+
const lonelyRow = y === loneY && loneRow(x);
|
|
31430
|
+
const nearLoneX = x === loneX - 1 && loneX > 0 && loneColumn(y) && !speckled(loneX, y);
|
|
31431
|
+
const nearLoneY = y === loneY - 1 && loneY > 0 && loneRow(x) && !speckled(x, loneY);
|
|
31432
|
+
const cornerRepeats = lonelyColumn && lonelyRow && (!speckled(x, y - 1) || !speckled(x - 1, y));
|
|
31408
31433
|
for (let c = 0; c < 3; c++) {
|
|
31409
|
-
|
|
31434
|
+
const v = at(x, y, c);
|
|
31435
|
+
let r;
|
|
31436
|
+
if (lonelyColumn && lonelyRow) {
|
|
31437
|
+
r = cornerRepeats ? at(left, y, c) + 3 * v + 2 >> 2 : hBlur(x, y, c);
|
|
31438
|
+
} else {
|
|
31439
|
+
let sum = lonelyColumn ? at(x, up, c) + 2 * v + at(x, down, c) : at(left, y, c) + 2 * v + at(right, y, c);
|
|
31440
|
+
if (nearLoneX) sum += loneColumnValue(y, c) - at(loneX, y, c);
|
|
31441
|
+
if (nearLoneY) sum += loneRowValue(x, c) - at(x, loneY, c);
|
|
31442
|
+
r = sum + 2 >> 2;
|
|
31443
|
+
}
|
|
31444
|
+
px[(y * w + x) * 3 + c] = r;
|
|
31410
31445
|
}
|
|
31411
31446
|
}
|
|
31412
31447
|
}
|