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.
@@ -80,6 +80,7 @@ __export(src_exports, {
80
80
  imageDataToAlphaMask: () => imageDataToAlphaMask,
81
81
  imageDataToDataUrl: () => imageDataToDataUrl,
82
82
  imageDataToImgBlob: () => imageDataToImgBlob,
83
+ imageDataToUInt32Array: () => imageDataToUInt32Array,
83
84
  imgBlobToImageData: () => imgBlobToImageData,
84
85
  indexedImageToAverageColor: () => indexedImageToAverageColor,
85
86
  indexedImageToImageData: () => indexedImageToImageData,
@@ -1650,6 +1651,16 @@ function imageDataToDataUrl(imageData) {
1650
1651
  }
1651
1652
  imageDataToDataUrl.reset = get.reset;
1652
1653
 
1654
+ // src/ImageData/imageDataToUInt32Array.ts
1655
+ function imageDataToUInt32Array(imageData) {
1656
+ return new Uint32Array(
1657
+ imageData.data.buffer,
1658
+ imageData.data.byteOffset,
1659
+ // Shift right by 2 is a fast bitwise division by 4.
1660
+ imageData.data.byteLength >> 2
1661
+ );
1662
+ }
1663
+
1653
1664
  // src/ImageData/invertImageData.ts
1654
1665
  function invertImageData(imageData) {
1655
1666
  const data = imageData.data;
@@ -2185,28 +2196,34 @@ function mergeMasks(dst, dstWidth, src, opts) {
2185
2196
 
2186
2197
  // src/PixelData/PixelData.ts
2187
2198
  var PixelData = class _PixelData {
2199
+ data32;
2200
+ imageData;
2201
+ get width() {
2202
+ return this.imageData.width;
2203
+ }
2204
+ get height() {
2205
+ return this.imageData.height;
2206
+ }
2188
2207
  constructor(imageData) {
2208
+ this.data32 = imageDataToUInt32Array(imageData);
2189
2209
  this.imageData = imageData;
2190
- this.width = imageData.width;
2191
- this.height = imageData.height;
2192
- this.data32 = new Uint32Array(
2193
- imageData.data.buffer,
2194
- imageData.data.byteOffset,
2195
- // Shift right by 2 is a fast bitwise division by 4.
2196
- imageData.data.byteLength >> 2
2197
- );
2198
2210
  }
2199
- data32;
2200
- width;
2201
- height;
2211
+ set(imageData) {
2212
+ this.imageData = imageData;
2213
+ this.data32 = imageDataToUInt32Array(imageData);
2214
+ }
2215
+ /**
2216
+ * Creates a deep copy of the PixelData using the environment's ImageData constructor.
2217
+ */
2202
2218
  copy() {
2203
- const buffer = new Uint8ClampedArray(this.data32.buffer.slice(0));
2204
- const imageData = {
2205
- data: buffer,
2206
- width: this.width,
2207
- height: this.height
2208
- };
2209
- return new _PixelData(imageData);
2219
+ const buffer = new Uint8ClampedArray(this.imageData.data);
2220
+ const ImageConstructor = typeof ImageData !== "undefined" ? ImageData : this.imageData.constructor;
2221
+ const newImageData = new ImageConstructor(
2222
+ buffer,
2223
+ this.width,
2224
+ this.height
2225
+ );
2226
+ return new _PixelData(newImageData);
2210
2227
  }
2211
2228
  };
2212
2229
 
@@ -2638,11 +2655,11 @@ function reflectPixelDataVertical(pixelData) {
2638
2655
  // src/PixelData/resamplePixelData.ts
2639
2656
  function resamplePixelData(pixelData, factor) {
2640
2657
  const { data, width, height } = resample32(pixelData.data32, pixelData.width, pixelData.height, factor);
2641
- return new PixelData({
2658
+ return new PixelData(new ImageData(
2659
+ new Uint8ClampedArray(data.buffer),
2642
2660
  width,
2643
- height,
2644
- data: new Uint8ClampedArray(data.buffer)
2645
- });
2661
+ height
2662
+ ));
2646
2663
  }
2647
2664
 
2648
2665
  // src/PixelData/rotatePixelData.ts
@@ -2656,30 +2673,33 @@ function rotatePixelData(pixelData) {
2656
2673
  }
2657
2674
  const newWidth = height;
2658
2675
  const newHeight = width;
2659
- const newData = new Uint32Array(data.length);
2676
+ const newData32 = new Uint32Array(data.length);
2660
2677
  for (let y = 0; y < height; y++) {
2661
2678
  for (let x = 0; x < width; x++) {
2662
2679
  const oldIdx = y * width + x;
2663
2680
  const newX = height - 1 - y;
2664
2681
  const newY = x;
2665
2682
  const newIdx = newY * newWidth + newX;
2666
- newData[newIdx] = data[oldIdx];
2683
+ newData32[newIdx] = data[oldIdx];
2667
2684
  }
2668
2685
  }
2669
- pixelData.width = newWidth;
2670
- pixelData.height = newHeight;
2671
- pixelData.data32 = newData;
2686
+ const newImageData = new ImageData(
2687
+ new Uint8ClampedArray(newData32.buffer),
2688
+ newWidth,
2689
+ newHeight
2690
+ );
2691
+ pixelData.set(newImageData);
2672
2692
  }
2673
2693
  function rotateSquareInPlace(pixelData) {
2674
2694
  const n = pixelData.width;
2675
2695
  const data = pixelData.data32;
2676
2696
  for (let i = 0; i < n / 2; i++) {
2677
2697
  for (let j = i; j < n - i - 1; j++) {
2678
- const temp = data[i * n + j];
2679
2698
  const top = i * n + j;
2680
2699
  const right = j * n + (n - 1 - i);
2681
2700
  const bottom = (n - 1 - i) * n + (n - 1 - j);
2682
2701
  const left = (n - 1 - j) * n + i;
2702
+ const temp = data[top];
2683
2703
  data[top] = data[left];
2684
2704
  data[left] = data[bottom];
2685
2705
  data[bottom] = data[right];
@@ -2749,6 +2769,7 @@ function rotateSquareInPlace(pixelData) {
2749
2769
  imageDataToAlphaMask,
2750
2770
  imageDataToDataUrl,
2751
2771
  imageDataToImgBlob,
2772
+ imageDataToUInt32Array,
2752
2773
  imgBlobToImageData,
2753
2774
  indexedImageToAverageColor,
2754
2775
  indexedImageToImageData,