pixel-data-js 0.13.0 → 0.14.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.
@@ -205,11 +205,15 @@ declare function color32ToHex(color: Color32): string;
205
205
  declare function color32ToCssRGBA(color: Color32): string;
206
206
 
207
207
  declare class PixelData {
208
- readonly imageData: ImageDataLike;
209
208
  data32: Uint32Array;
210
- width: number;
211
- height: number;
212
- constructor(imageData: ImageDataLike);
209
+ imageData: ImageData;
210
+ get width(): number;
211
+ get height(): number;
212
+ constructor(imageData: ImageData);
213
+ set(imageData: ImageData): void;
214
+ /**
215
+ * Creates a deep copy of the PixelData using the environment's ImageData constructor.
216
+ */
213
217
  copy(): PixelData;
214
218
  }
215
219
 
@@ -567,6 +571,11 @@ declare namespace imageDataToDataUrl {
567
571
  */
568
572
  declare function imageDataToImgBlob(imageData: ImageData): Promise<Blob>;
569
573
 
574
+ /**
575
+ * converts {@link ImageData} to a faster Uint32Array
576
+ */
577
+ declare function imageDataToUInt32Array(imageData: ImageDataLike): Uint32Array;
578
+
570
579
  /**
571
580
  * Decodes a {@link Blob} (typically PNG) back into an {@link ImageData} object.
572
581
  *
@@ -605,7 +614,7 @@ declare function resampleImageData(source: ImageData, factor: number): ImageData
605
614
  * instead, it crops or pads the image based on the new dimensions and
606
615
  * provides an offset for repositioning.
607
616
  *
608
- * @param current The source {@link ImageData} to resize.
617
+ * @param current The source {@link ImageDataLike} to resize.
609
618
  * @param newWidth The target width in pixels.
610
619
  * @param newHeight The target height in pixels.
611
620
  * @param offsetX The horizontal offset for placing the
@@ -629,7 +638,7 @@ declare function resampleImageData(source: ImageData, factor: number): ImageData
629
638
  * );
630
639
  * ```
631
640
  */
632
- declare function resizeImageData(current: ImageData, newWidth: number, newHeight: number, offsetX?: number, offsetY?: number): ImageData;
641
+ declare function resizeImageData(current: ImageDataLike, newWidth: number, newHeight: number, offsetX?: number, offsetY?: number): ImageData;
633
642
 
634
643
  declare function base64EncodeArrayBuffer(buffer: ArrayBufferLike): Base64EncodedUInt8Array;
635
644
  declare function base64DecodeArrayBuffer(encoded: Base64EncodedUInt8Array): Uint8ClampedArray<ArrayBuffer>;
@@ -969,7 +978,7 @@ declare function resamplePixelData(pixelData: PixelData, factor: number): PixelD
969
978
  /**
970
979
  * Rotates pixel data 90 degrees clockwise.
971
980
  * If the image is square, it performs the rotation in-place.
972
- * If rectangular, it returns a new Uint32Array and updates dimensions.
981
+ * If rectangular, it replaces the internal ImageData with a new rotated version.
973
982
  */
974
983
  declare function rotatePixelData(pixelData: PixelData): void;
975
984
 
@@ -989,4 +998,4 @@ declare function rotatePixelData(pixelData: PixelData): void;
989
998
  */
990
999
  declare function trimRectBounds<T extends Rect | SelectionRect>(target: T, bounds: Rect): void;
991
1000
 
992
- 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, 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 ReusableImageData, 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, indexedImageToImageData, invertAlphaMask, invertBinaryMask, invertImageData, invertPixelData, lerpColor32, lerpColor32Fast, lightenFast, lightenPerfect, lighterFast, lighterPerfect, linearBurnFast, linearBurnPerfect, linearDodgeFast, linearDodgePerfect, linearLightFast, linearLightPerfect, makePixelCanvas, makeReusableCanvas, makeReusableImageData, 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, writeImageData, writeImageDataPixels, writeImageDataToClipboard, writeImgBlobToClipboard };
1001
+ 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, 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 ReusableImageData, 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, imageDataToUInt32Array, imgBlobToImageData, indexedImageToAverageColor, indexedImageToImageData, invertAlphaMask, invertBinaryMask, invertImageData, invertPixelData, lerpColor32, lerpColor32Fast, lightenFast, lightenPerfect, lighterFast, lighterPerfect, linearBurnFast, linearBurnPerfect, linearDodgeFast, linearDodgePerfect, linearLightFast, linearLightPerfect, makePixelCanvas, makeReusableCanvas, makeReusableImageData, 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, writeImageData, writeImageDataPixels, writeImageDataToClipboard, writeImgBlobToClipboard };
@@ -1501,6 +1501,16 @@ function imageDataToDataUrl(imageData) {
1501
1501
  }
1502
1502
  imageDataToDataUrl.reset = get.reset;
1503
1503
 
1504
+ // src/ImageData/imageDataToUInt32Array.ts
1505
+ function imageDataToUInt32Array(imageData) {
1506
+ return new Uint32Array(
1507
+ imageData.data.buffer,
1508
+ imageData.data.byteOffset,
1509
+ // Shift right by 2 is a fast bitwise division by 4.
1510
+ imageData.data.byteLength >> 2
1511
+ );
1512
+ }
1513
+
1504
1514
  // src/ImageData/invertImageData.ts
1505
1515
  function invertImageData(imageData) {
1506
1516
  const data = imageData.data;
@@ -2036,28 +2046,34 @@ function mergeMasks(dst, dstWidth, src, opts) {
2036
2046
 
2037
2047
  // src/PixelData/PixelData.ts
2038
2048
  var PixelData = class _PixelData {
2049
+ data32;
2050
+ imageData;
2051
+ get width() {
2052
+ return this.imageData.width;
2053
+ }
2054
+ get height() {
2055
+ return this.imageData.height;
2056
+ }
2039
2057
  constructor(imageData) {
2058
+ this.data32 = imageDataToUInt32Array(imageData);
2040
2059
  this.imageData = imageData;
2041
- this.width = imageData.width;
2042
- this.height = imageData.height;
2043
- this.data32 = new Uint32Array(
2044
- imageData.data.buffer,
2045
- imageData.data.byteOffset,
2046
- // Shift right by 2 is a fast bitwise division by 4.
2047
- imageData.data.byteLength >> 2
2048
- );
2049
2060
  }
2050
- data32;
2051
- width;
2052
- height;
2061
+ set(imageData) {
2062
+ this.imageData = imageData;
2063
+ this.data32 = imageDataToUInt32Array(imageData);
2064
+ }
2065
+ /**
2066
+ * Creates a deep copy of the PixelData using the environment's ImageData constructor.
2067
+ */
2053
2068
  copy() {
2054
- const buffer = new Uint8ClampedArray(this.data32.buffer.slice(0));
2055
- const imageData = {
2056
- data: buffer,
2057
- width: this.width,
2058
- height: this.height
2059
- };
2060
- return new _PixelData(imageData);
2069
+ const buffer = new Uint8ClampedArray(this.imageData.data);
2070
+ const ImageConstructor = typeof ImageData !== "undefined" ? ImageData : this.imageData.constructor;
2071
+ const newImageData = new ImageConstructor(
2072
+ buffer,
2073
+ this.width,
2074
+ this.height
2075
+ );
2076
+ return new _PixelData(newImageData);
2061
2077
  }
2062
2078
  };
2063
2079
 
@@ -2489,11 +2505,11 @@ function reflectPixelDataVertical(pixelData) {
2489
2505
  // src/PixelData/resamplePixelData.ts
2490
2506
  function resamplePixelData(pixelData, factor) {
2491
2507
  const { data, width, height } = resample32(pixelData.data32, pixelData.width, pixelData.height, factor);
2492
- return new PixelData({
2508
+ return new PixelData(new ImageData(
2509
+ new Uint8ClampedArray(data.buffer),
2493
2510
  width,
2494
- height,
2495
- data: new Uint8ClampedArray(data.buffer)
2496
- });
2511
+ height
2512
+ ));
2497
2513
  }
2498
2514
 
2499
2515
  // src/PixelData/rotatePixelData.ts
@@ -2507,30 +2523,33 @@ function rotatePixelData(pixelData) {
2507
2523
  }
2508
2524
  const newWidth = height;
2509
2525
  const newHeight = width;
2510
- const newData = new Uint32Array(data.length);
2526
+ const newData32 = new Uint32Array(data.length);
2511
2527
  for (let y = 0; y < height; y++) {
2512
2528
  for (let x = 0; x < width; x++) {
2513
2529
  const oldIdx = y * width + x;
2514
2530
  const newX = height - 1 - y;
2515
2531
  const newY = x;
2516
2532
  const newIdx = newY * newWidth + newX;
2517
- newData[newIdx] = data[oldIdx];
2533
+ newData32[newIdx] = data[oldIdx];
2518
2534
  }
2519
2535
  }
2520
- pixelData.width = newWidth;
2521
- pixelData.height = newHeight;
2522
- pixelData.data32 = newData;
2536
+ const newImageData = new ImageData(
2537
+ new Uint8ClampedArray(newData32.buffer),
2538
+ newWidth,
2539
+ newHeight
2540
+ );
2541
+ pixelData.set(newImageData);
2523
2542
  }
2524
2543
  function rotateSquareInPlace(pixelData) {
2525
2544
  const n = pixelData.width;
2526
2545
  const data = pixelData.data32;
2527
2546
  for (let i = 0; i < n / 2; i++) {
2528
2547
  for (let j = i; j < n - i - 1; j++) {
2529
- const temp = data[i * n + j];
2530
2548
  const top = i * n + j;
2531
2549
  const right = j * n + (n - 1 - i);
2532
2550
  const bottom = (n - 1 - i) * n + (n - 1 - j);
2533
2551
  const left = (n - 1 - j) * n + i;
2552
+ const temp = data[top];
2534
2553
  data[top] = data[left];
2535
2554
  data[left] = data[bottom];
2536
2555
  data[bottom] = data[right];
@@ -2599,6 +2618,7 @@ export {
2599
2618
  imageDataToAlphaMask,
2600
2619
  imageDataToDataUrl,
2601
2620
  imageDataToImgBlob,
2621
+ imageDataToUInt32Array,
2602
2622
  imgBlobToImageData,
2603
2623
  indexedImageToAverageColor,
2604
2624
  indexedImageToImageData,