pixel-data-js 0.12.0 → 0.13.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.
@@ -101,6 +101,7 @@ __export(src_exports, {
101
101
  linearLightPerfect: () => linearLightPerfect,
102
102
  makePixelCanvas: () => makePixelCanvas,
103
103
  makeReusableCanvas: () => makeReusableCanvas,
104
+ makeReusableImageData: () => makeReusableImageData,
104
105
  mergeMasks: () => mergeMasks,
105
106
  multiplyFast: () => multiplyFast,
106
107
  multiplyPerfect: () => multiplyPerfect,
@@ -173,9 +174,11 @@ var BlendMode = /* @__PURE__ */ ((BlendMode2) => {
173
174
  BlendMode2[BlendMode2["divide"] = 22] = "divide";
174
175
  return BlendMode2;
175
176
  })(BlendMode || {});
177
+ var overwriteBase = (src, _dst) => src;
178
+ overwriteBase.isOverwrite = true;
176
179
 
177
180
  // src/BlendModes/blend-modes-fast.ts
178
- var overwriteFast = (src, _dst) => src;
181
+ var overwriteFast = overwriteBase;
179
182
  var sourceOverFast = (src, dst) => {
180
183
  const sa = src >>> 24 & 255;
181
184
  if (sa === 255) return src;
@@ -990,7 +993,7 @@ function floodFillSelection(img, startX, startY, {
990
993
  }
991
994
 
992
995
  // src/BlendModes/blend-modes-perfect.ts
993
- var overwritePerfect = (src, _dst) => src;
996
+ var overwritePerfect = overwriteBase;
994
997
  var sourceOverPerfect = (src, dst) => {
995
998
  const sa = src >>> 24 & 255;
996
999
  if (sa === 255) return src;
@@ -1573,6 +1576,22 @@ async function writeImageDataToClipboard(imageData) {
1573
1576
  return writeImgBlobToClipboard(blob);
1574
1577
  }
1575
1578
 
1579
+ // src/ImageData/ReusableImageData.ts
1580
+ function makeReusableImageData() {
1581
+ let imageData = null;
1582
+ let buffer = null;
1583
+ return function getReusableImageData(width, height) {
1584
+ const hasInstance = !!imageData;
1585
+ const widthMatches = hasInstance && imageData.width === width;
1586
+ const heightMatches = hasInstance && imageData.height === height;
1587
+ if (!widthMatches || !heightMatches) {
1588
+ const buffer2 = new Uint8ClampedArray(width * height * 4);
1589
+ imageData = new ImageData(buffer2, width, height);
1590
+ }
1591
+ return imageData;
1592
+ };
1593
+ }
1594
+
1576
1595
  // src/ImageData/copyImageData.ts
1577
1596
  function copyImageData({ data, width, height }) {
1578
1597
  return new ImageData(data.slice(), width, height);
@@ -1752,27 +1771,6 @@ function deserializeNullableImageData(serialized) {
1752
1771
  return deserializeImageData(serialized);
1753
1772
  }
1754
1773
 
1755
- // src/ImageData/writeImageDataPixels.ts
1756
- function writeImageDataPixels(imageData, data, _x, _y, _w, _h) {
1757
- const { x, y, w, h } = typeof _x === "object" ? _x : { x: _x, y: _y, w: _w, h: _h };
1758
- const { width: dstW, height: dstH, data: dst } = imageData;
1759
- const x0 = Math.max(0, x);
1760
- const y0 = Math.max(0, y);
1761
- const x1 = Math.min(dstW, x + w);
1762
- const y1 = Math.min(dstH, y + h);
1763
- if (x1 <= x0 || y1 <= y0) return;
1764
- const rowLen = (x1 - x0) * 4;
1765
- const srcCol = x0 - x;
1766
- const srcYOffset = y0 - y;
1767
- const actualH = y1 - y0;
1768
- for (let row = 0; row < actualH; row++) {
1769
- const dstStart = ((y0 + row) * dstW + x0) * 4;
1770
- const srcRow = srcYOffset + row;
1771
- const o = (srcRow * w + srcCol) * 4;
1772
- dst.set(data.subarray(o, o + rowLen), dstStart);
1773
- }
1774
- }
1775
-
1776
1774
  // src/ImageData/writeImageData.ts
1777
1775
  function writeImageData(target, source, x, y, sx = 0, sy = 0, sw = source.width, sh = source.height, mask = null, maskType = 1 /* BINARY */) {
1778
1776
  const dstW = target.width;
@@ -1827,6 +1825,27 @@ function writeImageData(target, source, x, y, sx = 0, sy = 0, sw = source.width,
1827
1825
  }
1828
1826
  }
1829
1827
 
1828
+ // src/ImageData/writeImageDataPixels.ts
1829
+ function writeImageDataPixels(imageData, data, _x, _y, _w, _h) {
1830
+ const { x, y, w, h } = typeof _x === "object" ? _x : { x: _x, y: _y, w: _w, h: _h };
1831
+ const { width: dstW, height: dstH, data: dst } = imageData;
1832
+ const x0 = Math.max(0, x);
1833
+ const y0 = Math.max(0, y);
1834
+ const x1 = Math.min(dstW, x + w);
1835
+ const y1 = Math.min(dstH, y + h);
1836
+ if (x1 <= x0 || y1 <= y0) return;
1837
+ const rowLen = (x1 - x0) * 4;
1838
+ const srcCol = x0 - x;
1839
+ const srcYOffset = y0 - y;
1840
+ const actualH = y1 - y0;
1841
+ for (let row = 0; row < actualH; row++) {
1842
+ const dstStart = ((y0 + row) * dstW + x0) * 4;
1843
+ const srcRow = srcYOffset + row;
1844
+ const o = (srcRow * w + srcCol) * 4;
1845
+ dst.set(data.subarray(o, o + rowLen), dstStart);
1846
+ }
1847
+ }
1848
+
1830
1849
  // src/IndexedImage/IndexedImage.ts
1831
1850
  var IndexedImage = class _IndexedImage {
1832
1851
  /** The width of the image in pixels. */
@@ -2279,14 +2298,14 @@ function applyMaskToPixelData(dst, mask, opts) {
2279
2298
  }
2280
2299
 
2281
2300
  // src/PixelData/blendColorPixelData.ts
2282
- function blendColorPixelData(dst, color, opts) {
2301
+ function blendColorPixelData(dst, color, opts = {}) {
2283
2302
  const {
2284
2303
  x: targetX = 0,
2285
2304
  y: targetY = 0,
2286
2305
  w: width = dst.width,
2287
2306
  h: height = dst.height,
2288
2307
  alpha: globalAlpha = 255,
2289
- blendFn = FAST_BLEND_MODES[1 /* sourceOver */],
2308
+ blendFn = sourceOverFast,
2290
2309
  mask,
2291
2310
  maskType = 0 /* ALPHA */,
2292
2311
  mw,
@@ -2295,6 +2314,9 @@ function blendColorPixelData(dst, color, opts) {
2295
2314
  invertMask = false
2296
2315
  } = opts;
2297
2316
  if (globalAlpha === 0) return;
2317
+ const baseSrcAlpha = color >>> 24;
2318
+ const isOverwrite = blendFn.isOverwrite;
2319
+ if (baseSrcAlpha === 0 && !isOverwrite) return;
2298
2320
  let x = targetX;
2299
2321
  let y = targetY;
2300
2322
  let w = width;
@@ -2320,15 +2342,8 @@ function blendColorPixelData(dst, color, opts) {
2320
2342
  let mIdx = (my + dy) * mPitch + (mx + dx);
2321
2343
  const dStride = dw - actualW;
2322
2344
  const mStride = mPitch - actualW;
2323
- const baseSrcColor = color;
2324
- const baseSrcAlpha = baseSrcColor >>> 24;
2325
2345
  for (let iy = 0; iy < actualH; iy++) {
2326
2346
  for (let ix = 0; ix < actualW; ix++) {
2327
- if (baseSrcAlpha === 0) {
2328
- dIdx++;
2329
- mIdx++;
2330
- continue;
2331
- }
2332
2347
  let weight = globalAlpha;
2333
2348
  if (mask) {
2334
2349
  const mVal = mask[mIdx];
@@ -2361,20 +2376,20 @@ function blendColorPixelData(dst, color, opts) {
2361
2376
  continue;
2362
2377
  }
2363
2378
  }
2364
- let currentSrcAlpha = baseSrcAlpha;
2365
- let currentSrcColor = baseSrcColor;
2379
+ let currentSrcColor = color;
2366
2380
  if (weight < 255) {
2381
+ let currentSrcAlpha = baseSrcAlpha;
2367
2382
  if (baseSrcAlpha === 255) {
2368
2383
  currentSrcAlpha = weight;
2369
2384
  } else {
2370
2385
  currentSrcAlpha = baseSrcAlpha * weight + 128 >> 8;
2371
2386
  }
2372
- if (currentSrcAlpha === 0) {
2387
+ if (!isOverwrite && currentSrcAlpha === 0) {
2373
2388
  dIdx++;
2374
2389
  mIdx++;
2375
2390
  continue;
2376
2391
  }
2377
- currentSrcColor = (baseSrcColor & 16777215 | currentSrcAlpha << 24) >>> 0;
2392
+ currentSrcColor = (color & 16777215 | currentSrcAlpha << 24) >>> 0;
2378
2393
  }
2379
2394
  dst32[dIdx] = blendFn(currentSrcColor, dst32[dIdx]);
2380
2395
  dIdx++;
@@ -2449,11 +2464,12 @@ function blendPixelData(dst, src, opts) {
2449
2464
  const dStride = dw - actualW;
2450
2465
  const sStride = sw - actualW;
2451
2466
  const mStride = mPitch - actualW;
2467
+ const isOverwrite = blendFn.isOverwrite;
2452
2468
  for (let iy = 0; iy < actualH; iy++) {
2453
2469
  for (let ix = 0; ix < actualW; ix++) {
2454
2470
  const baseSrcColor = src32[sIdx];
2455
2471
  const baseSrcAlpha = baseSrcColor >>> 24;
2456
- if (baseSrcAlpha === 0) {
2472
+ if (baseSrcAlpha === 0 && !isOverwrite) {
2457
2473
  dIdx++;
2458
2474
  sIdx++;
2459
2475
  mIdx++;
@@ -2494,15 +2510,15 @@ function blendPixelData(dst, src, opts) {
2494
2510
  continue;
2495
2511
  }
2496
2512
  }
2497
- let currentSrcAlpha = baseSrcAlpha;
2498
2513
  let currentSrcColor = baseSrcColor;
2499
2514
  if (weight < 255) {
2515
+ let currentSrcAlpha = baseSrcAlpha;
2500
2516
  if (baseSrcAlpha === 255) {
2501
2517
  currentSrcAlpha = weight;
2502
2518
  } else {
2503
2519
  currentSrcAlpha = baseSrcAlpha * weight + 128 >> 8;
2504
2520
  }
2505
- if (currentSrcAlpha === 0) {
2521
+ if (!isOverwrite && currentSrcAlpha === 0) {
2506
2522
  dIdx++;
2507
2523
  sIdx++;
2508
2524
  mIdx++;
@@ -2754,6 +2770,7 @@ function rotateSquareInPlace(pixelData) {
2754
2770
  linearLightPerfect,
2755
2771
  makePixelCanvas,
2756
2772
  makeReusableCanvas,
2773
+ makeReusableImageData,
2757
2774
  mergeMasks,
2758
2775
  multiplyFast,
2759
2776
  multiplyPerfect,