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.
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);
@@ -1610,7 +1635,7 @@ function makeIndexedImage(imageData) {
1610
1635
  }
1611
1636
  indexedData[i] = id;
1612
1637
  }
1613
- const palette = new Int32Array(colorMap.keys());
1638
+ const palette = new Uint32Array(colorMap.keys());
1614
1639
  return {
1615
1640
  width,
1616
1641
  height,
@@ -1620,6 +1645,18 @@ function makeIndexedImage(imageData) {
1620
1645
  };
1621
1646
  }
1622
1647
 
1648
+ // src/IndexedImage/getIndexedImageColorCounts.ts
1649
+ function getIndexedImageColorCounts(indexedImage) {
1650
+ const data = indexedImage.data;
1651
+ const palette = indexedImage.palette;
1652
+ const frequencies = new Int32Array(palette.length);
1653
+ for (let i = 0; i < data.length; i++) {
1654
+ const colorIndex = data[i];
1655
+ frequencies[colorIndex]++;
1656
+ }
1657
+ return frequencies;
1658
+ }
1659
+
1623
1660
  // src/IndexedImage/indexedImageToAverageColor.ts
1624
1661
  function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1625
1662
  const { data, palette, transparentPalletIndex } = indexedImage;
@@ -1662,16 +1699,30 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1662
1699
  return packColor(r, g, b, a);
1663
1700
  }
1664
1701
 
1665
- // src/IndexedImage/getIndexedImageColorCounts.ts
1666
- function getIndexedImageColorCounts(indexedImage) {
1667
- const data = indexedImage.data;
1668
- const palette = indexedImage.palette;
1669
- const frequencies = new Int32Array(palette.length);
1670
- for (let i = 0; i < data.length; i++) {
1671
- const colorIndex = data[i];
1672
- frequencies[colorIndex]++;
1702
+ // src/IndexedImage/resampleIndexedImage.ts
1703
+ function resampleIndexedImage(source, factor) {
1704
+ const srcW = source.width;
1705
+ const srcH = source.height;
1706
+ const dstW = srcW * factor;
1707
+ const dstH = srcH * factor;
1708
+ const srcData = source.data;
1709
+ const dstData = new Int32Array(dstW * dstH);
1710
+ for (let y = 0; y < dstH; y++) {
1711
+ const srcY = y / factor | 0;
1712
+ const rowOffset = srcY * srcW;
1713
+ const dstOffset = y * dstW;
1714
+ for (let x = 0; x < dstW; x++) {
1715
+ const srcX = x / factor | 0;
1716
+ dstData[dstOffset + x] = srcData[rowOffset + srcX];
1717
+ }
1673
1718
  }
1674
- return frequencies;
1719
+ return {
1720
+ width: dstW,
1721
+ height: dstH,
1722
+ data: dstData,
1723
+ palette: source.palette,
1724
+ transparentPalletIndex: source.transparentPalletIndex
1725
+ };
1675
1726
  }
1676
1727
 
1677
1728
  // src/Input/fileInputChangeToImageData.ts
@@ -2298,6 +2349,28 @@ function reflectPixelDataVertical(pixelData) {
2298
2349
  }
2299
2350
  }
2300
2351
 
2352
+ // src/PixelData/resamplePixelData.ts
2353
+ function resamplePixelData(pixelData, factor) {
2354
+ const dstW = Math.max(1, pixelData.width * factor | 0);
2355
+ const dstH = Math.max(1, pixelData.height * factor | 0);
2356
+ const dstBuffer = new Uint8ClampedArray(dstW * dstH * 4);
2357
+ const dstData32 = new Uint32Array(dstBuffer.buffer);
2358
+ for (let y = 0; y < dstH; y++) {
2359
+ const srcY = y / factor | 0;
2360
+ const srcRowOffset = srcY * pixelData.width;
2361
+ const dstRowOffset = y * dstW;
2362
+ for (let x = 0; x < dstW; x++) {
2363
+ const srcX = x / factor | 0;
2364
+ dstData32[dstRowOffset + x] = pixelData.data32[srcRowOffset + srcX];
2365
+ }
2366
+ }
2367
+ return new PixelData({
2368
+ data: dstBuffer,
2369
+ width: dstW,
2370
+ height: dstH
2371
+ });
2372
+ }
2373
+
2301
2374
  // src/PixelData/rotatePixelData.ts
2302
2375
  function rotatePixelData(pixelData) {
2303
2376
  const width = pixelData.width;
@@ -2435,6 +2508,9 @@ export {
2435
2508
  pixelDataToAlphaMask,
2436
2509
  reflectPixelDataHorizontal,
2437
2510
  reflectPixelDataVertical,
2511
+ resampleImageData,
2512
+ resampleIndexedImage,
2513
+ resamplePixelData,
2438
2514
  resizeImageData,
2439
2515
  rotatePixelData,
2440
2516
  screenFast,