pixel-data-js 0.5.0 → 0.5.2

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.
@@ -578,6 +578,41 @@ declare function writeImageDataPixels(imageData: ImageData, data: Uint8ClampedAr
578
578
  */
579
579
  declare function writeImageDataPixels(imageData: ImageData, data: Uint8ClampedArray, x: number, y: number, w: number, h: number): void;
580
580
 
581
+ /**
582
+ * Compressed data format for image processing optimization.
583
+ * Representing an image as a grid of palette indices rather than raw RGBA values
584
+ * significantly reduces memory overhead and optimizes pattern matching logic.
585
+ */
586
+ type IndexedImage = {
587
+ /** The width of the image in pixels. */
588
+ width: number;
589
+ /** The height of the image in pixels. */
590
+ height: number;
591
+ /**
592
+ * A flat array of indices where each value points to a color in the palette.
593
+ * Accessible via the formula: `index = x + (y * width)`.
594
+ */
595
+ data: Int32Array;
596
+ /**
597
+ * A flattened Uint8Array of RGBA values.
598
+ * Every 4 bytes represent one color: `[r, g, b, a]`.
599
+ */
600
+ palette: Uint8Array;
601
+ /**
602
+ * The specific index in the palette that represents a fully transparent pixel.
603
+ * All pixels with an alpha value of 0 are normalized to this index.
604
+ */
605
+ transparentPalletIndex: number;
606
+ };
607
+ /**
608
+ * Converts standard ImageData into an IndexedImage format.
609
+ * This process normalizes all transparent pixels into a single palette entry
610
+ * and maps all unique RGBA colors to sequential integer IDs.
611
+ * @param imageData - The raw ImageData from a canvas or image source.
612
+ * @returns An IndexedImage object containing the index grid and color palette.
613
+ */
614
+ declare function makeIndexedImage(imageData: ImageData): IndexedImage;
615
+
581
616
  /**
582
617
  * A convenience wrapper that extracts the first {@link File} from an
583
618
  * {@link HTMLInputElement} change event and converts it into {@link ImageData}.
@@ -784,4 +819,4 @@ declare function pixelDataToAlphaMask(pixelData: PixelData): AlphaMask;
784
819
  */
785
820
  declare function trimRectBounds<T extends Rect | SelectionRect>(target: T, bounds: Rect): void;
786
821
 
787
- export { type AlphaMask, type AnyMask, type ApplyMaskOptions, type Base64EncodedUInt8Array, type BinaryMask, type BlendColor32, BlendMode, type BlendModeIndex, COLOR_32_BLEND_MODES, COLOR_32_BLEND_TO_INDEX, type Color32, type ColorBlendOptions, type FloodFillImageDataOptions, type FloodFillResult, INDEX_TO_COLOR_32_BLEND, type ImageDataLike, MaskType, type PixelBlendOptions, type PixelCanvas, PixelData, type PixelOptions, type RGBA, type Rect, type RegisteredBlender, type ReusableCanvas, type SelectionRect, type SerializedImageData, UnsupportedFormatError, applyMaskToPixelData, base64DecodeArrayBuffer, base64EncodeArrayBuffer, blendColorPixelData, blendPixelData, clearPixelData, color32ToCssRGBA, color32ToHex, colorBurnColor32, colorDistance, colorDodgeColor32, copyImageData, copyImageDataLike, copyMask, darkenColor32, darkerColor32, deserializeImageData, deserializeNullableImageData, deserializeRawImageData, differenceColor32, divideColor32, exclusionColor32, extractImageDataPixels, extractMask, fileInputChangeToImageData, fileToImageData, fillPixelData, floodFillSelection, getImageDataFromClipboard, getSupportedPixelFormats, hardLightColor32, hardMixColor32, imageDataToAlphaMask, imageDataToDataUrl, imageDataToImgBlob, imgBlobToImageData, invertAlphaMask, invertBinaryMask, invertImageData, invertPixelData, lerpColor32, lerpColor32Fast, lightenColor32, lighterColor32, linearBurnColor32, linearDodgeColor32, linearLightColor32, makePixelCanvas, makeReusableCanvas, mergeMasks, multiplyColor32, overlayColor32, overwriteColor32, packColor, packRGBA, pinLightColor32, pixelDataToAlphaMask, resizeImageData, screenColor32, serializeImageData, serializeNullableImageData, softLightColor32, sourceOverColor32, subtractColor32, trimRectBounds, unpackAlpha, unpackBlue, unpackColor, unpackColorTo, unpackGreen, unpackRed, vividLightColor32, writeImageDataPixels, writeImageDataToClipboard, writeImgBlobToClipboard };
822
+ export { type AlphaMask, type AnyMask, type ApplyMaskOptions, type Base64EncodedUInt8Array, type BinaryMask, type BlendColor32, BlendMode, type BlendModeIndex, COLOR_32_BLEND_MODES, COLOR_32_BLEND_TO_INDEX, type Color32, type ColorBlendOptions, type FloodFillImageDataOptions, type FloodFillResult, INDEX_TO_COLOR_32_BLEND, type ImageDataLike, type IndexedImage, MaskType, type PixelBlendOptions, type PixelCanvas, PixelData, type PixelOptions, type RGBA, type Rect, type RegisteredBlender, type ReusableCanvas, type SelectionRect, type SerializedImageData, UnsupportedFormatError, applyMaskToPixelData, base64DecodeArrayBuffer, base64EncodeArrayBuffer, blendColorPixelData, blendPixelData, clearPixelData, color32ToCssRGBA, color32ToHex, colorBurnColor32, colorDistance, colorDodgeColor32, copyImageData, copyImageDataLike, copyMask, darkenColor32, darkerColor32, deserializeImageData, deserializeNullableImageData, deserializeRawImageData, differenceColor32, divideColor32, exclusionColor32, extractImageDataPixels, extractMask, fileInputChangeToImageData, fileToImageData, fillPixelData, floodFillSelection, getImageDataFromClipboard, getSupportedPixelFormats, hardLightColor32, hardMixColor32, imageDataToAlphaMask, imageDataToDataUrl, imageDataToImgBlob, imgBlobToImageData, invertAlphaMask, invertBinaryMask, invertImageData, invertPixelData, lerpColor32, lerpColor32Fast, lightenColor32, lighterColor32, linearBurnColor32, linearDodgeColor32, linearLightColor32, makeIndexedImage, makePixelCanvas, makeReusableCanvas, mergeMasks, multiplyColor32, overlayColor32, overwriteColor32, packColor, packRGBA, pinLightColor32, pixelDataToAlphaMask, resizeImageData, screenColor32, serializeImageData, serializeNullableImageData, softLightColor32, sourceOverColor32, subtractColor32, trimRectBounds, unpackAlpha, unpackBlue, unpackColor, unpackColorTo, unpackGreen, unpackRed, vividLightColor32, writeImageDataPixels, writeImageDataToClipboard, writeImgBlobToClipboard };
@@ -1075,6 +1075,53 @@ function writeImageDataPixels(imageData, data, _x, _y, _w, _h) {
1075
1075
  }
1076
1076
  }
1077
1077
 
1078
+ // src/IndexedImage/IndexedImage.ts
1079
+ function makeIndexedImage(imageData) {
1080
+ const width = imageData.width;
1081
+ const height = imageData.height;
1082
+ const rawData = imageData.data;
1083
+ const indexedData = new Int32Array(rawData.length / 4);
1084
+ const colorMap = /* @__PURE__ */ new Map();
1085
+ const tempPalette = [];
1086
+ const transparentKey = "0,0,0,0";
1087
+ const transparentPalletIndex = 0;
1088
+ colorMap.set(transparentKey, transparentPalletIndex);
1089
+ tempPalette.push(0);
1090
+ tempPalette.push(0);
1091
+ tempPalette.push(0);
1092
+ tempPalette.push(0);
1093
+ for (let i = 0; i < indexedData.length; i++) {
1094
+ const r = rawData[i * 4];
1095
+ const g = rawData[i * 4 + 1];
1096
+ const b = rawData[i * 4 + 2];
1097
+ const a = rawData[i * 4 + 3];
1098
+ let key;
1099
+ if (a === 0) {
1100
+ key = transparentKey;
1101
+ } else {
1102
+ key = `${r},${g},${b},${a}`;
1103
+ }
1104
+ let id = colorMap.get(key);
1105
+ if (id === void 0) {
1106
+ id = colorMap.size;
1107
+ tempPalette.push(r);
1108
+ tempPalette.push(g);
1109
+ tempPalette.push(b);
1110
+ tempPalette.push(a);
1111
+ colorMap.set(key, id);
1112
+ }
1113
+ indexedData[i] = id;
1114
+ }
1115
+ const palette = new Uint8Array(tempPalette);
1116
+ return {
1117
+ width,
1118
+ height,
1119
+ data: indexedData,
1120
+ transparentPalletIndex,
1121
+ palette
1122
+ };
1123
+ }
1124
+
1078
1125
  // src/Input/fileInputChangeToImageData.ts
1079
1126
  async function fileInputChangeToImageData(event) {
1080
1127
  const target = event.target;
@@ -1708,6 +1755,7 @@ export {
1708
1755
  linearBurnColor32,
1709
1756
  linearDodgeColor32,
1710
1757
  linearLightColor32,
1758
+ makeIndexedImage,
1711
1759
  makePixelCanvas,
1712
1760
  makeReusableCanvas,
1713
1761
  mergeMasks,