pixel-data-js 0.9.2 → 0.10.1

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.
@@ -114,6 +114,9 @@ __export(src_exports, {
114
114
  pixelDataToAlphaMask: () => pixelDataToAlphaMask,
115
115
  reflectPixelDataHorizontal: () => reflectPixelDataHorizontal,
116
116
  reflectPixelDataVertical: () => reflectPixelDataVertical,
117
+ resampleImageData: () => resampleImageData,
118
+ resampleIndexedImage: () => resampleIndexedImage,
119
+ resamplePixelData: () => resamplePixelData,
117
120
  resizeImageData: () => resizeImageData,
118
121
  rotatePixelData: () => rotatePixelData,
119
122
  screenFast: () => screenFast,
@@ -1638,6 +1641,31 @@ function invertImageData(imageData) {
1638
1641
  return imageData;
1639
1642
  }
1640
1643
 
1644
+ // src/ImageData/resampleImageData.ts
1645
+ function resampleImageData(source, factor) {
1646
+ const srcW = source.width;
1647
+ const srcH = source.height;
1648
+ const dstW = Math.max(1, srcW * factor | 0);
1649
+ const dstH = Math.max(1, srcH * factor | 0);
1650
+ const srcData = source.data;
1651
+ const dstData = new Uint8ClampedArray(dstW * dstH * 4);
1652
+ 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;
1656
+ 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];
1664
+ }
1665
+ }
1666
+ return new ImageData(dstData, dstW, dstH);
1667
+ }
1668
+
1641
1669
  // src/ImageData/resizeImageData.ts
1642
1670
  function resizeImageData(current, newWidth, newHeight, offsetX = 0, offsetY = 0) {
1643
1671
  const result = new ImageData(newWidth, newHeight);
@@ -1732,10 +1760,12 @@ function writeImageDataPixels(imageData, data, _x, _y, _w, _h) {
1732
1760
  }
1733
1761
 
1734
1762
  // src/IndexedImage/IndexedImage.ts
1735
- function makeIndexedImage(imageData) {
1736
- const width = imageData.width;
1737
- const height = imageData.height;
1738
- const rawData = new Uint32Array(imageData.data.buffer);
1763
+ function makeIndexedImage(imageOrData, width, height) {
1764
+ const isImageData = "width" in imageOrData;
1765
+ const actualWidth = isImageData ? imageOrData.width : width;
1766
+ const actualHeight = isImageData ? imageOrData.height : height;
1767
+ const buffer = isImageData ? imageOrData.data.buffer : imageOrData.buffer;
1768
+ const rawData = new Uint32Array(buffer);
1739
1769
  const indexedData = new Int32Array(rawData.length);
1740
1770
  const colorMap = /* @__PURE__ */ new Map();
1741
1771
  const transparentColor = 0;
@@ -1745,7 +1775,7 @@ function makeIndexedImage(imageData) {
1745
1775
  const pixel = rawData[i];
1746
1776
  const alpha = pixel >>> 24 & 255;
1747
1777
  const isTransparent = alpha === 0;
1748
- const colorKey = isTransparent ? transparentColor : pixel;
1778
+ const colorKey = isTransparent ? transparentColor : pixel >>> 0;
1749
1779
  let id = colorMap.get(colorKey);
1750
1780
  if (id === void 0) {
1751
1781
  id = colorMap.size;
@@ -1753,16 +1783,28 @@ function makeIndexedImage(imageData) {
1753
1783
  }
1754
1784
  indexedData[i] = id;
1755
1785
  }
1756
- const palette = new Int32Array(colorMap.keys());
1786
+ const palette = Uint32Array.from(colorMap.keys());
1757
1787
  return {
1758
- width,
1759
- height,
1788
+ width: actualWidth,
1789
+ height: actualHeight,
1760
1790
  data: indexedData,
1761
1791
  transparentPalletIndex,
1762
1792
  palette
1763
1793
  };
1764
1794
  }
1765
1795
 
1796
+ // src/IndexedImage/getIndexedImageColorCounts.ts
1797
+ function getIndexedImageColorCounts(indexedImage) {
1798
+ const data = indexedImage.data;
1799
+ const palette = indexedImage.palette;
1800
+ const frequencies = new Int32Array(palette.length);
1801
+ for (let i = 0; i < data.length; i++) {
1802
+ const colorIndex = data[i];
1803
+ frequencies[colorIndex]++;
1804
+ }
1805
+ return frequencies;
1806
+ }
1807
+
1766
1808
  // src/IndexedImage/indexedImageToAverageColor.ts
1767
1809
  function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1768
1810
  const { data, palette, transparentPalletIndex } = indexedImage;
@@ -1805,16 +1847,30 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1805
1847
  return packColor(r, g, b, a);
1806
1848
  }
1807
1849
 
1808
- // src/IndexedImage/getIndexedImageColorCounts.ts
1809
- function getIndexedImageColorCounts(indexedImage) {
1810
- const data = indexedImage.data;
1811
- const palette = indexedImage.palette;
1812
- const frequencies = new Int32Array(palette.length);
1813
- for (let i = 0; i < data.length; i++) {
1814
- const colorIndex = data[i];
1815
- frequencies[colorIndex]++;
1850
+ // src/IndexedImage/resampleIndexedImage.ts
1851
+ function resampleIndexedImage(source, factor) {
1852
+ const srcW = source.width;
1853
+ const srcH = source.height;
1854
+ const dstW = srcW * factor;
1855
+ const dstH = srcH * factor;
1856
+ const srcData = source.data;
1857
+ const dstData = new Int32Array(dstW * dstH);
1858
+ for (let y = 0; y < dstH; y++) {
1859
+ const srcY = y / factor | 0;
1860
+ const rowOffset = srcY * srcW;
1861
+ const dstOffset = y * dstW;
1862
+ for (let x = 0; x < dstW; x++) {
1863
+ const srcX = x / factor | 0;
1864
+ dstData[dstOffset + x] = srcData[rowOffset + srcX];
1865
+ }
1816
1866
  }
1817
- return frequencies;
1867
+ return {
1868
+ width: dstW,
1869
+ height: dstH,
1870
+ data: dstData,
1871
+ palette: source.palette,
1872
+ transparentPalletIndex: source.transparentPalletIndex
1873
+ };
1818
1874
  }
1819
1875
 
1820
1876
  // src/Input/fileInputChangeToImageData.ts
@@ -2441,6 +2497,28 @@ function reflectPixelDataVertical(pixelData) {
2441
2497
  }
2442
2498
  }
2443
2499
 
2500
+ // src/PixelData/resamplePixelData.ts
2501
+ function resamplePixelData(pixelData, factor) {
2502
+ const dstW = Math.max(1, pixelData.width * factor | 0);
2503
+ const dstH = Math.max(1, pixelData.height * factor | 0);
2504
+ const dstBuffer = new Uint8ClampedArray(dstW * dstH * 4);
2505
+ const dstData32 = new Uint32Array(dstBuffer.buffer);
2506
+ for (let y = 0; y < dstH; y++) {
2507
+ const srcY = y / factor | 0;
2508
+ const srcRowOffset = srcY * pixelData.width;
2509
+ const dstRowOffset = y * dstW;
2510
+ for (let x = 0; x < dstW; x++) {
2511
+ const srcX = x / factor | 0;
2512
+ dstData32[dstRowOffset + x] = pixelData.data32[srcRowOffset + srcX];
2513
+ }
2514
+ }
2515
+ return new PixelData({
2516
+ data: dstBuffer,
2517
+ width: dstW,
2518
+ height: dstH
2519
+ });
2520
+ }
2521
+
2444
2522
  // src/PixelData/rotatePixelData.ts
2445
2523
  function rotatePixelData(pixelData) {
2446
2524
  const width = pixelData.width;
@@ -2579,6 +2657,9 @@ function rotateSquareInPlace(pixelData) {
2579
2657
  pixelDataToAlphaMask,
2580
2658
  reflectPixelDataHorizontal,
2581
2659
  reflectPixelDataVertical,
2660
+ resampleImageData,
2661
+ resampleIndexedImage,
2662
+ resamplePixelData,
2582
2663
  resizeImageData,
2583
2664
  rotatePixelData,
2584
2665
  screenFast,