pixel-data-js 0.10.1 → 0.11.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.
- package/dist/index.dev.cjs +43 -52
- package/dist/index.dev.cjs.map +1 -1
- package/dist/index.dev.js +43 -52
- package/dist/index.dev.js.map +1 -1
- package/dist/index.prod.cjs +43 -52
- package/dist/index.prod.cjs.map +1 -1
- package/dist/index.prod.d.ts +10 -0
- package/dist/index.prod.js +43 -52
- package/dist/index.prod.js.map +1 -1
- package/package.json +1 -1
- package/src/ImageData/resampleImageData.ts +10 -28
- package/src/ImageData/serialization.ts +5 -2
- package/src/IndexedImage/resampleIndexedImage.ts +14 -19
- package/src/Internal/resample32.ts +40 -0
- package/src/PixelData/resamplePixelData.ts +14 -20
package/dist/index.dev.cjs
CHANGED
|
@@ -1641,29 +1641,39 @@ function invertImageData(imageData) {
|
|
|
1641
1641
|
return imageData;
|
|
1642
1642
|
}
|
|
1643
1643
|
|
|
1644
|
-
// src/
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
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
|
|
1651
|
-
const
|
|
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
|
|
1654
|
-
const srcRowOffset = srcY * srcW
|
|
1655
|
-
const dstRowOffset = y * dstW
|
|
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
|
|
1658
|
-
|
|
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
|
-
|
|
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
|
|
@@ -1700,7 +1710,9 @@ function resizeImageData(current, newWidth, newHeight, offsetX = 0, offsetY = 0)
|
|
|
1700
1710
|
|
|
1701
1711
|
// src/ImageData/serialization.ts
|
|
1702
1712
|
function base64EncodeArrayBuffer(buffer) {
|
|
1703
|
-
const
|
|
1713
|
+
const uint8 = new Uint8Array(buffer);
|
|
1714
|
+
const decoder = new TextDecoder("latin1");
|
|
1715
|
+
const binary = decoder.decode(uint8);
|
|
1704
1716
|
return btoa(binary);
|
|
1705
1717
|
}
|
|
1706
1718
|
function base64DecodeArrayBuffer(encoded) {
|
|
@@ -1849,25 +1861,16 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
|
|
|
1849
1861
|
|
|
1850
1862
|
// src/IndexedImage/resampleIndexedImage.ts
|
|
1851
1863
|
function resampleIndexedImage(source, factor) {
|
|
1852
|
-
const
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
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
|
-
}
|
|
1864
|
+
const { data, width, height } = resample32(
|
|
1865
|
+
source.data,
|
|
1866
|
+
source.width,
|
|
1867
|
+
source.height,
|
|
1868
|
+
factor
|
|
1869
|
+
);
|
|
1867
1870
|
return {
|
|
1868
|
-
width
|
|
1869
|
-
height
|
|
1870
|
-
data
|
|
1871
|
+
width,
|
|
1872
|
+
height,
|
|
1873
|
+
data,
|
|
1871
1874
|
palette: source.palette,
|
|
1872
1875
|
transparentPalletIndex: source.transparentPalletIndex
|
|
1873
1876
|
};
|
|
@@ -2499,23 +2502,11 @@ function reflectPixelDataVertical(pixelData) {
|
|
|
2499
2502
|
|
|
2500
2503
|
// src/PixelData/resamplePixelData.ts
|
|
2501
2504
|
function resamplePixelData(pixelData, factor) {
|
|
2502
|
-
const
|
|
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
|
-
}
|
|
2505
|
+
const { data, width, height } = resample32(pixelData.data32, pixelData.width, pixelData.height, factor);
|
|
2515
2506
|
return new PixelData({
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2507
|
+
width,
|
|
2508
|
+
height,
|
|
2509
|
+
data: new Uint8ClampedArray(data.buffer)
|
|
2519
2510
|
});
|
|
2520
2511
|
}
|
|
2521
2512
|
|