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.
- package/dist/index.dev.cjs +50 -59
- package/dist/index.dev.cjs.map +1 -1
- package/dist/index.dev.js +50 -59
- package/dist/index.dev.js.map +1 -1
- package/dist/index.prod.cjs +50 -59
- package/dist/index.prod.cjs.map +1 -1
- package/dist/index.prod.d.ts +11 -3
- package/dist/index.prod.js +50 -59
- package/dist/index.prod.js.map +1 -1
- package/package.json +1 -1
- package/src/ImageData/resampleImageData.ts +10 -28
- package/src/IndexedImage/IndexedImage.ts +23 -14
- 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
|
|
@@ -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(
|
|
1764
|
-
const
|
|
1765
|
-
const
|
|
1766
|
-
const
|
|
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 =
|
|
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
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
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
|
|
1867
|
-
height
|
|
1868
|
-
data
|
|
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
|
|
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
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2505
|
+
width,
|
|
2506
|
+
height,
|
|
2507
|
+
data: new Uint8ClampedArray(data.buffer)
|
|
2517
2508
|
});
|
|
2518
2509
|
}
|
|
2519
2510
|
|