pixel-data-js 0.10.1 → 0.11.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.
@@ -699,6 +699,11 @@ declare function getIndexedImageColorCounts(indexedImage: IndexedImage): Int32Ar
699
699
  */
700
700
  declare function indexedImageToAverageColor(indexedImage: IndexedImage, includeTransparent?: boolean): Color32;
701
701
 
702
+ /**
703
+ * Resamples an IndexedImage by a specific factor using nearest neighbor
704
+ * Factor > 1 upscales, Factor < 1 downscales.
705
+ */
706
+
702
707
  /**
703
708
  * Resamples an IndexedImage by a specific factor using nearest neighbor
704
709
  * Factor > 1 upscales, Factor < 1 downscales.
@@ -902,6 +907,11 @@ declare function reflectPixelDataVertical(pixelData: PixelData): void;
902
907
  * Resamples an PixelData by a specific factor using nearest neighbor
903
908
  * Factor > 1 upscales, Factor < 1 downscales.
904
909
  */
910
+
911
+ /**
912
+ * Resamples PixelData by a specific factor using nearest neighbor.
913
+ * Factor > 1 upscales, Factor < 1 downscales.
914
+ */
905
915
  declare function resamplePixelData(pixelData: PixelData, factor: number): PixelData;
906
916
 
907
917
  /**
@@ -1495,29 +1495,39 @@ function invertImageData(imageData) {
1495
1495
  return imageData;
1496
1496
  }
1497
1497
 
1498
- // src/ImageData/resampleImageData.ts
1499
- function resampleImageData(source, factor) {
1500
- const srcW = source.width;
1501
- const srcH = source.height;
1498
+ // src/Internal/resample32.ts
1499
+ var resample32Scratch = {
1500
+ data: null,
1501
+ width: 0,
1502
+ height: 0
1503
+ };
1504
+ function resample32(srcData32, srcW, srcH, factor) {
1502
1505
  const dstW = Math.max(1, srcW * factor | 0);
1503
1506
  const dstH = Math.max(1, srcH * factor | 0);
1504
- const srcData = source.data;
1505
- const dstData = new Uint8ClampedArray(dstW * dstH * 4);
1507
+ const dstData = new Int32Array(dstW * dstH);
1508
+ const scaleX = srcW / dstW;
1509
+ const scaleY = srcH / dstH;
1506
1510
  for (let y = 0; y < dstH; y++) {
1507
- const srcY = y / factor | 0;
1508
- const srcRowOffset = srcY * srcW * 4;
1509
- const dstRowOffset = y * dstW * 4;
1511
+ const srcY = Math.min(srcH - 1, y * scaleY | 0);
1512
+ const srcRowOffset = srcY * srcW;
1513
+ const dstRowOffset = y * dstW;
1510
1514
  for (let x = 0; x < dstW; x++) {
1511
- const srcX = x / factor | 0;
1512
- const srcIdx = srcRowOffset + srcX * 4;
1513
- const dstIdx = dstRowOffset + x * 4;
1514
- dstData[dstIdx] = srcData[srcIdx];
1515
- dstData[dstIdx + 1] = srcData[srcIdx + 1];
1516
- dstData[dstIdx + 2] = srcData[srcIdx + 2];
1517
- dstData[dstIdx + 3] = srcData[srcIdx + 3];
1515
+ const srcX = Math.min(srcW - 1, x * scaleX | 0);
1516
+ dstData[dstRowOffset + x] = srcData32[srcRowOffset + srcX];
1518
1517
  }
1519
1518
  }
1520
- return new ImageData(dstData, dstW, dstH);
1519
+ resample32Scratch.data = dstData;
1520
+ resample32Scratch.width = dstW;
1521
+ resample32Scratch.height = dstH;
1522
+ return resample32Scratch;
1523
+ }
1524
+
1525
+ // src/ImageData/resampleImageData.ts
1526
+ function resampleImageData(source, factor) {
1527
+ const src32 = new Uint32Array(source.data.buffer);
1528
+ const { data, width, height } = resample32(src32, source.width, source.height, factor);
1529
+ const uint8ClampedArray = new Uint8ClampedArray(data.buffer);
1530
+ return new ImageData(uint8ClampedArray, width, height);
1521
1531
  }
1522
1532
 
1523
1533
  // src/ImageData/resizeImageData.ts
@@ -1703,25 +1713,16 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1703
1713
 
1704
1714
  // src/IndexedImage/resampleIndexedImage.ts
1705
1715
  function resampleIndexedImage(source, factor) {
1706
- const srcW = source.width;
1707
- const srcH = source.height;
1708
- const dstW = srcW * factor;
1709
- const dstH = srcH * factor;
1710
- const srcData = source.data;
1711
- const dstData = new Int32Array(dstW * dstH);
1712
- for (let y = 0; y < dstH; y++) {
1713
- const srcY = y / factor | 0;
1714
- const rowOffset = srcY * srcW;
1715
- const dstOffset = y * dstW;
1716
- for (let x = 0; x < dstW; x++) {
1717
- const srcX = x / factor | 0;
1718
- dstData[dstOffset + x] = srcData[rowOffset + srcX];
1719
- }
1720
- }
1716
+ const { data, width, height } = resample32(
1717
+ source.data,
1718
+ source.width,
1719
+ source.height,
1720
+ factor
1721
+ );
1721
1722
  return {
1722
- width: dstW,
1723
- height: dstH,
1724
- data: dstData,
1723
+ width,
1724
+ height,
1725
+ data,
1725
1726
  palette: source.palette,
1726
1727
  transparentPalletIndex: source.transparentPalletIndex
1727
1728
  };
@@ -2353,23 +2354,11 @@ function reflectPixelDataVertical(pixelData) {
2353
2354
 
2354
2355
  // src/PixelData/resamplePixelData.ts
2355
2356
  function resamplePixelData(pixelData, factor) {
2356
- const dstW = Math.max(1, pixelData.width * factor | 0);
2357
- const dstH = Math.max(1, pixelData.height * factor | 0);
2358
- const dstBuffer = new Uint8ClampedArray(dstW * dstH * 4);
2359
- const dstData32 = new Uint32Array(dstBuffer.buffer);
2360
- for (let y = 0; y < dstH; y++) {
2361
- const srcY = y / factor | 0;
2362
- const srcRowOffset = srcY * pixelData.width;
2363
- const dstRowOffset = y * dstW;
2364
- for (let x = 0; x < dstW; x++) {
2365
- const srcX = x / factor | 0;
2366
- dstData32[dstRowOffset + x] = pixelData.data32[srcRowOffset + srcX];
2367
- }
2368
- }
2357
+ const { data, width, height } = resample32(pixelData.data32, pixelData.width, pixelData.height, factor);
2369
2358
  return new PixelData({
2370
- data: dstBuffer,
2371
- width: dstW,
2372
- height: dstH
2359
+ width,
2360
+ height,
2361
+ data: new Uint8ClampedArray(data.buffer)
2373
2362
  });
2374
2363
  }
2375
2364