pixel-data-js 0.10.1 → 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
@@ -1849,25 +1859,16 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1849
1859
 
1850
1860
  // src/IndexedImage/resampleIndexedImage.ts
1851
1861
  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
- }
1866
- }
1862
+ const { data, width, height } = resample32(
1863
+ source.data,
1864
+ source.width,
1865
+ source.height,
1866
+ factor
1867
+ );
1867
1868
  return {
1868
- width: dstW,
1869
- height: dstH,
1870
- data: dstData,
1869
+ width,
1870
+ height,
1871
+ data,
1871
1872
  palette: source.palette,
1872
1873
  transparentPalletIndex: source.transparentPalletIndex
1873
1874
  };
@@ -2499,23 +2500,11 @@ function reflectPixelDataVertical(pixelData) {
2499
2500
 
2500
2501
  // src/PixelData/resamplePixelData.ts
2501
2502
  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
- }
2503
+ const { data, width, height } = resample32(pixelData.data32, pixelData.width, pixelData.height, factor);
2515
2504
  return new PixelData({
2516
- data: dstBuffer,
2517
- width: dstW,
2518
- height: dstH
2505
+ width,
2506
+ height,
2507
+ data: new Uint8ClampedArray(data.buffer)
2519
2508
  });
2520
2509
  }
2521
2510