pixel-data-js 0.9.2 → 0.10.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.
@@ -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);
@@ -1753,7 +1781,7 @@ function makeIndexedImage(imageData) {
1753
1781
  }
1754
1782
  indexedData[i] = id;
1755
1783
  }
1756
- const palette = new Int32Array(colorMap.keys());
1784
+ const palette = new Uint32Array(colorMap.keys());
1757
1785
  return {
1758
1786
  width,
1759
1787
  height,
@@ -1763,6 +1791,18 @@ function makeIndexedImage(imageData) {
1763
1791
  };
1764
1792
  }
1765
1793
 
1794
+ // src/IndexedImage/getIndexedImageColorCounts.ts
1795
+ function getIndexedImageColorCounts(indexedImage) {
1796
+ const data = indexedImage.data;
1797
+ const palette = indexedImage.palette;
1798
+ const frequencies = new Int32Array(palette.length);
1799
+ for (let i = 0; i < data.length; i++) {
1800
+ const colorIndex = data[i];
1801
+ frequencies[colorIndex]++;
1802
+ }
1803
+ return frequencies;
1804
+ }
1805
+
1766
1806
  // src/IndexedImage/indexedImageToAverageColor.ts
1767
1807
  function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1768
1808
  const { data, palette, transparentPalletIndex } = indexedImage;
@@ -1805,16 +1845,30 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1805
1845
  return packColor(r, g, b, a);
1806
1846
  }
1807
1847
 
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]++;
1848
+ // src/IndexedImage/resampleIndexedImage.ts
1849
+ 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
+ }
1816
1864
  }
1817
- return frequencies;
1865
+ return {
1866
+ width: dstW,
1867
+ height: dstH,
1868
+ data: dstData,
1869
+ palette: source.palette,
1870
+ transparentPalletIndex: source.transparentPalletIndex
1871
+ };
1818
1872
  }
1819
1873
 
1820
1874
  // src/Input/fileInputChangeToImageData.ts
@@ -2441,6 +2495,28 @@ function reflectPixelDataVertical(pixelData) {
2441
2495
  }
2442
2496
  }
2443
2497
 
2498
+ // src/PixelData/resamplePixelData.ts
2499
+ 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
+ }
2513
+ return new PixelData({
2514
+ data: dstBuffer,
2515
+ width: dstW,
2516
+ height: dstH
2517
+ });
2518
+ }
2519
+
2444
2520
  // src/PixelData/rotatePixelData.ts
2445
2521
  function rotatePixelData(pixelData) {
2446
2522
  const width = pixelData.width;
@@ -2579,6 +2655,9 @@ function rotateSquareInPlace(pixelData) {
2579
2655
  pixelDataToAlphaMask,
2580
2656
  reflectPixelDataHorizontal,
2581
2657
  reflectPixelDataVertical,
2658
+ resampleImageData,
2659
+ resampleIndexedImage,
2660
+ resamplePixelData,
2582
2661
  resizeImageData,
2583
2662
  rotatePixelData,
2584
2663
  screenFast,