pixel-data-js 0.10.0 → 0.11.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.
@@ -1641,29 +1641,39 @@ function invertImageData(imageData) {
1641
1641
  return imageData;
1642
1642
  }
1643
1643
 
1644
- // src/ImageData/resampleImageData.ts
1645
- function resampleImageData(source, factor) {
1646
- const srcW = source.width;
1647
- const srcH = source.height;
1644
+ // src/Internal/resample32.ts
1645
+ var resample32Scratch = {
1646
+ data: null,
1647
+ width: 0,
1648
+ height: 0
1649
+ };
1650
+ function resample32(srcData32, srcW, srcH, factor) {
1648
1651
  const dstW = Math.max(1, srcW * factor | 0);
1649
1652
  const dstH = Math.max(1, srcH * factor | 0);
1650
- const srcData = source.data;
1651
- const dstData = new Uint8ClampedArray(dstW * dstH * 4);
1653
+ const dstData = new Int32Array(dstW * dstH);
1654
+ const scaleX = srcW / dstW;
1655
+ const scaleY = srcH / dstH;
1652
1656
  for (let y = 0; y < dstH; y++) {
1653
- const srcY = y / factor | 0;
1654
- const srcRowOffset = srcY * srcW * 4;
1655
- const dstRowOffset = y * dstW * 4;
1657
+ const srcY = Math.min(srcH - 1, y * scaleY | 0);
1658
+ const srcRowOffset = srcY * srcW;
1659
+ const dstRowOffset = y * dstW;
1656
1660
  for (let x = 0; x < dstW; x++) {
1657
- const srcX = x / factor | 0;
1658
- const srcIdx = srcRowOffset + srcX * 4;
1659
- const dstIdx = dstRowOffset + x * 4;
1660
- dstData[dstIdx] = srcData[srcIdx];
1661
- dstData[dstIdx + 1] = srcData[srcIdx + 1];
1662
- dstData[dstIdx + 2] = srcData[srcIdx + 2];
1663
- dstData[dstIdx + 3] = srcData[srcIdx + 3];
1661
+ const srcX = Math.min(srcW - 1, x * scaleX | 0);
1662
+ dstData[dstRowOffset + x] = srcData32[srcRowOffset + srcX];
1664
1663
  }
1665
1664
  }
1666
- return new ImageData(dstData, dstW, dstH);
1665
+ resample32Scratch.data = dstData;
1666
+ resample32Scratch.width = dstW;
1667
+ resample32Scratch.height = dstH;
1668
+ return resample32Scratch;
1669
+ }
1670
+
1671
+ // src/ImageData/resampleImageData.ts
1672
+ function resampleImageData(source, factor) {
1673
+ const src32 = new Uint32Array(source.data.buffer);
1674
+ const { data, width, height } = resample32(src32, source.width, source.height, factor);
1675
+ const uint8ClampedArray = new Uint8ClampedArray(data.buffer);
1676
+ return new ImageData(uint8ClampedArray, width, height);
1667
1677
  }
1668
1678
 
1669
1679
  // src/ImageData/resizeImageData.ts
@@ -1760,10 +1770,12 @@ function writeImageDataPixels(imageData, data, _x, _y, _w, _h) {
1760
1770
  }
1761
1771
 
1762
1772
  // src/IndexedImage/IndexedImage.ts
1763
- function makeIndexedImage(imageData) {
1764
- const width = imageData.width;
1765
- const height = imageData.height;
1766
- const rawData = new Uint32Array(imageData.data.buffer);
1773
+ function makeIndexedImage(imageOrData, width, height) {
1774
+ const isImageData = "width" in imageOrData;
1775
+ const actualWidth = isImageData ? imageOrData.width : width;
1776
+ const actualHeight = isImageData ? imageOrData.height : height;
1777
+ const buffer = isImageData ? imageOrData.data.buffer : imageOrData.buffer;
1778
+ const rawData = new Uint32Array(buffer);
1767
1779
  const indexedData = new Int32Array(rawData.length);
1768
1780
  const colorMap = /* @__PURE__ */ new Map();
1769
1781
  const transparentColor = 0;
@@ -1773,7 +1785,7 @@ function makeIndexedImage(imageData) {
1773
1785
  const pixel = rawData[i];
1774
1786
  const alpha = pixel >>> 24 & 255;
1775
1787
  const isTransparent = alpha === 0;
1776
- const colorKey = isTransparent ? transparentColor : pixel;
1788
+ const colorKey = isTransparent ? transparentColor : pixel >>> 0;
1777
1789
  let id = colorMap.get(colorKey);
1778
1790
  if (id === void 0) {
1779
1791
  id = colorMap.size;
@@ -1781,10 +1793,10 @@ function makeIndexedImage(imageData) {
1781
1793
  }
1782
1794
  indexedData[i] = id;
1783
1795
  }
1784
- const palette = new Uint32Array(colorMap.keys());
1796
+ const palette = Uint32Array.from(colorMap.keys());
1785
1797
  return {
1786
- width,
1787
- height,
1798
+ width: actualWidth,
1799
+ height: actualHeight,
1788
1800
  data: indexedData,
1789
1801
  transparentPalletIndex,
1790
1802
  palette
@@ -1847,25 +1859,16 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1847
1859
 
1848
1860
  // src/IndexedImage/resampleIndexedImage.ts
1849
1861
  function resampleIndexedImage(source, factor) {
1850
- const srcW = source.width;
1851
- const srcH = source.height;
1852
- const dstW = srcW * factor;
1853
- const dstH = srcH * factor;
1854
- const srcData = source.data;
1855
- const dstData = new Int32Array(dstW * dstH);
1856
- for (let y = 0; y < dstH; y++) {
1857
- const srcY = y / factor | 0;
1858
- const rowOffset = srcY * srcW;
1859
- const dstOffset = y * dstW;
1860
- for (let x = 0; x < dstW; x++) {
1861
- const srcX = x / factor | 0;
1862
- dstData[dstOffset + x] = srcData[rowOffset + srcX];
1863
- }
1864
- }
1862
+ const { data, width, height } = resample32(
1863
+ source.data,
1864
+ source.width,
1865
+ source.height,
1866
+ factor
1867
+ );
1865
1868
  return {
1866
- width: dstW,
1867
- height: dstH,
1868
- data: dstData,
1869
+ width,
1870
+ height,
1871
+ data,
1869
1872
  palette: source.palette,
1870
1873
  transparentPalletIndex: source.transparentPalletIndex
1871
1874
  };
@@ -2497,23 +2500,11 @@ function reflectPixelDataVertical(pixelData) {
2497
2500
 
2498
2501
  // src/PixelData/resamplePixelData.ts
2499
2502
  function resamplePixelData(pixelData, factor) {
2500
- const dstW = Math.max(1, pixelData.width * factor | 0);
2501
- const dstH = Math.max(1, pixelData.height * factor | 0);
2502
- const dstBuffer = new Uint8ClampedArray(dstW * dstH * 4);
2503
- const dstData32 = new Uint32Array(dstBuffer.buffer);
2504
- for (let y = 0; y < dstH; y++) {
2505
- const srcY = y / factor | 0;
2506
- const srcRowOffset = srcY * pixelData.width;
2507
- const dstRowOffset = y * dstW;
2508
- for (let x = 0; x < dstW; x++) {
2509
- const srcX = x / factor | 0;
2510
- dstData32[dstRowOffset + x] = pixelData.data32[srcRowOffset + srcX];
2511
- }
2512
- }
2503
+ const { data, width, height } = resample32(pixelData.data32, pixelData.width, pixelData.height, factor);
2513
2504
  return new PixelData({
2514
- data: dstBuffer,
2515
- width: dstW,
2516
- height: dstH
2505
+ width,
2506
+ height,
2507
+ data: new Uint8ClampedArray(data.buffer)
2517
2508
  });
2518
2509
  }
2519
2510