pixel-data-js 0.9.1 → 0.10.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pixel-data-js",
3
3
  "type": "module",
4
- "version": "0.9.1",
4
+ "version": "0.10.0",
5
5
  "packageManager": "pnpm@10.30.0",
6
6
  "description": "JS Pixel and ImageData operations",
7
7
  "author": {
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Resamples ImageData by a specific factor.
3
+ * Factor > 1 upscales, Factor < 1 downscales.
4
+ */
5
+ export function resampleImageData(
6
+ source: ImageData,
7
+ factor: number,
8
+ ): ImageData {
9
+ const srcW = source.width
10
+ const srcH = source.height
11
+ const dstW = Math.max(1, (srcW * factor) | 0)
12
+ const dstH = Math.max(1, (srcH * factor) | 0)
13
+ const srcData = source.data
14
+ const dstData = new Uint8ClampedArray(dstW * dstH * 4)
15
+
16
+ for (let y = 0; y < dstH; y++) {
17
+ const srcY = (y / factor) | 0
18
+ const srcRowOffset = srcY * srcW * 4
19
+ const dstRowOffset = y * dstW * 4
20
+
21
+ for (let x = 0; x < dstW; x++) {
22
+ const srcX = (x / factor) | 0
23
+ const srcIdx = srcRowOffset + srcX * 4
24
+ const dstIdx = dstRowOffset + x * 4
25
+
26
+ // Copy RGBA channels
27
+ dstData[dstIdx] = srcData[srcIdx]!
28
+ dstData[dstIdx + 1] = srcData[srcIdx + 1]!
29
+ dstData[dstIdx + 2] = srcData[srcIdx + 2]!
30
+ dstData[dstIdx + 3] = srcData[srcIdx + 3]!
31
+ }
32
+ }
33
+
34
+ return new ImageData(dstData, dstW, dstH)
35
+ }
@@ -16,7 +16,7 @@ export type IndexedImage = {
16
16
  /**
17
17
  * A palette of packed 32-bit colors (ABGR).
18
18
  */
19
- palette: Int32Array;
19
+ palette: Uint32Array;
20
20
  /**
21
21
  * The specific index in the palette that represents a fully transparent pixel.
22
22
  */
@@ -63,7 +63,7 @@ export function makeIndexedImage(imageData: ImageData): IndexedImage {
63
63
  indexedData[i] = id
64
64
  }
65
65
 
66
- const palette = new Int32Array(colorMap.keys())
66
+ const palette = new Uint32Array(colorMap.keys())
67
67
  return {
68
68
  width,
69
69
  height,
@@ -0,0 +1,36 @@
1
+ import type { IndexedImage } from '../index'
2
+
3
+ /**
4
+ * Resamples an IndexedImage by a specific factor using nearest neighbor
5
+ * Factor > 1 upscales, Factor < 1 downscales.
6
+ */
7
+ export function resampleIndexedImage(
8
+ source: IndexedImage,
9
+ factor: number,
10
+ ): IndexedImage {
11
+ const srcW = source.width
12
+ const srcH = source.height
13
+ const dstW = srcW * factor
14
+ const dstH = srcH * factor
15
+ const srcData = source.data
16
+ const dstData = new Int32Array(dstW * dstH)
17
+
18
+ for (let y = 0; y < dstH; y++) {
19
+ const srcY = (y / factor) | 0
20
+ const rowOffset = srcY * srcW
21
+ const dstOffset = y * dstW
22
+
23
+ for (let x = 0; x < dstW; x++) {
24
+ const srcX = (x / factor) | 0
25
+ dstData[dstOffset + x] = srcData[rowOffset + srcX]!
26
+ }
27
+ }
28
+
29
+ return {
30
+ width: dstW,
31
+ height: dstH,
32
+ data: dstData,
33
+ palette: source.palette,
34
+ transparentPalletIndex: source.transparentPalletIndex,
35
+ }
36
+ }
@@ -0,0 +1,29 @@
1
+ import { PixelData } from '../index'
2
+
3
+ /**
4
+ * Resamples an PixelData by a specific factor using nearest neighbor
5
+ * Factor > 1 upscales, Factor < 1 downscales.
6
+ */
7
+ export function resamplePixelData(pixelData: PixelData, factor: number): PixelData {
8
+ const dstW = Math.max(1, (pixelData.width * factor) | 0)
9
+ const dstH = Math.max(1, (pixelData.height * factor) | 0)
10
+ const dstBuffer = new Uint8ClampedArray(dstW * dstH * 4)
11
+ const dstData32 = new Uint32Array(dstBuffer.buffer)
12
+
13
+ for (let y = 0; y < dstH; y++) {
14
+ const srcY = (y / factor) | 0
15
+ const srcRowOffset = srcY * pixelData.width
16
+ const dstRowOffset = y * dstW
17
+
18
+ for (let x = 0; x < dstW; x++) {
19
+ const srcX = (x / factor) | 0
20
+ dstData32[dstRowOffset + x] = pixelData.data32[srcRowOffset + srcX]!
21
+ }
22
+ }
23
+
24
+ return new PixelData({
25
+ data: dstBuffer,
26
+ width: dstW,
27
+ height: dstH,
28
+ })
29
+ }
package/src/index.ts CHANGED
@@ -29,12 +29,15 @@ export * from './ImageData/imageDataToDataUrl'
29
29
  export * from './ImageData/imageDataToImgBlob'
30
30
  export * from './ImageData/imgBlobToImageData'
31
31
  export * from './ImageData/invertImageData'
32
+ export * from './ImageData/resampleImageData'
32
33
  export * from './ImageData/resizeImageData'
33
34
  export * from './ImageData/serialization'
34
35
  export * from './ImageData/writeImageDataPixels'
35
36
 
36
37
  export * from './IndexedImage/IndexedImage'
38
+ export * from './IndexedImage/getIndexedImageColorCounts'
37
39
  export * from './IndexedImage/indexedImageToAverageColor'
40
+ export * from './IndexedImage/resampleIndexedImage'
38
41
 
39
42
  export * from './Input/fileInputChangeToImageData'
40
43
  export * from './Input/fileToImageData'
@@ -54,6 +57,7 @@ export * from './PixelData/fillPixelData'
54
57
  export * from './PixelData/invertPixelData'
55
58
  export * from './PixelData/pixelDataToAlphaMask'
56
59
  export * from './PixelData/reflectPixelData'
60
+ export * from './PixelData/resamplePixelData'
57
61
  export * from './PixelData/rotatePixelData'
58
62
 
59
63
  export * from './Rect/trimRectBounds'