pixel-data-js 0.9.0 → 0.9.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.
@@ -155,6 +155,10 @@ type SelectionRect = Rect & ({
155
155
  mask?: null;
156
156
  maskType?: null;
157
157
  });
158
+ type Point = {
159
+ x: number;
160
+ y: number;
161
+ };
158
162
 
159
163
  /**
160
164
  * Packs RGBA into a 32-bit integer compatible with
@@ -276,6 +280,7 @@ declare enum BlendMode {
276
280
  subtract = 21,
277
281
  divide = 22
278
282
  }
283
+ type BlendModeIndex = typeof BlendMode[keyof typeof BlendMode];
279
284
 
280
285
  declare const overwriteFast: BlendColor32;
281
286
  declare const sourceOverFast: BlendColor32;
@@ -325,16 +330,9 @@ declare const subtractFast: BlendColor32;
325
330
  declare const divideFast: BlendColor32;
326
331
  declare const FAST_BLENDER_REGISTRY: readonly [readonly [BlendMode.overwrite, BlendColor32], readonly [BlendMode.sourceOver, BlendColor32], readonly [BlendMode.darken, BlendColor32], readonly [BlendMode.multiply, BlendColor32], readonly [BlendMode.colorBurn, BlendColor32], readonly [BlendMode.linearBurn, BlendColor32], readonly [BlendMode.darkerColor, BlendColor32], readonly [BlendMode.lighten, BlendColor32], readonly [BlendMode.screen, BlendColor32], readonly [BlendMode.colorDodge, BlendColor32], readonly [BlendMode.linearDodge, BlendColor32], readonly [BlendMode.lighterColor, BlendColor32], readonly [BlendMode.overlay, BlendColor32], readonly [BlendMode.softLight, BlendColor32], readonly [BlendMode.hardLight, BlendColor32], readonly [BlendMode.vividLight, BlendColor32], readonly [BlendMode.linearLight, BlendColor32], readonly [BlendMode.pinLight, BlendColor32], readonly [BlendMode.hardMix, BlendColor32], readonly [BlendMode.difference, BlendColor32], readonly [BlendMode.exclusion, BlendColor32], readonly [BlendMode.subtract, BlendColor32], readonly [BlendMode.divide, BlendColor32]];
327
332
  type RegisteredFastBlender = typeof FAST_BLENDER_REGISTRY[number][1];
328
- type FastBlendModeIndex = number & {
329
- readonly __brandBlendModeIndex: unique symbol;
330
- };
331
333
  declare const FAST_BLEND_MODES: BlendColor32[];
332
- declare const FAST_BLEND_TO_INDEX: {
333
- get: (blend: RegisteredFastBlender) => FastBlendModeIndex;
334
- };
335
- declare const INDEX_TO_FAST_BLEND: {
336
- get: (index: FastBlendModeIndex) => RegisteredFastBlender;
337
- };
334
+ declare const FAST_BLEND_TO_INDEX: BaseBlendToIndexGetter<RegisteredFastBlender>;
335
+ declare const INDEX_TO_FAST_BLEND: BaseIndexToBlendGetter<RegisteredFastBlender>;
338
336
  type FastBlendModes = {
339
337
  [K in keyof typeof BlendMode]: RegisteredFastBlender;
340
338
  };
@@ -388,21 +386,23 @@ declare const subtractPerfect: BlendColor32;
388
386
  declare const dividePerfect: BlendColor32;
389
387
  declare const PERFECT_BLENDER_REGISTRY: readonly [readonly [BlendMode.overwrite, BlendColor32], readonly [BlendMode.sourceOver, BlendColor32], readonly [BlendMode.darken, BlendColor32], readonly [BlendMode.multiply, BlendColor32], readonly [BlendMode.colorBurn, BlendColor32], readonly [BlendMode.linearBurn, BlendColor32], readonly [BlendMode.darkerColor, BlendColor32], readonly [BlendMode.lighten, BlendColor32], readonly [BlendMode.screen, BlendColor32], readonly [BlendMode.colorDodge, BlendColor32], readonly [BlendMode.linearDodge, BlendColor32], readonly [BlendMode.lighterColor, BlendColor32], readonly [BlendMode.overlay, BlendColor32], readonly [BlendMode.softLight, BlendColor32], readonly [BlendMode.hardLight, BlendColor32], readonly [BlendMode.vividLight, BlendColor32], readonly [BlendMode.linearLight, BlendColor32], readonly [BlendMode.pinLight, BlendColor32], readonly [BlendMode.hardMix, BlendColor32], readonly [BlendMode.difference, BlendColor32], readonly [BlendMode.exclusion, BlendColor32], readonly [BlendMode.subtract, BlendColor32], readonly [BlendMode.divide, BlendColor32]];
390
388
  type RegisteredPerfectBlender = typeof PERFECT_BLENDER_REGISTRY[number][1];
391
- type PerfectBlendModeIndex = number & {
392
- readonly __brandBlendModeIndex: unique symbol;
393
- };
394
389
  declare const PERFECT_BLEND_MODES: BlendColor32[];
395
- declare const PERFECT_BLEND_TO_INDEX: {
396
- get: (blend: RegisteredPerfectBlender) => PerfectBlendModeIndex;
397
- };
398
- declare const INDEX_TO_PERFECT_BLEND: {
399
- get: (index: PerfectBlendModeIndex) => RegisteredPerfectBlender;
400
- };
390
+ declare const PERFECT_BLEND_TO_INDEX: BaseBlendToIndexGetter<RegisteredPerfectBlender>;
391
+ declare const INDEX_TO_PERFECT_BLEND: BaseIndexToBlendGetter<RegisteredPerfectBlender>;
401
392
  type PerfectBlendModes = {
402
393
  [K in keyof typeof BlendMode]: RegisteredPerfectBlender;
403
394
  };
404
395
  declare const PERFECT_BLEND_MODE_BY_NAME: PerfectBlendModes;
405
396
 
397
+ type BaseIndexToBlendGetter<B extends BlendColor32> = {
398
+ get: (index: BlendModeIndex) => B;
399
+ };
400
+ type IndexToBlendGetter = typeof INDEX_TO_FAST_BLEND | typeof INDEX_TO_PERFECT_BLEND;
401
+ type BaseBlendToIndexGetter<B extends BlendColor32> = {
402
+ get: (blend: B) => BlendModeIndex;
403
+ };
404
+ type BlendToIndexGetter = typeof FAST_BLEND_TO_INDEX | typeof PERFECT_BLEND_TO_INDEX;
405
+
406
406
  type PixelCanvas = {
407
407
  readonly canvas: HTMLCanvasElement;
408
408
  readonly ctx: CanvasRenderingContext2D;
@@ -687,6 +687,14 @@ declare function makeIndexedImage(imageData: ImageData): IndexedImage;
687
687
  */
688
688
  declare function indexedImageToAverageColor(indexedImage: IndexedImage, includeTransparent?: boolean): Color32;
689
689
 
690
+ /**
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.
695
+ */
696
+ declare function getIndexedImageColorCounts(indexedImage: IndexedImage): Int32Array;
697
+
690
698
  /**
691
699
  * A convenience wrapper that extracts the first {@link File} from an
692
700
  * {@link HTMLInputElement} change event and converts it into {@link ImageData}.
@@ -877,6 +885,16 @@ declare function invertPixelData(pixelData: PixelData): PixelData;
877
885
  */
878
886
  declare function pixelDataToAlphaMask(pixelData: PixelData): AlphaMask;
879
887
 
888
+ declare function reflectPixelDataHorizontal(pixelData: PixelData): void;
889
+ declare function reflectPixelDataVertical(pixelData: PixelData): void;
890
+
891
+ /**
892
+ * Rotates pixel data 90 degrees clockwise.
893
+ * If the image is square, it performs the rotation in-place.
894
+ * If rectangular, it returns a new Uint32Array and updates dimensions.
895
+ */
896
+ declare function rotatePixelData(pixelData: PixelData): void;
897
+
880
898
  /**
881
899
  * Intersects a target rectangle with a boundary, trimming dimensions and masks in-place.
882
900
  * This utility calculates the axis-aligned intersection between the `target` and `bounds`.
@@ -893,4 +911,4 @@ declare function pixelDataToAlphaMask(pixelData: PixelData): AlphaMask;
893
911
  */
894
912
  declare function trimRectBounds<T extends Rect | SelectionRect>(target: T, bounds: Rect): void;
895
913
 
896
- export { type AlphaMask, type AnyMask, type ApplyMaskOptions, type Base64EncodedUInt8Array, type BinaryMask, type BlendColor32, BlendMode, type Color32, type ColorBlendOptions, FAST_BLENDER_REGISTRY, FAST_BLEND_MODES, FAST_BLEND_MODE_BY_NAME, FAST_BLEND_TO_INDEX, type FastBlendModeIndex, type FastBlendModes, type FloodFillImageDataOptions, type FloodFillResult, INDEX_TO_FAST_BLEND, INDEX_TO_PERFECT_BLEND, type ImageDataLike, type IndexedImage, MaskType, PERFECT_BLENDER_REGISTRY, PERFECT_BLEND_MODES, PERFECT_BLEND_MODE_BY_NAME, PERFECT_BLEND_TO_INDEX, type PerfectBlendModeIndex, type PerfectBlendModes, type PixelBlendOptions, type PixelCanvas, PixelData, type PixelOptions, 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, 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, resizeImageData, screenFast, screenPerfect, serializeImageData, serializeNullableImageData, softLightFast, softLightPerfect, sourceOverFast, sourceOverPerfect, subtractFast, subtractPerfect, trimRectBounds, unpackAlpha, unpackBlue, unpackColor, unpackColorTo, unpackGreen, unpackRed, vividLightFast, vividLightPerfect, writeImageDataPixels, writeImageDataToClipboard, writeImgBlobToClipboard };
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 };
@@ -1662,6 +1662,18 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1662
1662
  return packColor(r, g, b, a);
1663
1663
  }
1664
1664
 
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]++;
1673
+ }
1674
+ return frequencies;
1675
+ }
1676
+
1665
1677
  // src/Input/fileInputChangeToImageData.ts
1666
1678
  async function fileInputChangeToImageData(event) {
1667
1679
  const target = event.target;
@@ -2250,6 +2262,84 @@ function invertPixelData(pixelData) {
2250
2262
  }
2251
2263
  return pixelData;
2252
2264
  }
2265
+
2266
+ // src/PixelData/reflectPixelData.ts
2267
+ function reflectPixelDataHorizontal(pixelData) {
2268
+ const width = pixelData.width;
2269
+ const height = pixelData.height;
2270
+ const data = pixelData.data32;
2271
+ const halfWidth = Math.floor(width / 2);
2272
+ for (let y = 0; y < height; y++) {
2273
+ const rowOffset = y * width;
2274
+ for (let x = 0; x < halfWidth; x++) {
2275
+ const leftIdx = rowOffset + x;
2276
+ const rightIdx = rowOffset + (width - 1 - x);
2277
+ const temp = data[leftIdx];
2278
+ data[leftIdx] = data[rightIdx];
2279
+ data[rightIdx] = temp;
2280
+ }
2281
+ }
2282
+ }
2283
+ function reflectPixelDataVertical(pixelData) {
2284
+ const width = pixelData.width;
2285
+ const height = pixelData.height;
2286
+ const data = pixelData.data32;
2287
+ const halfHeight = Math.floor(height / 2);
2288
+ for (let y = 0; y < halfHeight; y++) {
2289
+ const topRowOffset = y * width;
2290
+ const bottomRowOffset = (height - 1 - y) * width;
2291
+ for (let x = 0; x < width; x++) {
2292
+ const topIdx = topRowOffset + x;
2293
+ const bottomIdx = bottomRowOffset + x;
2294
+ const temp = data[topIdx];
2295
+ data[topIdx] = data[bottomIdx];
2296
+ data[bottomIdx] = temp;
2297
+ }
2298
+ }
2299
+ }
2300
+
2301
+ // src/PixelData/rotatePixelData.ts
2302
+ function rotatePixelData(pixelData) {
2303
+ const width = pixelData.width;
2304
+ const height = pixelData.height;
2305
+ const data = pixelData.data32;
2306
+ if (width === height) {
2307
+ rotateSquareInPlace(pixelData);
2308
+ return;
2309
+ }
2310
+ const newWidth = height;
2311
+ const newHeight = width;
2312
+ const newData = new Uint32Array(data.length);
2313
+ for (let y = 0; y < height; y++) {
2314
+ for (let x = 0; x < width; x++) {
2315
+ const oldIdx = y * width + x;
2316
+ const newX = height - 1 - y;
2317
+ const newY = x;
2318
+ const newIdx = newY * newWidth + newX;
2319
+ newData[newIdx] = data[oldIdx];
2320
+ }
2321
+ }
2322
+ pixelData.width = newWidth;
2323
+ pixelData.height = newHeight;
2324
+ pixelData.data32 = newData;
2325
+ }
2326
+ function rotateSquareInPlace(pixelData) {
2327
+ const n = pixelData.width;
2328
+ const data = pixelData.data32;
2329
+ for (let i = 0; i < n / 2; i++) {
2330
+ for (let j = i; j < n - i - 1; j++) {
2331
+ const temp = data[i * n + j];
2332
+ const top = i * n + j;
2333
+ const right = j * n + (n - 1 - i);
2334
+ const bottom = (n - 1 - i) * n + (n - 1 - j);
2335
+ const left = (n - 1 - j) * n + i;
2336
+ data[top] = data[left];
2337
+ data[left] = data[bottom];
2338
+ data[bottom] = data[right];
2339
+ data[right] = temp;
2340
+ }
2341
+ }
2342
+ }
2253
2343
  export {
2254
2344
  BlendMode,
2255
2345
  FAST_BLENDER_REGISTRY,
@@ -2301,6 +2391,7 @@ export {
2301
2391
  fillPixelData,
2302
2392
  floodFillSelection,
2303
2393
  getImageDataFromClipboard,
2394
+ getIndexedImageColorCounts,
2304
2395
  getSupportedPixelFormats,
2305
2396
  hardLightFast,
2306
2397
  hardLightPerfect,
@@ -2342,7 +2433,10 @@ export {
2342
2433
  pinLightFast,
2343
2434
  pinLightPerfect,
2344
2435
  pixelDataToAlphaMask,
2436
+ reflectPixelDataHorizontal,
2437
+ reflectPixelDataVertical,
2345
2438
  resizeImageData,
2439
+ rotatePixelData,
2346
2440
  screenFast,
2347
2441
  screenPerfect,
2348
2442
  serializeImageData,