pixel-data-js 0.8.0 → 0.9.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.
@@ -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;
@@ -670,6 +670,9 @@ type IndexedImage = {
670
670
  */
671
671
  transparentPalletIndex: number;
672
672
  };
673
+ /**
674
+ * Converts standard ImageData into an IndexedImage format.
675
+ */
673
676
  /**
674
677
  * Converts standard ImageData into an IndexedImage format.
675
678
  */
@@ -874,6 +877,16 @@ declare function invertPixelData(pixelData: PixelData): PixelData;
874
877
  */
875
878
  declare function pixelDataToAlphaMask(pixelData: PixelData): AlphaMask;
876
879
 
880
+ declare function reflectPixelDataHorizontal(pixelData: PixelData): void;
881
+ declare function reflectPixelDataVertical(pixelData: PixelData): void;
882
+
883
+ /**
884
+ * Rotates pixel data 90 degrees clockwise.
885
+ * If the image is square, it performs the rotation in-place.
886
+ * If rectangular, it returns a new Uint32Array and updates dimensions.
887
+ */
888
+ declare function rotatePixelData(pixelData: PixelData): void;
889
+
877
890
  /**
878
891
  * Intersects a target rectangle with a boundary, trimming dimensions and masks in-place.
879
892
  * This utility calculates the axis-aligned intersection between the `target` and `bounds`.
@@ -890,4 +903,4 @@ declare function pixelDataToAlphaMask(pixelData: PixelData): AlphaMask;
890
903
  */
891
904
  declare function trimRectBounds<T extends Rect | SelectionRect>(target: T, bounds: Rect): void;
892
905
 
893
- 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 };
906
+ 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, 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 };
@@ -1595,24 +1595,22 @@ function makeIndexedImage(imageData) {
1595
1595
  const rawData = new Uint32Array(imageData.data.buffer);
1596
1596
  const indexedData = new Int32Array(rawData.length);
1597
1597
  const colorMap = /* @__PURE__ */ new Map();
1598
- const tempPalette = [];
1599
1598
  const transparentColor = 0;
1600
1599
  const transparentPalletIndex = 0;
1601
1600
  colorMap.set(transparentColor, transparentPalletIndex);
1602
- tempPalette.push(transparentColor);
1603
1601
  for (let i = 0; i < rawData.length; i++) {
1604
1602
  const pixel = rawData[i];
1605
- const isTransparent = pixel >>> 24 === 0;
1603
+ const alpha = pixel >>> 24 & 255;
1604
+ const isTransparent = alpha === 0;
1606
1605
  const colorKey = isTransparent ? transparentColor : pixel;
1607
1606
  let id = colorMap.get(colorKey);
1608
1607
  if (id === void 0) {
1609
1608
  id = colorMap.size;
1610
- tempPalette.push(colorKey);
1611
1609
  colorMap.set(colorKey, id);
1612
1610
  }
1613
1611
  indexedData[i] = id;
1614
1612
  }
1615
- const palette = new Int32Array(tempPalette);
1613
+ const palette = new Int32Array(colorMap.keys());
1616
1614
  return {
1617
1615
  width,
1618
1616
  height,
@@ -1625,7 +1623,7 @@ function makeIndexedImage(imageData) {
1625
1623
  // src/IndexedImage/indexedImageToAverageColor.ts
1626
1624
  function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1627
1625
  const { data, palette, transparentPalletIndex } = indexedImage;
1628
- const counts = new Uint32Array(palette.length / 4);
1626
+ const counts = new Uint32Array(palette.length);
1629
1627
  for (let i = 0; i < data.length; i++) {
1630
1628
  const id = data[i];
1631
1629
  counts[id]++;
@@ -1643,11 +1641,11 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1643
1641
  if (!includeTransparent && id === transparentPalletIndex) {
1644
1642
  continue;
1645
1643
  }
1646
- const pIdx = id * 4;
1647
- const r2 = palette[pIdx];
1648
- const g2 = palette[pIdx + 1];
1649
- const b2 = palette[pIdx + 2];
1650
- const a2 = palette[pIdx + 3];
1644
+ const color = palette[id] >>> 0;
1645
+ const r2 = color & 255;
1646
+ const g2 = color >> 8 & 255;
1647
+ const b2 = color >> 16 & 255;
1648
+ const a2 = color >> 24 & 255;
1651
1649
  rSum += r2 * weight;
1652
1650
  gSum += g2 * weight;
1653
1651
  bSum += b2 * weight;
@@ -2252,6 +2250,84 @@ function invertPixelData(pixelData) {
2252
2250
  }
2253
2251
  return pixelData;
2254
2252
  }
2253
+
2254
+ // src/PixelData/reflectPixelData.ts
2255
+ function reflectPixelDataHorizontal(pixelData) {
2256
+ const width = pixelData.width;
2257
+ const height = pixelData.height;
2258
+ const data = pixelData.data32;
2259
+ const halfWidth = Math.floor(width / 2);
2260
+ for (let y = 0; y < height; y++) {
2261
+ const rowOffset = y * width;
2262
+ for (let x = 0; x < halfWidth; x++) {
2263
+ const leftIdx = rowOffset + x;
2264
+ const rightIdx = rowOffset + (width - 1 - x);
2265
+ const temp = data[leftIdx];
2266
+ data[leftIdx] = data[rightIdx];
2267
+ data[rightIdx] = temp;
2268
+ }
2269
+ }
2270
+ }
2271
+ function reflectPixelDataVertical(pixelData) {
2272
+ const width = pixelData.width;
2273
+ const height = pixelData.height;
2274
+ const data = pixelData.data32;
2275
+ const halfHeight = Math.floor(height / 2);
2276
+ for (let y = 0; y < halfHeight; y++) {
2277
+ const topRowOffset = y * width;
2278
+ const bottomRowOffset = (height - 1 - y) * width;
2279
+ for (let x = 0; x < width; x++) {
2280
+ const topIdx = topRowOffset + x;
2281
+ const bottomIdx = bottomRowOffset + x;
2282
+ const temp = data[topIdx];
2283
+ data[topIdx] = data[bottomIdx];
2284
+ data[bottomIdx] = temp;
2285
+ }
2286
+ }
2287
+ }
2288
+
2289
+ // src/PixelData/rotatePixelData.ts
2290
+ function rotatePixelData(pixelData) {
2291
+ const width = pixelData.width;
2292
+ const height = pixelData.height;
2293
+ const data = pixelData.data32;
2294
+ if (width === height) {
2295
+ rotateSquareInPlace(pixelData);
2296
+ return;
2297
+ }
2298
+ const newWidth = height;
2299
+ const newHeight = width;
2300
+ const newData = new Uint32Array(data.length);
2301
+ for (let y = 0; y < height; y++) {
2302
+ for (let x = 0; x < width; x++) {
2303
+ const oldIdx = y * width + x;
2304
+ const newX = height - 1 - y;
2305
+ const newY = x;
2306
+ const newIdx = newY * newWidth + newX;
2307
+ newData[newIdx] = data[oldIdx];
2308
+ }
2309
+ }
2310
+ pixelData.width = newWidth;
2311
+ pixelData.height = newHeight;
2312
+ pixelData.data32 = newData;
2313
+ }
2314
+ function rotateSquareInPlace(pixelData) {
2315
+ const n = pixelData.width;
2316
+ const data = pixelData.data32;
2317
+ for (let i = 0; i < n / 2; i++) {
2318
+ for (let j = i; j < n - i - 1; j++) {
2319
+ const temp = data[i * n + j];
2320
+ const top = i * n + j;
2321
+ const right = j * n + (n - 1 - i);
2322
+ const bottom = (n - 1 - i) * n + (n - 1 - j);
2323
+ const left = (n - 1 - j) * n + i;
2324
+ data[top] = data[left];
2325
+ data[left] = data[bottom];
2326
+ data[bottom] = data[right];
2327
+ data[right] = temp;
2328
+ }
2329
+ }
2330
+ }
2255
2331
  export {
2256
2332
  BlendMode,
2257
2333
  FAST_BLENDER_REGISTRY,
@@ -2344,7 +2420,10 @@ export {
2344
2420
  pinLightFast,
2345
2421
  pinLightPerfect,
2346
2422
  pixelDataToAlphaMask,
2423
+ reflectPixelDataHorizontal,
2424
+ reflectPixelDataVertical,
2347
2425
  resizeImageData,
2426
+ rotatePixelData,
2348
2427
  screenFast,
2349
2428
  screenPerfect,
2350
2429
  serializeImageData,