pixel-data-js 0.13.0 → 0.15.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.
@@ -64,8 +64,9 @@ __export(src_exports, {
64
64
  dividePerfect: () => dividePerfect,
65
65
  exclusionFast: () => exclusionFast,
66
66
  exclusionPerfect: () => exclusionPerfect,
67
- extractImageDataPixels: () => extractImageDataPixels,
67
+ extractImageDataBuffer: () => extractImageDataBuffer,
68
68
  extractMask: () => extractMask,
69
+ extractPixelDataBuffer: () => extractPixelDataBuffer,
69
70
  fileInputChangeToImageData: () => fileInputChangeToImageData,
70
71
  fileToImageData: () => fileToImageData,
71
72
  fillPixelData: () => fillPixelData,
@@ -80,6 +81,7 @@ __export(src_exports, {
80
81
  imageDataToAlphaMask: () => imageDataToAlphaMask,
81
82
  imageDataToDataUrl: () => imageDataToDataUrl,
82
83
  imageDataToImgBlob: () => imageDataToImgBlob,
84
+ imageDataToUInt32Array: () => imageDataToUInt32Array,
83
85
  imgBlobToImageData: () => imgBlobToImageData,
84
86
  indexedImageToAverageColor: () => indexedImageToAverageColor,
85
87
  indexedImageToImageData: () => indexedImageToImageData,
@@ -703,8 +705,8 @@ function color32ToCssRGBA(color) {
703
705
  return `rgba(${r},${g},${b},${alpha})`;
704
706
  }
705
707
 
706
- // src/ImageData/extractImageDataPixels.ts
707
- function extractImageDataPixels(imageData, _x, _y, _w, _h) {
708
+ // src/ImageData/extractImageDataBuffer.ts
709
+ function extractImageDataBuffer(imageData, _x, _y, _w, _h) {
708
710
  const { x, y, w, h } = typeof _x === "object" ? _x : { x: _x, y: _y, w: _w, h: _h };
709
711
  const { width: srcW, height: srcH, data: src } = imageData;
710
712
  if (w <= 0 || h <= 0) return new Uint8ClampedArray(0);
@@ -977,7 +979,7 @@ function floodFillSelection(img, startX, startY, {
977
979
  selectionRect,
978
980
  { x: 0, y: 0, w: width, h: height }
979
981
  );
980
- const extracted = extractImageDataPixels(
982
+ const extracted = extractImageDataBuffer(
981
983
  imageData,
982
984
  selectionRect.x,
983
985
  selectionRect.y,
@@ -1650,6 +1652,16 @@ function imageDataToDataUrl(imageData) {
1650
1652
  }
1651
1653
  imageDataToDataUrl.reset = get.reset;
1652
1654
 
1655
+ // src/ImageData/imageDataToUInt32Array.ts
1656
+ function imageDataToUInt32Array(imageData) {
1657
+ return new Uint32Array(
1658
+ imageData.data.buffer,
1659
+ imageData.data.byteOffset,
1660
+ // Shift right by 2 is a fast bitwise division by 4.
1661
+ imageData.data.byteLength >> 2
1662
+ );
1663
+ }
1664
+
1653
1665
  // src/ImageData/invertImageData.ts
1654
1666
  function invertImageData(imageData) {
1655
1667
  const data = imageData.data;
@@ -2185,28 +2197,34 @@ function mergeMasks(dst, dstWidth, src, opts) {
2185
2197
 
2186
2198
  // src/PixelData/PixelData.ts
2187
2199
  var PixelData = class _PixelData {
2200
+ data32;
2201
+ imageData;
2202
+ get width() {
2203
+ return this.imageData.width;
2204
+ }
2205
+ get height() {
2206
+ return this.imageData.height;
2207
+ }
2188
2208
  constructor(imageData) {
2209
+ this.data32 = imageDataToUInt32Array(imageData);
2189
2210
  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
2211
  }
2199
- data32;
2200
- width;
2201
- height;
2212
+ set(imageData) {
2213
+ this.imageData = imageData;
2214
+ this.data32 = imageDataToUInt32Array(imageData);
2215
+ }
2216
+ /**
2217
+ * Creates a deep copy of the PixelData using the environment's ImageData constructor.
2218
+ */
2202
2219
  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);
2220
+ const buffer = new Uint8ClampedArray(this.imageData.data);
2221
+ const ImageConstructor = typeof ImageData !== "undefined" ? ImageData : this.imageData.constructor;
2222
+ const newImageData = new ImageConstructor(
2223
+ buffer,
2224
+ this.width,
2225
+ this.height
2226
+ );
2227
+ return new _PixelData(newImageData);
2210
2228
  }
2211
2229
  };
2212
2230
 
@@ -2590,6 +2608,38 @@ function clearPixelData(dst, rect) {
2590
2608
  fillPixelData(dst, 0, rect);
2591
2609
  }
2592
2610
 
2611
+ // src/PixelData/extractPixelDataBuffer.ts
2612
+ function extractPixelDataBuffer(source, _x, _y, _w, _h) {
2613
+ const { x, y, w, h } = typeof _x === "object" ? _x : { x: _x, y: _y, w: _w, h: _h };
2614
+ const srcW = source.width;
2615
+ const srcH = source.height;
2616
+ const srcData = source.data32;
2617
+ if (w <= 0 || h <= 0) {
2618
+ return new Uint32Array(0);
2619
+ }
2620
+ const dstImageData = new ImageData(w, h);
2621
+ const dstData = new Uint32Array(dstImageData.data.buffer);
2622
+ const x0 = Math.max(0, x);
2623
+ const y0 = Math.max(0, y);
2624
+ const x1 = Math.min(srcW, x + w);
2625
+ const y1 = Math.min(srcH, y + h);
2626
+ if (x1 <= x0 || y1 <= y0) {
2627
+ return dstData;
2628
+ }
2629
+ const copyWidth = x1 - x0;
2630
+ const copyHeight = y1 - y0;
2631
+ for (let row = 0; row < copyHeight; row++) {
2632
+ const srcRow = y0 + row;
2633
+ const srcStart = srcRow * srcW + x0;
2634
+ const dstRow = y0 - y + row;
2635
+ const dstCol = x0 - x;
2636
+ const dstStart = dstRow * w + dstCol;
2637
+ const chunk = srcData.subarray(srcStart, srcStart + copyWidth);
2638
+ dstData.set(chunk, dstStart);
2639
+ }
2640
+ return dstData;
2641
+ }
2642
+
2593
2643
  // src/PixelData/invertPixelData.ts
2594
2644
  function invertPixelData(pixelData) {
2595
2645
  const data32 = pixelData.data32;
@@ -2638,11 +2688,11 @@ function reflectPixelDataVertical(pixelData) {
2638
2688
  // src/PixelData/resamplePixelData.ts
2639
2689
  function resamplePixelData(pixelData, factor) {
2640
2690
  const { data, width, height } = resample32(pixelData.data32, pixelData.width, pixelData.height, factor);
2641
- return new PixelData({
2691
+ return new PixelData(new ImageData(
2692
+ new Uint8ClampedArray(data.buffer),
2642
2693
  width,
2643
- height,
2644
- data: new Uint8ClampedArray(data.buffer)
2645
- });
2694
+ height
2695
+ ));
2646
2696
  }
2647
2697
 
2648
2698
  // src/PixelData/rotatePixelData.ts
@@ -2656,30 +2706,33 @@ function rotatePixelData(pixelData) {
2656
2706
  }
2657
2707
  const newWidth = height;
2658
2708
  const newHeight = width;
2659
- const newData = new Uint32Array(data.length);
2709
+ const newData32 = new Uint32Array(data.length);
2660
2710
  for (let y = 0; y < height; y++) {
2661
2711
  for (let x = 0; x < width; x++) {
2662
2712
  const oldIdx = y * width + x;
2663
2713
  const newX = height - 1 - y;
2664
2714
  const newY = x;
2665
2715
  const newIdx = newY * newWidth + newX;
2666
- newData[newIdx] = data[oldIdx];
2716
+ newData32[newIdx] = data[oldIdx];
2667
2717
  }
2668
2718
  }
2669
- pixelData.width = newWidth;
2670
- pixelData.height = newHeight;
2671
- pixelData.data32 = newData;
2719
+ const newImageData = new ImageData(
2720
+ new Uint8ClampedArray(newData32.buffer),
2721
+ newWidth,
2722
+ newHeight
2723
+ );
2724
+ pixelData.set(newImageData);
2672
2725
  }
2673
2726
  function rotateSquareInPlace(pixelData) {
2674
2727
  const n = pixelData.width;
2675
2728
  const data = pixelData.data32;
2676
2729
  for (let i = 0; i < n / 2; i++) {
2677
2730
  for (let j = i; j < n - i - 1; j++) {
2678
- const temp = data[i * n + j];
2679
2731
  const top = i * n + j;
2680
2732
  const right = j * n + (n - 1 - i);
2681
2733
  const bottom = (n - 1 - i) * n + (n - 1 - j);
2682
2734
  const left = (n - 1 - j) * n + i;
2735
+ const temp = data[top];
2683
2736
  data[top] = data[left];
2684
2737
  data[left] = data[bottom];
2685
2738
  data[bottom] = data[right];
@@ -2733,8 +2786,9 @@ function rotateSquareInPlace(pixelData) {
2733
2786
  dividePerfect,
2734
2787
  exclusionFast,
2735
2788
  exclusionPerfect,
2736
- extractImageDataPixels,
2789
+ extractImageDataBuffer,
2737
2790
  extractMask,
2791
+ extractPixelDataBuffer,
2738
2792
  fileInputChangeToImageData,
2739
2793
  fileToImageData,
2740
2794
  fillPixelData,
@@ -2749,6 +2803,7 @@ function rotateSquareInPlace(pixelData) {
2749
2803
  imageDataToAlphaMask,
2750
2804
  imageDataToDataUrl,
2751
2805
  imageDataToImgBlob,
2806
+ imageDataToUInt32Array,
2752
2807
  imgBlobToImageData,
2753
2808
  indexedImageToAverageColor,
2754
2809
  indexedImageToImageData,