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.
- package/dist/index.dev.cjs +98 -17
- package/dist/index.dev.cjs.map +1 -1
- package/dist/index.dev.js +95 -17
- package/dist/index.dev.js.map +1 -1
- package/dist/index.prod.cjs +98 -17
- package/dist/index.prod.cjs.map +1 -1
- package/dist/index.prod.d.ts +25 -9
- package/dist/index.prod.js +95 -17
- package/dist/index.prod.js.map +1 -1
- package/package.json +1 -1
- package/src/ImageData/resampleImageData.ts +35 -0
- package/src/IndexedImage/IndexedImage.ts +24 -15
- package/src/IndexedImage/resampleIndexedImage.ts +36 -0
- package/src/PixelData/resamplePixelData.ts +29 -0
- package/src/index.ts +4 -1
package/dist/index.dev.js
CHANGED
|
@@ -1495,6 +1495,31 @@ function invertImageData(imageData) {
|
|
|
1495
1495
|
return imageData;
|
|
1496
1496
|
}
|
|
1497
1497
|
|
|
1498
|
+
// src/ImageData/resampleImageData.ts
|
|
1499
|
+
function resampleImageData(source, factor) {
|
|
1500
|
+
const srcW = source.width;
|
|
1501
|
+
const srcH = source.height;
|
|
1502
|
+
const dstW = Math.max(1, srcW * factor | 0);
|
|
1503
|
+
const dstH = Math.max(1, srcH * factor | 0);
|
|
1504
|
+
const srcData = source.data;
|
|
1505
|
+
const dstData = new Uint8ClampedArray(dstW * dstH * 4);
|
|
1506
|
+
for (let y = 0; y < dstH; y++) {
|
|
1507
|
+
const srcY = y / factor | 0;
|
|
1508
|
+
const srcRowOffset = srcY * srcW * 4;
|
|
1509
|
+
const dstRowOffset = y * dstW * 4;
|
|
1510
|
+
for (let x = 0; x < dstW; x++) {
|
|
1511
|
+
const srcX = x / factor | 0;
|
|
1512
|
+
const srcIdx = srcRowOffset + srcX * 4;
|
|
1513
|
+
const dstIdx = dstRowOffset + x * 4;
|
|
1514
|
+
dstData[dstIdx] = srcData[srcIdx];
|
|
1515
|
+
dstData[dstIdx + 1] = srcData[srcIdx + 1];
|
|
1516
|
+
dstData[dstIdx + 2] = srcData[srcIdx + 2];
|
|
1517
|
+
dstData[dstIdx + 3] = srcData[srcIdx + 3];
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
return new ImageData(dstData, dstW, dstH);
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1498
1523
|
// src/ImageData/resizeImageData.ts
|
|
1499
1524
|
function resizeImageData(current, newWidth, newHeight, offsetX = 0, offsetY = 0) {
|
|
1500
1525
|
const result = new ImageData(newWidth, newHeight);
|
|
@@ -1589,10 +1614,12 @@ function writeImageDataPixels(imageData, data, _x, _y, _w, _h) {
|
|
|
1589
1614
|
}
|
|
1590
1615
|
|
|
1591
1616
|
// src/IndexedImage/IndexedImage.ts
|
|
1592
|
-
function makeIndexedImage(
|
|
1593
|
-
const
|
|
1594
|
-
const
|
|
1595
|
-
const
|
|
1617
|
+
function makeIndexedImage(imageOrData, width, height) {
|
|
1618
|
+
const isImageData = "width" in imageOrData;
|
|
1619
|
+
const actualWidth = isImageData ? imageOrData.width : width;
|
|
1620
|
+
const actualHeight = isImageData ? imageOrData.height : height;
|
|
1621
|
+
const buffer = isImageData ? imageOrData.data.buffer : imageOrData.buffer;
|
|
1622
|
+
const rawData = new Uint32Array(buffer);
|
|
1596
1623
|
const indexedData = new Int32Array(rawData.length);
|
|
1597
1624
|
const colorMap = /* @__PURE__ */ new Map();
|
|
1598
1625
|
const transparentColor = 0;
|
|
@@ -1602,7 +1629,7 @@ function makeIndexedImage(imageData) {
|
|
|
1602
1629
|
const pixel = rawData[i];
|
|
1603
1630
|
const alpha = pixel >>> 24 & 255;
|
|
1604
1631
|
const isTransparent = alpha === 0;
|
|
1605
|
-
const colorKey = isTransparent ? transparentColor : pixel;
|
|
1632
|
+
const colorKey = isTransparent ? transparentColor : pixel >>> 0;
|
|
1606
1633
|
let id = colorMap.get(colorKey);
|
|
1607
1634
|
if (id === void 0) {
|
|
1608
1635
|
id = colorMap.size;
|
|
@@ -1610,16 +1637,28 @@ function makeIndexedImage(imageData) {
|
|
|
1610
1637
|
}
|
|
1611
1638
|
indexedData[i] = id;
|
|
1612
1639
|
}
|
|
1613
|
-
const palette =
|
|
1640
|
+
const palette = Uint32Array.from(colorMap.keys());
|
|
1614
1641
|
return {
|
|
1615
|
-
width,
|
|
1616
|
-
height,
|
|
1642
|
+
width: actualWidth,
|
|
1643
|
+
height: actualHeight,
|
|
1617
1644
|
data: indexedData,
|
|
1618
1645
|
transparentPalletIndex,
|
|
1619
1646
|
palette
|
|
1620
1647
|
};
|
|
1621
1648
|
}
|
|
1622
1649
|
|
|
1650
|
+
// src/IndexedImage/getIndexedImageColorCounts.ts
|
|
1651
|
+
function getIndexedImageColorCounts(indexedImage) {
|
|
1652
|
+
const data = indexedImage.data;
|
|
1653
|
+
const palette = indexedImage.palette;
|
|
1654
|
+
const frequencies = new Int32Array(palette.length);
|
|
1655
|
+
for (let i = 0; i < data.length; i++) {
|
|
1656
|
+
const colorIndex = data[i];
|
|
1657
|
+
frequencies[colorIndex]++;
|
|
1658
|
+
}
|
|
1659
|
+
return frequencies;
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1623
1662
|
// src/IndexedImage/indexedImageToAverageColor.ts
|
|
1624
1663
|
function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
|
|
1625
1664
|
const { data, palette, transparentPalletIndex } = indexedImage;
|
|
@@ -1662,16 +1701,30 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
|
|
|
1662
1701
|
return packColor(r, g, b, a);
|
|
1663
1702
|
}
|
|
1664
1703
|
|
|
1665
|
-
// src/IndexedImage/
|
|
1666
|
-
function
|
|
1667
|
-
const
|
|
1668
|
-
const
|
|
1669
|
-
const
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1704
|
+
// src/IndexedImage/resampleIndexedImage.ts
|
|
1705
|
+
function resampleIndexedImage(source, factor) {
|
|
1706
|
+
const srcW = source.width;
|
|
1707
|
+
const srcH = source.height;
|
|
1708
|
+
const dstW = srcW * factor;
|
|
1709
|
+
const dstH = srcH * factor;
|
|
1710
|
+
const srcData = source.data;
|
|
1711
|
+
const dstData = new Int32Array(dstW * dstH);
|
|
1712
|
+
for (let y = 0; y < dstH; y++) {
|
|
1713
|
+
const srcY = y / factor | 0;
|
|
1714
|
+
const rowOffset = srcY * srcW;
|
|
1715
|
+
const dstOffset = y * dstW;
|
|
1716
|
+
for (let x = 0; x < dstW; x++) {
|
|
1717
|
+
const srcX = x / factor | 0;
|
|
1718
|
+
dstData[dstOffset + x] = srcData[rowOffset + srcX];
|
|
1719
|
+
}
|
|
1673
1720
|
}
|
|
1674
|
-
return
|
|
1721
|
+
return {
|
|
1722
|
+
width: dstW,
|
|
1723
|
+
height: dstH,
|
|
1724
|
+
data: dstData,
|
|
1725
|
+
palette: source.palette,
|
|
1726
|
+
transparentPalletIndex: source.transparentPalletIndex
|
|
1727
|
+
};
|
|
1675
1728
|
}
|
|
1676
1729
|
|
|
1677
1730
|
// src/Input/fileInputChangeToImageData.ts
|
|
@@ -2298,6 +2351,28 @@ function reflectPixelDataVertical(pixelData) {
|
|
|
2298
2351
|
}
|
|
2299
2352
|
}
|
|
2300
2353
|
|
|
2354
|
+
// src/PixelData/resamplePixelData.ts
|
|
2355
|
+
function resamplePixelData(pixelData, factor) {
|
|
2356
|
+
const dstW = Math.max(1, pixelData.width * factor | 0);
|
|
2357
|
+
const dstH = Math.max(1, pixelData.height * factor | 0);
|
|
2358
|
+
const dstBuffer = new Uint8ClampedArray(dstW * dstH * 4);
|
|
2359
|
+
const dstData32 = new Uint32Array(dstBuffer.buffer);
|
|
2360
|
+
for (let y = 0; y < dstH; y++) {
|
|
2361
|
+
const srcY = y / factor | 0;
|
|
2362
|
+
const srcRowOffset = srcY * pixelData.width;
|
|
2363
|
+
const dstRowOffset = y * dstW;
|
|
2364
|
+
for (let x = 0; x < dstW; x++) {
|
|
2365
|
+
const srcX = x / factor | 0;
|
|
2366
|
+
dstData32[dstRowOffset + x] = pixelData.data32[srcRowOffset + srcX];
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
return new PixelData({
|
|
2370
|
+
data: dstBuffer,
|
|
2371
|
+
width: dstW,
|
|
2372
|
+
height: dstH
|
|
2373
|
+
});
|
|
2374
|
+
}
|
|
2375
|
+
|
|
2301
2376
|
// src/PixelData/rotatePixelData.ts
|
|
2302
2377
|
function rotatePixelData(pixelData) {
|
|
2303
2378
|
const width = pixelData.width;
|
|
@@ -2435,6 +2510,9 @@ export {
|
|
|
2435
2510
|
pixelDataToAlphaMask,
|
|
2436
2511
|
reflectPixelDataHorizontal,
|
|
2437
2512
|
reflectPixelDataVertical,
|
|
2513
|
+
resampleImageData,
|
|
2514
|
+
resampleIndexedImage,
|
|
2515
|
+
resamplePixelData,
|
|
2438
2516
|
resizeImageData,
|
|
2439
2517
|
rotatePixelData,
|
|
2440
2518
|
screenFast,
|