emf-converter 4.6.3 → 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 CHANGED
@@ -15598,9 +15598,37 @@ function mix(c0, c1, f) {
15598
15598
  c0[3] + (c1[3] - c0[3]) * f
15599
15599
  ];
15600
15600
  }
15601
+ var f32Buffer = new Float32Array(1);
15602
+ var f32Bits = new Int32Array(f32Buffer.buffer);
15603
+ function truncF32(x) {
15604
+ const r = Math.fround(x);
15605
+ if (Math.abs(r) > Math.abs(x)) {
15606
+ f32Buffer[0] = r;
15607
+ f32Bits[0] -= 1;
15608
+ return f32Buffer[0];
15609
+ }
15610
+ return r;
15611
+ }
15612
+ function blendFactor(positions, factors, t) {
15613
+ const n = Math.min(positions.length, factors.length);
15614
+ if (n === 0) {
15615
+ return t;
15616
+ }
15617
+ if (t <= positions[0]) {
15618
+ return factors[0];
15619
+ }
15620
+ for (let i = 1; i < n; i++) {
15621
+ if (t <= positions[i]) {
15622
+ const span = truncF32(positions[i] - positions[i - 1]);
15623
+ const s = span > 0 ? truncF32(truncF32(t - positions[i - 1]) / span) : 1;
15624
+ return truncF32(factors[i - 1] + truncF32(truncF32(factors[i] - factors[i - 1]) * s));
15625
+ }
15626
+ }
15627
+ return factors[n - 1];
15628
+ }
15601
15629
  function mixBlend(c0, c1, f) {
15602
- const g = Math.fround(1 - f);
15603
- const ch = (a, b) => (Math.trunc(a * 256 * g) + Math.trunc(b * 256 * f)) / 256;
15630
+ const g = truncF32(1 - f);
15631
+ const ch = (a, b) => truncF32(truncF32(a * g) + truncF32(b * f));
15604
15632
  return [ch(c0[0], c1[0]), ch(c0[1], c1[1]), ch(c0[2], c1[2]), ch(c0[3], c1[3])];
15605
15633
  }
15606
15634
  function rampColorAt(ramp, t) {
@@ -15623,7 +15651,7 @@ function rampColorAt(ramp, t) {
15623
15651
  const start = premultiplied(ramp.startArgb, gamma);
15624
15652
  const end = premultiplied(ramp.endArgb, gamma);
15625
15653
  if (ramp.blend && !gamma) {
15626
- return mixBlend(start, end, piecewise(ramp.blend.positions, ramp.blend.factors, t));
15654
+ return mixBlend(start, end, blendFactor(ramp.blend.positions, ramp.blend.factors, t));
15627
15655
  }
15628
15656
  return mix(start, end, ramp.blend ? piecewise(ramp.blend.positions, ramp.blend.factors, t) : t);
15629
15657
  }
@@ -31344,42 +31372,77 @@ function halftoneDespeckle(px, w, h) {
31344
31372
  const src = px.slice();
31345
31373
  const at = (x, y, c) => src[(y * w + x) * 3 + c];
31346
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
+ };
31347
31420
  for (let y = 0; y < h; y++) {
31348
- const up = y > 0 ? y - 1 : Math.min(h - 1, y + 1);
31349
- const down = y < h - 1 ? y + 1 : Math.max(0, y - 1);
31350
31421
  for (let x = 0; x < w; x++) {
31351
- const left = x > 0 ? x - 1 : Math.min(w - 1, x + 1);
31352
- const right = x < w - 1 ? x + 1 : Math.max(0, x - 1);
31353
- let hClip = 0;
31354
- let vClip = 0;
31355
- let alternating = 0;
31356
- for (let c = 0; c < 3; c++) {
31357
- const v = at(x, y, c);
31358
- const axial = Math.max(
31359
- Math.abs(v - at(left, y, c)),
31360
- Math.abs(v - at(right, y, c)),
31361
- Math.abs(v - at(x, up, c)),
31362
- Math.abs(v - at(x, down, c))
31363
- );
31364
- const diagonal = Math.min(
31365
- Math.abs(v - at(left, up, c)),
31366
- Math.abs(v - at(right, up, c)),
31367
- Math.abs(v - at(left, down, c)),
31368
- Math.abs(v - at(right, down, c))
31369
- );
31370
- if (diagonal < axial) alternating++;
31371
- if (over(6 * v - at(left, y, c) - at(right, y, c))) {
31372
- hClip++;
31373
- }
31374
- if (over(6 * v - at(x, up, c) - at(x, down, c))) {
31375
- vClip++;
31376
- }
31377
- }
31378
- if (hClip < 2 || vClip < 2 || alternating < 2) {
31422
+ if (!speckled(x, y)) {
31379
31423
  continue;
31380
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));
31381
31434
  for (let c = 0; c < 3; c++) {
31382
- px[(y * w + x) * 3 + c] = at(left, y, c) + 2 * at(x, y, c) + at(right, y, c) + 2 >> 2;
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;
31383
31446
  }
31384
31447
  }
31385
31448
  }
package/dist/browser.mjs CHANGED
@@ -15596,9 +15596,37 @@ function mix(c0, c1, f) {
15596
15596
  c0[3] + (c1[3] - c0[3]) * f
15597
15597
  ];
15598
15598
  }
15599
+ var f32Buffer = new Float32Array(1);
15600
+ var f32Bits = new Int32Array(f32Buffer.buffer);
15601
+ function truncF32(x) {
15602
+ const r = Math.fround(x);
15603
+ if (Math.abs(r) > Math.abs(x)) {
15604
+ f32Buffer[0] = r;
15605
+ f32Bits[0] -= 1;
15606
+ return f32Buffer[0];
15607
+ }
15608
+ return r;
15609
+ }
15610
+ function blendFactor(positions, factors, t) {
15611
+ const n = Math.min(positions.length, factors.length);
15612
+ if (n === 0) {
15613
+ return t;
15614
+ }
15615
+ if (t <= positions[0]) {
15616
+ return factors[0];
15617
+ }
15618
+ for (let i = 1; i < n; i++) {
15619
+ if (t <= positions[i]) {
15620
+ const span = truncF32(positions[i] - positions[i - 1]);
15621
+ const s = span > 0 ? truncF32(truncF32(t - positions[i - 1]) / span) : 1;
15622
+ return truncF32(factors[i - 1] + truncF32(truncF32(factors[i] - factors[i - 1]) * s));
15623
+ }
15624
+ }
15625
+ return factors[n - 1];
15626
+ }
15599
15627
  function mixBlend(c0, c1, f) {
15600
- const g = Math.fround(1 - f);
15601
- const ch = (a, b) => (Math.trunc(a * 256 * g) + Math.trunc(b * 256 * f)) / 256;
15628
+ const g = truncF32(1 - f);
15629
+ const ch = (a, b) => truncF32(truncF32(a * g) + truncF32(b * f));
15602
15630
  return [ch(c0[0], c1[0]), ch(c0[1], c1[1]), ch(c0[2], c1[2]), ch(c0[3], c1[3])];
15603
15631
  }
15604
15632
  function rampColorAt(ramp, t) {
@@ -15621,7 +15649,7 @@ function rampColorAt(ramp, t) {
15621
15649
  const start = premultiplied(ramp.startArgb, gamma);
15622
15650
  const end = premultiplied(ramp.endArgb, gamma);
15623
15651
  if (ramp.blend && !gamma) {
15624
- return mixBlend(start, end, piecewise(ramp.blend.positions, ramp.blend.factors, t));
15652
+ return mixBlend(start, end, blendFactor(ramp.blend.positions, ramp.blend.factors, t));
15625
15653
  }
15626
15654
  return mix(start, end, ramp.blend ? piecewise(ramp.blend.positions, ramp.blend.factors, t) : t);
15627
15655
  }
@@ -31342,42 +31370,77 @@ function halftoneDespeckle(px, w, h) {
31342
31370
  const src = px.slice();
31343
31371
  const at = (x, y, c) => src[(y * w + x) * 3 + c];
31344
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
+ };
31345
31418
  for (let y = 0; y < h; y++) {
31346
- const up = y > 0 ? y - 1 : Math.min(h - 1, y + 1);
31347
- const down = y < h - 1 ? y + 1 : Math.max(0, y - 1);
31348
31419
  for (let x = 0; x < w; x++) {
31349
- const left = x > 0 ? x - 1 : Math.min(w - 1, x + 1);
31350
- const right = x < w - 1 ? x + 1 : Math.max(0, x - 1);
31351
- let hClip = 0;
31352
- let vClip = 0;
31353
- let alternating = 0;
31354
- for (let c = 0; c < 3; c++) {
31355
- const v = at(x, y, c);
31356
- const axial = Math.max(
31357
- Math.abs(v - at(left, y, c)),
31358
- Math.abs(v - at(right, y, c)),
31359
- Math.abs(v - at(x, up, c)),
31360
- Math.abs(v - at(x, down, c))
31361
- );
31362
- const diagonal = Math.min(
31363
- Math.abs(v - at(left, up, c)),
31364
- Math.abs(v - at(right, up, c)),
31365
- Math.abs(v - at(left, down, c)),
31366
- Math.abs(v - at(right, down, c))
31367
- );
31368
- if (diagonal < axial) alternating++;
31369
- if (over(6 * v - at(left, y, c) - at(right, y, c))) {
31370
- hClip++;
31371
- }
31372
- if (over(6 * v - at(x, up, c) - at(x, down, c))) {
31373
- vClip++;
31374
- }
31375
- }
31376
- if (hClip < 2 || vClip < 2 || alternating < 2) {
31420
+ if (!speckled(x, y)) {
31377
31421
  continue;
31378
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));
31379
31432
  for (let c = 0; c < 3; c++) {
31380
- px[(y * w + x) * 3 + c] = at(left, y, c) + 2 * at(x, y, c) + at(right, y, c) + 2 >> 2;
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;
31381
31444
  }
31382
31445
  }
31383
31446
  }
package/dist/index.js CHANGED
@@ -15599,9 +15599,37 @@ function mix(c0, c1, f) {
15599
15599
  c0[3] + (c1[3] - c0[3]) * f
15600
15600
  ];
15601
15601
  }
15602
+ var f32Buffer = new Float32Array(1);
15603
+ var f32Bits = new Int32Array(f32Buffer.buffer);
15604
+ function truncF32(x) {
15605
+ const r = Math.fround(x);
15606
+ if (Math.abs(r) > Math.abs(x)) {
15607
+ f32Buffer[0] = r;
15608
+ f32Bits[0] -= 1;
15609
+ return f32Buffer[0];
15610
+ }
15611
+ return r;
15612
+ }
15613
+ function blendFactor(positions, factors, t) {
15614
+ const n = Math.min(positions.length, factors.length);
15615
+ if (n === 0) {
15616
+ return t;
15617
+ }
15618
+ if (t <= positions[0]) {
15619
+ return factors[0];
15620
+ }
15621
+ for (let i = 1; i < n; i++) {
15622
+ if (t <= positions[i]) {
15623
+ const span = truncF32(positions[i] - positions[i - 1]);
15624
+ const s = span > 0 ? truncF32(truncF32(t - positions[i - 1]) / span) : 1;
15625
+ return truncF32(factors[i - 1] + truncF32(truncF32(factors[i] - factors[i - 1]) * s));
15626
+ }
15627
+ }
15628
+ return factors[n - 1];
15629
+ }
15602
15630
  function mixBlend(c0, c1, f) {
15603
- const g = Math.fround(1 - f);
15604
- const ch = (a, b) => (Math.trunc(a * 256 * g) + Math.trunc(b * 256 * f)) / 256;
15631
+ const g = truncF32(1 - f);
15632
+ const ch = (a, b) => truncF32(truncF32(a * g) + truncF32(b * f));
15605
15633
  return [ch(c0[0], c1[0]), ch(c0[1], c1[1]), ch(c0[2], c1[2]), ch(c0[3], c1[3])];
15606
15634
  }
15607
15635
  function rampColorAt(ramp, t) {
@@ -15624,7 +15652,7 @@ function rampColorAt(ramp, t) {
15624
15652
  const start = premultiplied(ramp.startArgb, gamma);
15625
15653
  const end = premultiplied(ramp.endArgb, gamma);
15626
15654
  if (ramp.blend && !gamma) {
15627
- return mixBlend(start, end, piecewise(ramp.blend.positions, ramp.blend.factors, t));
15655
+ return mixBlend(start, end, blendFactor(ramp.blend.positions, ramp.blend.factors, t));
15628
15656
  }
15629
15657
  return mix(start, end, ramp.blend ? piecewise(ramp.blend.positions, ramp.blend.factors, t) : t);
15630
15658
  }
@@ -31345,42 +31373,77 @@ function halftoneDespeckle(px, w, h) {
31345
31373
  const src = px.slice();
31346
31374
  const at = (x, y, c) => src[(y * w + x) * 3 + c];
31347
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
+ };
31348
31421
  for (let y = 0; y < h; y++) {
31349
- const up = y > 0 ? y - 1 : Math.min(h - 1, y + 1);
31350
- const down = y < h - 1 ? y + 1 : Math.max(0, y - 1);
31351
31422
  for (let x = 0; x < w; x++) {
31352
- const left = x > 0 ? x - 1 : Math.min(w - 1, x + 1);
31353
- const right = x < w - 1 ? x + 1 : Math.max(0, x - 1);
31354
- let hClip = 0;
31355
- let vClip = 0;
31356
- let alternating = 0;
31357
- for (let c = 0; c < 3; c++) {
31358
- const v = at(x, y, c);
31359
- const axial = Math.max(
31360
- Math.abs(v - at(left, y, c)),
31361
- Math.abs(v - at(right, y, c)),
31362
- Math.abs(v - at(x, up, c)),
31363
- Math.abs(v - at(x, down, c))
31364
- );
31365
- const diagonal = Math.min(
31366
- Math.abs(v - at(left, up, c)),
31367
- Math.abs(v - at(right, up, c)),
31368
- Math.abs(v - at(left, down, c)),
31369
- Math.abs(v - at(right, down, c))
31370
- );
31371
- if (diagonal < axial) alternating++;
31372
- if (over(6 * v - at(left, y, c) - at(right, y, c))) {
31373
- hClip++;
31374
- }
31375
- if (over(6 * v - at(x, up, c) - at(x, down, c))) {
31376
- vClip++;
31377
- }
31378
- }
31379
- if (hClip < 2 || vClip < 2 || alternating < 2) {
31423
+ if (!speckled(x, y)) {
31380
31424
  continue;
31381
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));
31382
31435
  for (let c = 0; c < 3; c++) {
31383
- px[(y * w + x) * 3 + c] = at(left, y, c) + 2 * at(x, y, c) + at(right, y, c) + 2 >> 2;
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;
31384
31447
  }
31385
31448
  }
31386
31449
  }
package/dist/index.mjs CHANGED
@@ -15597,9 +15597,37 @@ function mix(c0, c1, f) {
15597
15597
  c0[3] + (c1[3] - c0[3]) * f
15598
15598
  ];
15599
15599
  }
15600
+ var f32Buffer = new Float32Array(1);
15601
+ var f32Bits = new Int32Array(f32Buffer.buffer);
15602
+ function truncF32(x) {
15603
+ const r = Math.fround(x);
15604
+ if (Math.abs(r) > Math.abs(x)) {
15605
+ f32Buffer[0] = r;
15606
+ f32Bits[0] -= 1;
15607
+ return f32Buffer[0];
15608
+ }
15609
+ return r;
15610
+ }
15611
+ function blendFactor(positions, factors, t) {
15612
+ const n = Math.min(positions.length, factors.length);
15613
+ if (n === 0) {
15614
+ return t;
15615
+ }
15616
+ if (t <= positions[0]) {
15617
+ return factors[0];
15618
+ }
15619
+ for (let i = 1; i < n; i++) {
15620
+ if (t <= positions[i]) {
15621
+ const span = truncF32(positions[i] - positions[i - 1]);
15622
+ const s = span > 0 ? truncF32(truncF32(t - positions[i - 1]) / span) : 1;
15623
+ return truncF32(factors[i - 1] + truncF32(truncF32(factors[i] - factors[i - 1]) * s));
15624
+ }
15625
+ }
15626
+ return factors[n - 1];
15627
+ }
15600
15628
  function mixBlend(c0, c1, f) {
15601
- const g = Math.fround(1 - f);
15602
- const ch = (a, b) => (Math.trunc(a * 256 * g) + Math.trunc(b * 256 * f)) / 256;
15629
+ const g = truncF32(1 - f);
15630
+ const ch = (a, b) => truncF32(truncF32(a * g) + truncF32(b * f));
15603
15631
  return [ch(c0[0], c1[0]), ch(c0[1], c1[1]), ch(c0[2], c1[2]), ch(c0[3], c1[3])];
15604
15632
  }
15605
15633
  function rampColorAt(ramp, t) {
@@ -15622,7 +15650,7 @@ function rampColorAt(ramp, t) {
15622
15650
  const start = premultiplied(ramp.startArgb, gamma);
15623
15651
  const end = premultiplied(ramp.endArgb, gamma);
15624
15652
  if (ramp.blend && !gamma) {
15625
- return mixBlend(start, end, piecewise(ramp.blend.positions, ramp.blend.factors, t));
15653
+ return mixBlend(start, end, blendFactor(ramp.blend.positions, ramp.blend.factors, t));
15626
15654
  }
15627
15655
  return mix(start, end, ramp.blend ? piecewise(ramp.blend.positions, ramp.blend.factors, t) : t);
15628
15656
  }
@@ -31343,42 +31371,77 @@ function halftoneDespeckle(px, w, h) {
31343
31371
  const src = px.slice();
31344
31372
  const at = (x, y, c) => src[(y * w + x) * 3 + c];
31345
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
+ };
31346
31419
  for (let y = 0; y < h; y++) {
31347
- const up = y > 0 ? y - 1 : Math.min(h - 1, y + 1);
31348
- const down = y < h - 1 ? y + 1 : Math.max(0, y - 1);
31349
31420
  for (let x = 0; x < w; x++) {
31350
- const left = x > 0 ? x - 1 : Math.min(w - 1, x + 1);
31351
- const right = x < w - 1 ? x + 1 : Math.max(0, x - 1);
31352
- let hClip = 0;
31353
- let vClip = 0;
31354
- let alternating = 0;
31355
- for (let c = 0; c < 3; c++) {
31356
- const v = at(x, y, c);
31357
- const axial = Math.max(
31358
- Math.abs(v - at(left, y, c)),
31359
- Math.abs(v - at(right, y, c)),
31360
- Math.abs(v - at(x, up, c)),
31361
- Math.abs(v - at(x, down, c))
31362
- );
31363
- const diagonal = Math.min(
31364
- Math.abs(v - at(left, up, c)),
31365
- Math.abs(v - at(right, up, c)),
31366
- Math.abs(v - at(left, down, c)),
31367
- Math.abs(v - at(right, down, c))
31368
- );
31369
- if (diagonal < axial) alternating++;
31370
- if (over(6 * v - at(left, y, c) - at(right, y, c))) {
31371
- hClip++;
31372
- }
31373
- if (over(6 * v - at(x, up, c) - at(x, down, c))) {
31374
- vClip++;
31375
- }
31376
- }
31377
- if (hClip < 2 || vClip < 2 || alternating < 2) {
31421
+ if (!speckled(x, y)) {
31378
31422
  continue;
31379
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));
31380
31433
  for (let c = 0; c < 3; c++) {
31381
- px[(y * w + x) * 3 + c] = at(left, y, c) + 2 * at(x, y, c) + at(right, y, c) + 2 >> 2;
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;
31382
31445
  }
31383
31446
  }
31384
31447
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emf-converter",
3
- "version": "4.6.3",
3
+ "version": "4.6.5",
4
4
  "description": "Convert EMF/WMF metafile binaries to PNG data URLs or SVG (markup, data URL, React/JSX)",
5
5
  "keywords": [
6
6
  "canvas",