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.
@@ -580,6 +580,12 @@ declare function imgBlobToImageData(blob: Blob): Promise<ImageData>;
580
580
 
581
581
  declare function invertImageData(imageData: ImageData): ImageData;
582
582
 
583
+ /**
584
+ * Resamples ImageData by a specific factor.
585
+ * Factor > 1 upscales, Factor < 1 downscales.
586
+ */
587
+ declare function resampleImageData(source: ImageData, factor: number): ImageData;
588
+
583
589
  /**
584
590
  * Non destructively resizes the {@link ImageData} buffer to new dimensions, optionally
585
591
  * offsetting the original content.
@@ -664,7 +670,7 @@ type IndexedImage = {
664
670
  /**
665
671
  * A palette of packed 32-bit colors (ABGR).
666
672
  */
667
- palette: Int32Array;
673
+ palette: Uint32Array;
668
674
  /**
669
675
  * The specific index in the palette that represents a fully transparent pixel.
670
676
  */
@@ -673,10 +679,16 @@ type IndexedImage = {
673
679
  /**
674
680
  * Converts standard ImageData into an IndexedImage format.
675
681
  */
682
+ declare function makeIndexedImage(imageData: ImageData): IndexedImage;
683
+ declare function makeIndexedImage(data: Uint8ClampedArray, width: number, height: number): IndexedImage;
684
+
676
685
  /**
677
- * Converts standard ImageData into an IndexedImage format.
686
+ * Calculates the frequency of each palette index based on the image data.
687
+ * The index of the returned array maps directly to the index of the palette.
688
+ * @param indexedImage - The source image containing data and palette definitions.
689
+ * @returns A typed array where each entry represents the total count of that palette index.
678
690
  */
679
- declare function makeIndexedImage(imageData: ImageData): IndexedImage;
691
+ declare function getIndexedImageColorCounts(indexedImage: IndexedImage): Int32Array;
680
692
 
681
693
  /**
682
694
  * Calculates the area-weighted average color of an IndexedImage.
@@ -688,12 +700,10 @@ declare function makeIndexedImage(imageData: ImageData): IndexedImage;
688
700
  declare function indexedImageToAverageColor(indexedImage: IndexedImage, includeTransparent?: boolean): Color32;
689
701
 
690
702
  /**
691
- * Calculates the frequency of each palette index based on the image data.
692
- * The index of the returned array maps directly to the index of the palette.
693
- * @param indexedImage - The source image containing data and palette definitions.
694
- * @returns A typed array where each entry represents the total count of that palette index.
703
+ * Resamples an IndexedImage by a specific factor using nearest neighbor
704
+ * Factor > 1 upscales, Factor < 1 downscales.
695
705
  */
696
- declare function getIndexedImageColorCounts(indexedImage: IndexedImage): Int32Array;
706
+ declare function resampleIndexedImage(source: IndexedImage, factor: number): IndexedImage;
697
707
 
698
708
  /**
699
709
  * A convenience wrapper that extracts the first {@link File} from an
@@ -888,6 +898,12 @@ declare function pixelDataToAlphaMask(pixelData: PixelData): AlphaMask;
888
898
  declare function reflectPixelDataHorizontal(pixelData: PixelData): void;
889
899
  declare function reflectPixelDataVertical(pixelData: PixelData): void;
890
900
 
901
+ /**
902
+ * Resamples an PixelData by a specific factor using nearest neighbor
903
+ * Factor > 1 upscales, Factor < 1 downscales.
904
+ */
905
+ declare function resamplePixelData(pixelData: PixelData, factor: number): PixelData;
906
+
891
907
  /**
892
908
  * Rotates pixel data 90 degrees clockwise.
893
909
  * If the image is square, it performs the rotation in-place.
@@ -911,4 +927,4 @@ declare function rotatePixelData(pixelData: PixelData): void;
911
927
  */
912
928
  declare function trimRectBounds<T extends Rect | SelectionRect>(target: T, bounds: Rect): void;
913
929
 
914
- export { type AlphaMask, type AnyMask, type ApplyMaskOptions, type Base64EncodedUInt8Array, type BinaryMask, type BlendColor32, BlendMode, type BlendModeIndex, type BlendToIndexGetter, type Color32, type ColorBlendOptions, FAST_BLENDER_REGISTRY, FAST_BLEND_MODES, FAST_BLEND_MODE_BY_NAME, FAST_BLEND_TO_INDEX, type FastBlendModes, type FloodFillImageDataOptions, type FloodFillResult, INDEX_TO_FAST_BLEND, INDEX_TO_PERFECT_BLEND, type ImageDataLike, type IndexToBlendGetter, type IndexedImage, MaskType, PERFECT_BLENDER_REGISTRY, PERFECT_BLEND_MODES, PERFECT_BLEND_MODE_BY_NAME, PERFECT_BLEND_TO_INDEX, type PerfectBlendModes, type PixelBlendOptions, type PixelCanvas, PixelData, type PixelOptions, type Point, type RGBA, type Rect, type RegisteredFastBlender, type RegisteredPerfectBlender, type ReusableCanvas, type SelectionRect, type SerializedImageData, UnsupportedFormatError, applyMaskToPixelData, base64DecodeArrayBuffer, base64EncodeArrayBuffer, blendColorPixelData, blendPixelData, clearPixelData, color32ToCssRGBA, color32ToHex, colorBurnFast, colorBurnPerfect, colorDistance, colorDodgeFast, colorDodgePerfect, copyImageData, copyImageDataLike, copyMask, darkenFast, darkenPerfect, darkerFast, darkerPerfect, deserializeImageData, deserializeNullableImageData, deserializeRawImageData, differenceFast, differencePerfect, divideFast, dividePerfect, exclusionFast, exclusionPerfect, extractImageDataPixels, extractMask, fileInputChangeToImageData, fileToImageData, fillPixelData, floodFillSelection, getImageDataFromClipboard, getIndexedImageColorCounts, getSupportedPixelFormats, hardLightFast, hardLightPerfect, hardMixFast, hardMixPerfect, imageDataToAlphaMask, imageDataToDataUrl, imageDataToImgBlob, imgBlobToImageData, indexedImageToAverageColor, invertAlphaMask, invertBinaryMask, invertImageData, invertPixelData, lerpColor32, lerpColor32Fast, lightenFast, lightenPerfect, lighterFast, lighterPerfect, linearBurnFast, linearBurnPerfect, linearDodgeFast, linearDodgePerfect, linearLightFast, linearLightPerfect, makeIndexedImage, makePixelCanvas, makeReusableCanvas, mergeMasks, multiplyFast, multiplyPerfect, overlayFast, overlayPerfect, overwriteFast, overwritePerfect, packColor, packRGBA, pinLightFast, pinLightPerfect, pixelDataToAlphaMask, reflectPixelDataHorizontal, reflectPixelDataVertical, resizeImageData, rotatePixelData, screenFast, screenPerfect, serializeImageData, serializeNullableImageData, softLightFast, softLightPerfect, sourceOverFast, sourceOverPerfect, subtractFast, subtractPerfect, trimRectBounds, unpackAlpha, unpackBlue, unpackColor, unpackColorTo, unpackGreen, unpackRed, vividLightFast, vividLightPerfect, writeImageDataPixels, writeImageDataToClipboard, writeImgBlobToClipboard };
930
+ export { type AlphaMask, type AnyMask, type ApplyMaskOptions, type Base64EncodedUInt8Array, type BinaryMask, type BlendColor32, BlendMode, type BlendModeIndex, type BlendToIndexGetter, type Color32, type ColorBlendOptions, FAST_BLENDER_REGISTRY, FAST_BLEND_MODES, FAST_BLEND_MODE_BY_NAME, FAST_BLEND_TO_INDEX, type FastBlendModes, type FloodFillImageDataOptions, type FloodFillResult, INDEX_TO_FAST_BLEND, INDEX_TO_PERFECT_BLEND, type ImageDataLike, type IndexToBlendGetter, type IndexedImage, MaskType, PERFECT_BLENDER_REGISTRY, PERFECT_BLEND_MODES, PERFECT_BLEND_MODE_BY_NAME, PERFECT_BLEND_TO_INDEX, type PerfectBlendModes, type PixelBlendOptions, type PixelCanvas, PixelData, type PixelOptions, type Point, type RGBA, type Rect, type RegisteredFastBlender, type RegisteredPerfectBlender, type ReusableCanvas, type SelectionRect, type SerializedImageData, UnsupportedFormatError, applyMaskToPixelData, base64DecodeArrayBuffer, base64EncodeArrayBuffer, blendColorPixelData, blendPixelData, clearPixelData, color32ToCssRGBA, color32ToHex, colorBurnFast, colorBurnPerfect, colorDistance, colorDodgeFast, colorDodgePerfect, copyImageData, copyImageDataLike, copyMask, darkenFast, darkenPerfect, darkerFast, darkerPerfect, deserializeImageData, deserializeNullableImageData, deserializeRawImageData, differenceFast, differencePerfect, divideFast, dividePerfect, exclusionFast, exclusionPerfect, extractImageDataPixels, extractMask, fileInputChangeToImageData, fileToImageData, fillPixelData, floodFillSelection, getImageDataFromClipboard, getIndexedImageColorCounts, getSupportedPixelFormats, hardLightFast, hardLightPerfect, hardMixFast, hardMixPerfect, imageDataToAlphaMask, imageDataToDataUrl, imageDataToImgBlob, imgBlobToImageData, indexedImageToAverageColor, invertAlphaMask, invertBinaryMask, invertImageData, invertPixelData, lerpColor32, lerpColor32Fast, lightenFast, lightenPerfect, lighterFast, lighterPerfect, linearBurnFast, linearBurnPerfect, linearDodgeFast, linearDodgePerfect, linearLightFast, linearLightPerfect, makeIndexedImage, makePixelCanvas, makeReusableCanvas, mergeMasks, multiplyFast, multiplyPerfect, overlayFast, overlayPerfect, overwriteFast, overwritePerfect, packColor, packRGBA, pinLightFast, pinLightPerfect, pixelDataToAlphaMask, reflectPixelDataHorizontal, reflectPixelDataVertical, resampleImageData, resampleIndexedImage, resamplePixelData, resizeImageData, rotatePixelData, screenFast, screenPerfect, serializeImageData, serializeNullableImageData, softLightFast, softLightPerfect, sourceOverFast, sourceOverPerfect, subtractFast, subtractPerfect, trimRectBounds, unpackAlpha, unpackBlue, unpackColor, unpackColorTo, unpackGreen, unpackRed, vividLightFast, vividLightPerfect, writeImageDataPixels, writeImageDataToClipboard, writeImgBlobToClipboard };
@@ -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(imageData) {
1593
- const width = imageData.width;
1594
- const height = imageData.height;
1595
- const rawData = new Uint32Array(imageData.data.buffer);
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 = new Int32Array(colorMap.keys());
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/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]++;
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 frequencies;
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,