emf-converter 4.15.2 → 4.16.0

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.mjs CHANGED
@@ -21042,6 +21042,7 @@ function invert2(m) {
21042
21042
  }
21043
21043
  var COVERAGE_NUDGE_X = 1e-6;
21044
21044
  var COVERAGE_NUDGE_Y = 1e-9;
21045
+ var AXIS_ALIGNED_SHEAR = 5e-4;
21045
21046
  var SUBPIXEL_GRID = 16;
21046
21047
  function snapToDeviceGrid(spec) {
21047
21048
  const m = spec.toDevice;
@@ -21091,8 +21092,11 @@ function fadeAt(d, bilinear) {
21091
21092
  const i = Math.floor(x);
21092
21093
  return FAR_FADE[i] + (FAR_FADE[i + 1] - FAR_FADE[i]) * (x - i);
21093
21094
  }
21095
+ function ceilExtent(len) {
21096
+ return Math.ceil(len * (1 - 15e-8));
21097
+ }
21094
21098
  function nearSourceExtent(deviceLen, srcLen) {
21095
- const d = Math.ceil(deviceLen - 1e-6) - srcLen;
21099
+ const d = ceilExtent(deviceLen) - srcLen;
21096
21100
  return d >= -1 && d <= 1;
21097
21101
  }
21098
21102
  var BICUBIC_A = -0.5;
@@ -21121,7 +21125,7 @@ function axisFilter(kernel, scale, copy) {
21121
21125
  return { radius: 0.5, weight: (i, c) => Math.floor(c + 0.5) === i ? 1 : 0 };
21122
21126
  }
21123
21127
  }
21124
- function blendSeparable(rgba, width, box, iu0, wu, iv0, wv, verticalFirst, edge = { wrap: void 0, clamp: null }, integerBicubic = false) {
21128
+ function blendSeparable(rgba, width, box, iu0, wu, iv0, wv, verticalFirst, edge = { wrap: void 0, clamp: null }, integerBicubic = false, premult = false) {
21125
21129
  const [outer0, outerW, inner0, innerW] = verticalFirst ? [iu0, wu, iv0, wv] : [iv0, wv, iu0, wu];
21126
21130
  const [oMin, oMax, iMin, iMax] = verticalFirst ? [box.x0, box.x1, box.y0, box.y1] : [box.y0, box.y1, box.x0, box.x1];
21127
21131
  let r = 0;
@@ -21162,9 +21166,9 @@ function blendSeparable(rgba, width, box, iu0, wu, iv0, wv, verticalFirst, edge
21162
21166
  }
21163
21167
  const alpha = px[s + 3];
21164
21168
  const wa = wi * alpha / 255;
21165
- lr += integerBicubic ? wi * Math.round(alpha * px[s] / 255) : wa * px[s];
21166
- lg += integerBicubic ? wi * Math.round(alpha * px[s + 1] / 255) : wa * px[s + 1];
21167
- lb += integerBicubic ? wi * Math.round(alpha * px[s + 2] / 255) : wa * px[s + 2];
21169
+ lr += premult ? wi * px[s] : integerBicubic ? wi * Math.round(alpha * px[s] / 255) : wa * px[s];
21170
+ lg += premult ? wi * px[s + 1] : integerBicubic ? wi * Math.round(alpha * px[s + 1] / 255) : wa * px[s + 1];
21171
+ lb += premult ? wi * px[s + 2] : integerBicubic ? wi * Math.round(alpha * px[s + 2] / 255) : wa * px[s + 2];
21168
21172
  la += wi * alpha;
21169
21173
  }
21170
21174
  if (integerBicubic) {
@@ -21204,7 +21208,8 @@ function mirroredOrigin(o) {
21204
21208
  const f = o - Math.floor(o);
21205
21209
  return f === 0 ? o : Math.floor(o) + 1 - f;
21206
21210
  }
21207
- function resampleImage(rgba, width, height, spec, surface) {
21211
+ function resampleImage(rgba, width, height, spec, surface, options2 = {}) {
21212
+ const { plainRotated: plainRotatedPass = false, keepOrigin = false, premultSource = false, premultOut = false } = typeof options2 === "boolean" ? { plainRotated: options2 } : options2;
21208
21213
  if (spec.rightHalo && spec.rightHalo.length === height * 4) {
21209
21214
  const grown = new Uint8ClampedArray((width + 1) * height * 4);
21210
21215
  for (let y = 0; y < height; y++) {
@@ -21216,20 +21221,27 @@ function resampleImage(rgba, width, height, spec, surface) {
21216
21221
  if (spec.kernel === "nearest" && !spec.wrap) {
21217
21222
  return resampleNearest(rgba, width, height, spec, surface);
21218
21223
  }
21219
- let m = snapToDeviceGrid(spec);
21224
+ let m = keepOrigin ? spec.toDevice : snapToDeviceGrid(spec);
21220
21225
  let kernel = spec.kernel;
21221
21226
  const hq = kernel === "hq-bilinear" || kernel === "hq-bicubic";
21227
+ if (hq && (m[1] !== 0 || m[2] !== 0) && Math.abs(m[1]) <= AXIS_ALIGNED_SHEAR * Math.abs(m[0]) && Math.abs(m[2]) <= AXIS_ALIGNED_SHEAR * Math.abs(m[3])) {
21228
+ m = [m[0], 0, 0, m[3], m[4], m[5]];
21229
+ }
21222
21230
  const axisAligned = m[1] === 0 && m[2] === 0;
21223
- const integerBicubic = kernel === "bicubic" && axisAligned;
21224
- const unitBicubicCopy = integerBicubic && !spec.halfPixelOffset && m[0] === 1 && m[3] === 1 && m[4] - Math.round(m[4]) > -1 / 64 && m[4] - Math.round(m[4]) <= 1 / 64 && m[5] - Math.round(m[5]) > -1 / 64 && m[5] - Math.round(m[5]) <= 1 / 64;
21231
+ let plainRotated = plainRotatedPass;
21232
+ const unitBicubicCopy = kernel === "bicubic" && axisAligned && !spec.halfPixelOffset && m[0] === 1 && m[3] === 1 && m[4] - Math.round(m[4]) > -1 / 64 && m[4] - Math.round(m[4]) <= 1 / 64 && m[5] - Math.round(m[5]) > -1 / 64 && m[5] - Math.round(m[5]) <= 1 / 64;
21225
21233
  let farFade = false;
21226
21234
  let shiftX = 0;
21227
21235
  let shiftY = 0;
21228
21236
  if (hq && !axisAligned) {
21229
21237
  const extentU = Math.hypot(m[0], m[1]) * spec.srcW;
21230
21238
  const extentV = Math.hypot(m[2], m[3]) * spec.srcH;
21231
- if (nearSourceExtent(extentU, spec.srcW) || nearSourceExtent(extentV, spec.srcH)) {
21239
+ const near = nearSourceExtent(extentU, spec.srcW) || nearSourceExtent(extentV, spec.srcH);
21240
+ if (!spec.wrap) {
21241
+ return resampleRotatedTwoStage(rgba, width, height, spec, surface, m, kernel, near);
21242
+ } else if (near) {
21232
21243
  kernel = kernel === "hq-bicubic" ? "bicubic" : "bilinear";
21244
+ plainRotated = true;
21233
21245
  } else {
21234
21246
  farFade = true;
21235
21247
  if (spec.halfPixelOffset) {
@@ -21240,6 +21252,13 @@ function resampleImage(rgba, width, height, spec, surface) {
21240
21252
  }
21241
21253
  }
21242
21254
  }
21255
+ const integerBicubic = kernel === "bicubic" && (axisAligned || plainRotated);
21256
+ if (hq && axisAligned && !spec.wrap && !premultSource) {
21257
+ const phased = resampleHqAxisAligned(rgba, width, height, spec, surface, m, premultOut);
21258
+ if (phased !== void 0) {
21259
+ return phased;
21260
+ }
21261
+ }
21243
21262
  if (hq && axisAligned) {
21244
21263
  const ox = m[0] * spec.srcX + m[4];
21245
21264
  m = [m[0], 0, 0, m[3], m[4] + mirroredOrigin(ox) - ox, m[5]];
@@ -21349,7 +21368,7 @@ function resampleImage(rgba, width, height, spec, surface) {
21349
21368
  for (let t = iv0; t <= iv1; t++) {
21350
21369
  wv.push(integerBicubic ? Math.round(fv.weight(t, cv) * 65536) / 65536 : fv.weight(t, cv));
21351
21370
  }
21352
- return blendSeparable(rgba, width, box, iu0, wu, iv0, wv, kernel === "bicubic", edge, integerBicubic);
21371
+ return blendSeparable(rgba, width, box, iu0, wu, iv0, wv, kernel === "bicubic", edge, integerBicubic, premultSource);
21353
21372
  };
21354
21373
  let [r, g, b, a] = sampleAt(u, v);
21355
21374
  if (farFade && !spec.wrap && a > 0) {
@@ -21373,6 +21392,12 @@ function resampleImage(rgba, width, height, spec, surface) {
21373
21392
  b = Math.trunc(b);
21374
21393
  a = Math.trunc(a);
21375
21394
  if (a <= 0) continue;
21395
+ } else if (premultSource) {
21396
+ r = Math.round(r);
21397
+ g = Math.round(g);
21398
+ b = Math.round(b);
21399
+ a = Math.round(a);
21400
+ if (a <= 0) continue;
21376
21401
  }
21377
21402
  const alpha = Math.min(255, a);
21378
21403
  const dst = (j * w + i) * 4;
@@ -21384,6 +21409,244 @@ function resampleImage(rgba, width, height, spec, surface) {
21384
21409
  }
21385
21410
  return { x: bx0, y: by0, w, h, rgba: out };
21386
21411
  }
21412
+ var HQ_PHASES = 128;
21413
+ function resampleHqAxisAligned(rgba, width, height, spec, surface, m, premultOut) {
21414
+ const kernel = spec.kernel;
21415
+ const scaleU = m[0];
21416
+ const scaleV = m[3];
21417
+ if (!(scaleU >= 1 && scaleV >= 1) || spec.rightHalo) {
21418
+ return void 0;
21419
+ }
21420
+ const integral = (v) => Math.abs(v - Math.round(v)) < 1e-9;
21421
+ if (scaleU === 1 && scaleV === 1 && integral(m[4]) && integral(m[5])) {
21422
+ return void 0;
21423
+ }
21424
+ const shift = spec.halfPixelOffset ? 0.5 : 0;
21425
+ const left = m[0] * spec.srcX + m[4] - shift;
21426
+ const top = m[3] * spec.srcY + m[5] - shift;
21427
+ const right = left + m[0] * spec.srcW;
21428
+ const bottom = top + m[3] * spec.srcH;
21429
+ const xFirst = Math.ceil(left - COVERAGE_NUDGE_X);
21430
+ const xLast = Math.ceil(right - COVERAGE_NUDGE_X) - 1;
21431
+ const yFirst = Math.ceil(top - COVERAGE_NUDGE_Y);
21432
+ const yLast = Math.ceil(bottom - COVERAGE_NUDGE_Y) - 1;
21433
+ const cx0 = Math.max(0, xFirst);
21434
+ const cy0 = Math.max(0, yFirst);
21435
+ const cx1 = Math.min(surface.w - 1, xLast);
21436
+ const cy1 = Math.min(surface.h - 1, yLast);
21437
+ if (cx1 < cx0 || cy1 < cy0) {
21438
+ return null;
21439
+ }
21440
+ const w = cx1 - cx0 + 1;
21441
+ const h = cy1 - cy0 + 1;
21442
+ if (w * h > MAX_RESAMPLE_PIXELS) {
21443
+ return null;
21444
+ }
21445
+ const cubic = kernel === "hq-bicubic";
21446
+ const bin = cubic ? 0.5 : 1;
21447
+ const stepU = Math.round(65536 / scaleU);
21448
+ const stepV = Math.round(65536 / scaleV);
21449
+ const dx = Math.max(0, xFirst - left);
21450
+ const dy = Math.max(0, yFirst - top);
21451
+ const offsetU = dx > 1e-9 ? Math.round((1 - dx) * stepU) : 0;
21452
+ const offsetV = dy > 1e-9 ? Math.round(dy * stepV) : 0;
21453
+ const filterU = axisFilter(kernel, scaleU, false);
21454
+ const filterV = axisFilter(kernel, scaleV, false);
21455
+ const tapsFor = (filter, step, offset, origin, from, to) => {
21456
+ const taps = [];
21457
+ for (let k = from; k <= to; k++) {
21458
+ const phase = Math.floor((k * step + offset - 1) / (65536 / HQ_PHASES));
21459
+ const c = origin + (phase + bin) / HQ_PHASES;
21460
+ const first = Math.ceil(c - filter.radius);
21461
+ const last = Math.floor(c + filter.radius);
21462
+ const weights = [];
21463
+ for (let t = first; t <= last; t++) {
21464
+ weights.push(filter.weight(t, c));
21465
+ }
21466
+ taps.push({ first, weights });
21467
+ }
21468
+ return taps;
21469
+ };
21470
+ const colTaps = tapsFor(filterU, stepU, offsetU, spec.srcX, cx0 - xFirst, cx1 - xFirst);
21471
+ const rowTaps = tapsFor(filterV, stepV, offsetV, spec.srcY, cy0 - yFirst, cy1 - yFirst);
21472
+ let rowMin = Infinity;
21473
+ let rowMax = -Infinity;
21474
+ for (const t of rowTaps) {
21475
+ rowMin = Math.min(rowMin, t.first);
21476
+ rowMax = Math.max(rowMax, t.first + t.weights.length - 1);
21477
+ }
21478
+ rowMin = Math.max(0, rowMin);
21479
+ rowMax = Math.min(height - 1, rowMax);
21480
+ const rows = Math.max(0, rowMax - rowMin + 1);
21481
+ const inter = new Uint8Array(rows * w * 4);
21482
+ for (let r = 0; r < rows; r++) {
21483
+ const srcRow = (rowMin + r) * width;
21484
+ for (let i = 0; i < w; i++) {
21485
+ const t = colTaps[i];
21486
+ let pr = 0;
21487
+ let pg = 0;
21488
+ let pb = 0;
21489
+ let pa = 0;
21490
+ for (let q = 0; q < t.weights.length; q++) {
21491
+ const tx = t.first + q;
21492
+ if (tx < 0 || tx >= width) {
21493
+ continue;
21494
+ }
21495
+ const o = (srcRow + tx) * 4;
21496
+ const a = rgba[o + 3];
21497
+ if (a === 0) {
21498
+ continue;
21499
+ }
21500
+ const wq = t.weights[q];
21501
+ pr += wq * Math.round(rgba[o] * a / 255);
21502
+ pg += wq * Math.round(rgba[o + 1] * a / 255);
21503
+ pb += wq * Math.round(rgba[o + 2] * a / 255);
21504
+ pa += wq * a;
21505
+ }
21506
+ const alpha = Math.min(255, Math.max(0, Math.round(pa)));
21507
+ const d = (r * w + i) * 4;
21508
+ inter[d] = Math.min(alpha, Math.max(0, Math.round(pr)));
21509
+ inter[d + 1] = Math.min(alpha, Math.max(0, Math.round(pg)));
21510
+ inter[d + 2] = Math.min(alpha, Math.max(0, Math.round(pb)));
21511
+ inter[d + 3] = alpha;
21512
+ }
21513
+ }
21514
+ const out = new Uint8ClampedArray(w * h * 4);
21515
+ for (let j = 0; j < h; j++) {
21516
+ const t = rowTaps[j];
21517
+ for (let i = 0; i < w; i++) {
21518
+ let pr = 0;
21519
+ let pg = 0;
21520
+ let pb = 0;
21521
+ let pa = 0;
21522
+ for (let q = 0; q < t.weights.length; q++) {
21523
+ const ty = t.first + q - rowMin;
21524
+ if (ty < 0 || ty >= rows) {
21525
+ continue;
21526
+ }
21527
+ const o = (ty * w + i) * 4;
21528
+ const wq = t.weights[q];
21529
+ pr += wq * inter[o];
21530
+ pg += wq * inter[o + 1];
21531
+ pb += wq * inter[o + 2];
21532
+ pa += wq * inter[o + 3];
21533
+ }
21534
+ const alpha = Math.min(255, Math.max(0, Math.round(pa)));
21535
+ if (alpha === 0) {
21536
+ continue;
21537
+ }
21538
+ const d = (j * w + i) * 4;
21539
+ const k = premultOut ? 1 : 255 / alpha;
21540
+ out[d] = Math.min(alpha, Math.max(0, Math.round(pr))) * k;
21541
+ out[d + 1] = Math.min(alpha, Math.max(0, Math.round(pg))) * k;
21542
+ out[d + 2] = Math.min(alpha, Math.max(0, Math.round(pb))) * k;
21543
+ out[d + 3] = alpha;
21544
+ }
21545
+ }
21546
+ return { x: cx0, y: cy0, w, h, rgba: out, premultiplied: premultOut };
21547
+ }
21548
+ function resampleRotatedTwoStage(rgba, width, height, spec, surface, m, kernel, near) {
21549
+ const pad = 2;
21550
+ let img;
21551
+ let pw;
21552
+ let ph;
21553
+ let a;
21554
+ let b;
21555
+ let c;
21556
+ let d;
21557
+ let ex;
21558
+ let ey;
21559
+ const half = spec.halfPixelOffset;
21560
+ if (near) {
21561
+ pw = width + 2 * pad;
21562
+ ph = height + 2 * pad;
21563
+ img = new Uint8ClampedArray(pw * ph * 4);
21564
+ for (let y = 0; y < height; y++) {
21565
+ for (let x = 0; x < width; x++) {
21566
+ const s = (y * width + x) * 4;
21567
+ const al = rgba[s + 3];
21568
+ const o = ((y + pad) * pw + x + pad) * 4;
21569
+ img[o] = Math.round(rgba[s] * al / 255);
21570
+ img[o + 1] = Math.round(rgba[s + 1] * al / 255);
21571
+ img[o + 2] = Math.round(rgba[s + 2] * al / 255);
21572
+ img[o + 3] = al;
21573
+ }
21574
+ }
21575
+ [a, b, c, d] = m;
21576
+ ex = m[4] - (half ? 0.5 : 0);
21577
+ ey = m[5] - (half ? 0.5 : 0);
21578
+ } else {
21579
+ const W = ceilExtent(Math.hypot(m[0], m[1]) * spec.srcW);
21580
+ const H = ceilExtent(Math.hypot(m[2], m[3]) * spec.srcH);
21581
+ const kx = W / spec.srcW;
21582
+ const ky = H / spec.srcH;
21583
+ const pre = resampleImage(rgba, width, height, {
21584
+ srcX: spec.srcX,
21585
+ srcY: spec.srcY,
21586
+ srcW: spec.srcW,
21587
+ srcH: spec.srcH,
21588
+ toDevice: [kx, 0, 0, ky, -spec.srcX * kx, -spec.srcY * ky],
21589
+ kernel,
21590
+ halfPixelOffset: false
21591
+ }, { w: W, h: H }, { premultOut: true });
21592
+ if (!pre) {
21593
+ return null;
21594
+ }
21595
+ if (!pre.premultiplied) {
21596
+ for (let i = 0; i < pre.rgba.length; i += 4) {
21597
+ const al = pre.rgba[i + 3];
21598
+ pre.rgba[i] = Math.round(pre.rgba[i] * al / 255);
21599
+ pre.rgba[i + 1] = Math.round(pre.rgba[i + 1] * al / 255);
21600
+ pre.rgba[i + 2] = Math.round(pre.rgba[i + 2] * al / 255);
21601
+ }
21602
+ }
21603
+ pw = W + 2 * pad;
21604
+ ph = H + 2 * pad;
21605
+ img = new Uint8ClampedArray(pw * ph * 4);
21606
+ for (let y = 0; y < pre.h; y++) {
21607
+ for (let x = 0; x < pre.w; x++) {
21608
+ const dx = x + pre.x;
21609
+ const dy = y + pre.y;
21610
+ if (dx < W && dy < H) {
21611
+ img.set(pre.rgba.subarray((y * pre.w + x) * 4, (y * pre.w + x) * 4 + 4), ((dy + pad) * pw + dx + pad) * 4);
21612
+ }
21613
+ }
21614
+ }
21615
+ a = m[0] / kx;
21616
+ b = m[1] / kx;
21617
+ c = m[2] / ky;
21618
+ d = m[3] / ky;
21619
+ const ox = m[0] * spec.srcX + m[2] * spec.srcY + m[4];
21620
+ const oy = m[1] * spec.srcX + m[3] * spec.srcY + m[5];
21621
+ ex = half ? ox - 0.5 + 0.5 * (a + c) : ox;
21622
+ ey = half ? oy - 0.5 + 0.5 * (b + d) : oy;
21623
+ }
21624
+ const block = resampleImage(img, pw, ph, {
21625
+ srcX: 0,
21626
+ srcY: 0,
21627
+ srcW: pw,
21628
+ srcH: ph,
21629
+ toDevice: [a, b, c, d, ex - a * pad - c * pad, ey - b * pad - d * pad],
21630
+ kernel: kernel === "hq-bicubic" ? "bicubic" : "bilinear",
21631
+ halfPixelOffset: false
21632
+ }, surface, { plainRotated: true, keepOrigin: true, premultSource: true });
21633
+ if (!block) {
21634
+ return block;
21635
+ }
21636
+ const corners = [
21637
+ [spec.srcX, spec.srcY],
21638
+ [spec.srcX + spec.srcW, spec.srcY],
21639
+ [spec.srcX + spec.srcW, spec.srcY + spec.srcH],
21640
+ [spec.srcX, spec.srcY + spec.srcH]
21641
+ ].flatMap(([u, v]) => [toPlusFix(m[0] * u + m[2] * v + m[4]), toPlusFix(m[1] * u + m[3] * v + m[5])]);
21642
+ const coverage = rasterizePlusFill([corners], false, false, half, { x: block.x, y: block.y, w: block.w, h: block.h });
21643
+ for (let i = 0; i < block.w * block.h; i++) {
21644
+ if (!coverage[i]) {
21645
+ block.rgba.fill(0, i * 4, i * 4 + 4);
21646
+ }
21647
+ }
21648
+ return block;
21649
+ }
21387
21650
  var FIX16 = 65536;
21388
21651
  function resampleNearest(rgba, width, height, spec, surface) {
21389
21652
  const m = spec.toDevice;