pixel-data-js 0.27.0 → 0.28.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/README.md +12 -2
- package/dist/index.prod.cjs +2222 -1045
- package/dist/index.prod.cjs.map +1 -1
- package/dist/index.prod.d.ts +542 -417
- package/dist/index.prod.js +2167 -1024
- package/dist/index.prod.js.map +1 -1
- package/package.json +11 -11
- package/src/Algorithm/floodFillSelection.ts +8 -6
- package/src/Algorithm/forEachLinePoint.ts +6 -6
- package/src/{Internal/resample32.ts → Algorithm/resampleUint32Array.ts} +11 -21
- package/src/BlendModes/blend-modes-fast.ts +169 -0
- package/src/BlendModes/blend-modes-perfect.ts +207 -0
- package/src/BlendModes/blend-modes.ts +9 -0
- package/src/Canvas/CanvasFrameRenderer.ts +20 -28
- package/src/Canvas/CanvasPixelDataRenderer.ts +23 -0
- package/src/Canvas/PixelCanvas.ts +2 -7
- package/src/Canvas/ReusableCanvas.ts +4 -12
- package/src/Canvas/_canvas-types.ts +26 -0
- package/src/History/PixelAccumulator.ts +17 -17
- package/src/History/PixelEngineConfig.ts +3 -3
- package/src/History/PixelMutator/mutatorApplyAlphaMask.ts +4 -3
- package/src/History/PixelMutator/mutatorApplyBinaryMask.ts +4 -3
- package/src/History/PixelMutator/mutatorApplyMask.ts +4 -3
- package/src/History/PixelMutator/mutatorBlendAlphaMask.ts +6 -4
- package/src/History/PixelMutator/mutatorBlendBinaryMask.ts +6 -4
- package/src/History/PixelMutator/mutatorBlendColor.ts +2 -2
- package/src/History/PixelMutator/mutatorBlendColorPaintAlphaMask.ts +2 -1
- package/src/History/PixelMutator/mutatorBlendColorPaintBinaryMask.ts +2 -1
- package/src/History/PixelMutator/mutatorBlendColorPaintMask.ts +3 -1
- package/src/History/PixelMutator/mutatorBlendColorPaintRect.ts +3 -3
- package/src/History/PixelMutator/mutatorBlendMask.ts +6 -4
- package/src/History/PixelMutator/mutatorBlendPixelData.ts +5 -4
- package/src/History/PixelMutator/mutatorClear.ts +4 -3
- package/src/History/PixelMutator/mutatorFill.ts +5 -4
- package/src/History/PixelMutator/mutatorFillBinaryMask.ts +2 -1
- package/src/History/PixelMutator/mutatorInvert.ts +2 -2
- package/src/History/PixelMutator.ts +1 -1
- package/src/History/PixelPatchTiles.ts +7 -7
- package/src/History/PixelWriter.ts +12 -63
- package/src/ImageData/ImageDataLike.ts +1 -1
- package/src/ImageData/_ImageData-types.ts +13 -0
- package/src/ImageData/copyImageData.ts +1 -1
- package/src/ImageData/extractImageDataBuffer.ts +3 -2
- package/src/ImageData/imageDataToUint32Array.ts +18 -0
- package/src/ImageData/resampleImageData.ts +3 -3
- package/src/ImageData/resizeImageData.ts +1 -1
- package/src/ImageData/serialization.ts +1 -1
- package/src/ImageData/uInt32ArrayToImageData.ts +1 -1
- package/src/ImageData/writeImageData.ts +2 -2
- package/src/ImageData/writeImageDataBuffer.ts +2 -2
- package/src/IndexedImage/IndexedImage.ts +56 -98
- package/src/IndexedImage/_indexedImage-types.ts +18 -0
- package/src/IndexedImage/getIndexedImageColorCounts.ts +3 -3
- package/src/IndexedImage/indexedImageToAverageColor.ts +1 -1
- package/src/IndexedImage/indexedImageToImageData.ts +4 -6
- package/src/IndexedImage/resampleIndexedImage.ts +7 -15
- package/src/Input/fileToImageData.ts +1 -1
- package/src/Internal/_errors.ts +2 -0
- package/src/Internal/macros.ts +14 -0
- package/src/Mask/AlphaMask.ts +1 -1
- package/src/Mask/BinaryMask/makeBinaryMaskFromAlphaMask.ts +23 -0
- package/src/Mask/BinaryMask/makeBinaryMaskOutline.ts +88 -0
- package/src/Mask/BinaryMask/makeCircleBinaryMaskOutline.ts +104 -0
- package/src/Mask/BinaryMask/makeRectBinaryMaskOutline.ts +34 -0
- package/src/Mask/BinaryMask.ts +1 -1
- package/src/Mask/_mask-types.ts +73 -0
- package/src/Mask/applyBinaryMaskToAlphaMask.ts +2 -1
- package/src/Mask/copyMask.ts +1 -1
- package/src/Mask/extractMask.ts +2 -1
- package/src/Mask/extractMaskBuffer.ts +1 -1
- package/src/Mask/mergeAlphaMasks.ts +6 -3
- package/src/Mask/mergeBinaryMasks.ts +2 -1
- package/src/Mask/setMaskData.ts +1 -1
- package/src/MaskRect/merge2BinaryMaskRects.ts +2 -2
- package/src/MaskRect/mergeBinaryMaskRects.ts +1 -1
- package/src/MaskRect/subtractBinaryMaskRects.ts +1 -1
- package/src/Paint/AlphaMaskPaintBuffer.ts +339 -0
- package/src/Paint/AlphaMaskPaintBufferCanvasRenderer.ts +78 -0
- package/src/Paint/BinaryMaskPaintBuffer.ts +254 -0
- package/src/Paint/BinaryMaskPaintBufferCanvasRenderer.ts +67 -0
- package/src/Paint/{PaintBuffer.ts → ColorPaintBuffer.ts} +148 -77
- package/src/Paint/{PaintBufferCanvasRenderer.ts → ColorPaintBufferCanvasRenderer.ts} +6 -5
- package/src/Paint/PaintCursorRenderer.ts +117 -0
- package/src/Paint/_paint-types.ts +22 -0
- package/src/Paint/eachTileInBounds.ts +45 -0
- package/src/Paint/makeCirclePaintMask.ts +74 -0
- package/src/Paint/makePaintMask.ts +5 -2
- package/src/Paint/makeRectFalloffPaintAlphaMask.ts +4 -2
- package/src/PixelData/PixelData.ts +15 -19
- package/src/PixelData/ReusablePixelData.ts +36 -0
- package/src/PixelData/_pixelData-types.ts +17 -0
- package/src/PixelData/applyAlphaMaskToPixelData.ts +80 -43
- package/src/PixelData/applyBinaryMaskToPixelData.ts +10 -8
- package/src/PixelData/applyMaskToPixelData.ts +4 -9
- package/src/PixelData/blendColorPixelData.ts +9 -8
- package/src/PixelData/blendColorPixelDataAlphaMask.ts +9 -7
- package/src/PixelData/blendColorPixelDataBinaryMask.ts +9 -7
- package/src/PixelData/blendColorPixelDataMask.ts +4 -2
- package/src/PixelData/blendColorPixelDataPaintAlphaMask.ts +4 -2
- package/src/PixelData/blendColorPixelDataPaintBinaryMask.ts +4 -2
- package/src/PixelData/blendColorPixelDataPaintMask.ts +5 -2
- package/src/PixelData/blendPixel.ts +6 -5
- package/src/PixelData/blendPixelData.ts +14 -13
- package/src/PixelData/blendPixelDataAlphaMask.ts +15 -13
- package/src/PixelData/blendPixelDataBinaryMask.ts +15 -13
- package/src/PixelData/blendPixelDataMask.ts +5 -3
- package/src/PixelData/blendPixelDataPaintBuffer.ts +5 -4
- package/src/PixelData/clearPixelDataFast.ts +4 -2
- package/src/PixelData/copyPixelData.ts +14 -0
- package/src/PixelData/extractPixelData.ts +8 -7
- package/src/PixelData/extractPixelDataBuffer.ts +9 -8
- package/src/PixelData/fillPixelData.ts +16 -14
- package/src/PixelData/fillPixelDataBinaryMask.ts +10 -8
- package/src/PixelData/fillPixelDataFast.ts +16 -14
- package/src/PixelData/invertPixelData.ts +9 -8
- package/src/PixelData/pixelDataToAlphaMask.ts +9 -8
- package/src/PixelData/reflectPixelData.ts +9 -9
- package/src/PixelData/resamplePixelData.ts +20 -9
- package/src/PixelData/rotatePixelData.ts +8 -7
- package/src/PixelData/uInt32ArrayToPixelData.ts +15 -0
- package/src/PixelData/writePaintBufferToPixelData.ts +5 -5
- package/src/PixelData/writePixelDataBuffer.ts +10 -9
- package/src/Rect/_rect-types.ts +7 -0
- package/src/Rect/getRectsBounds.ts +1 -1
- package/src/Rect/trimMaskRectBounds.ts +2 -1
- package/src/Rect/trimRectBounds.ts +1 -1
- package/src/Tile/MaskTile.ts +40 -0
- package/src/Tile/PixelTile.ts +23 -0
- package/src/{PixelTile/PixelTilePool.ts → Tile/TilePool.ts} +9 -9
- package/src/Tile/_tile-types.ts +33 -0
- package/src/_errors.ts +1 -0
- package/src/_types.ts +2 -118
- package/src/index.ts +46 -21
- package/src/ImageData/imageDataToUInt32Array.ts +0 -13
- package/src/Internal/helpers.ts +0 -5
- package/src/Paint/makeCirclePaintAlphaMask.ts +0 -41
- package/src/Paint/makeCirclePaintBinaryMask.ts +0 -29
- package/src/PixelTile/PixelTile.ts +0 -21
- /package/src/{Internal → Rect}/resolveClipping.ts +0 -0
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import type { Color32
|
|
2
|
-
import {
|
|
1
|
+
import type { Color32 } from '../_types'
|
|
2
|
+
import type { Rect } from '../Rect/_rect-types'
|
|
3
|
+
import { makeClippedRect, resolveRectClipping } from '../Rect/resolveClipping'
|
|
4
|
+
import type { PixelData32 } from './_pixelData-types'
|
|
3
5
|
|
|
4
6
|
const SCRATCH_RECT = makeClippedRect()
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
|
-
* Fills a region or the {@link
|
|
9
|
+
* Fills a region or the {@link PixelData32} buffer with a solid color.
|
|
8
10
|
* This function is faster than {@link fillPixelData} but does not
|
|
9
11
|
* return a boolean value indicating changes were made.
|
|
10
12
|
*
|
|
@@ -14,7 +16,7 @@ const SCRATCH_RECT = makeClippedRect()
|
|
|
14
16
|
* buffer is filled.
|
|
15
17
|
*/
|
|
16
18
|
export function fillPixelDataFast(
|
|
17
|
-
target:
|
|
19
|
+
target: PixelData32,
|
|
18
20
|
color: Color32,
|
|
19
21
|
rect?: Partial<Rect>,
|
|
20
22
|
): void
|
|
@@ -27,7 +29,7 @@ export function fillPixelDataFast(
|
|
|
27
29
|
* @param h - Height of the fill area.
|
|
28
30
|
*/
|
|
29
31
|
export function fillPixelDataFast(
|
|
30
|
-
dst:
|
|
32
|
+
dst: PixelData32,
|
|
31
33
|
color: Color32,
|
|
32
34
|
x: number,
|
|
33
35
|
y: number,
|
|
@@ -35,7 +37,7 @@ export function fillPixelDataFast(
|
|
|
35
37
|
h: number,
|
|
36
38
|
): void
|
|
37
39
|
export function fillPixelDataFast(
|
|
38
|
-
dst:
|
|
40
|
+
dst: PixelData32,
|
|
39
41
|
color: Color32,
|
|
40
42
|
_x?: Partial<Rect> | number,
|
|
41
43
|
_y?: number,
|
|
@@ -50,8 +52,8 @@ export function fillPixelDataFast(
|
|
|
50
52
|
if (typeof _x === 'object') {
|
|
51
53
|
x = _x.x ?? 0
|
|
52
54
|
y = _x.y ?? 0
|
|
53
|
-
w = _x.w ?? dst.
|
|
54
|
-
h = _x.h ?? dst.
|
|
55
|
+
w = _x.w ?? dst.w
|
|
56
|
+
h = _x.h ?? dst.h
|
|
55
57
|
} else if (typeof _x === 'number') {
|
|
56
58
|
x = _x
|
|
57
59
|
y = _y!
|
|
@@ -60,11 +62,11 @@ export function fillPixelDataFast(
|
|
|
60
62
|
} else {
|
|
61
63
|
x = 0
|
|
62
64
|
y = 0
|
|
63
|
-
w = dst.
|
|
64
|
-
h = dst.
|
|
65
|
+
w = dst.w
|
|
66
|
+
h = dst.h
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
const clip = resolveRectClipping(x, y, w, h, dst.
|
|
69
|
+
const clip = resolveRectClipping(x, y, w, h, dst.w, dst.h, SCRATCH_RECT)
|
|
68
70
|
|
|
69
71
|
if (!clip.inBounds) return
|
|
70
72
|
|
|
@@ -76,11 +78,11 @@ export function fillPixelDataFast(
|
|
|
76
78
|
h: actualH,
|
|
77
79
|
} = clip
|
|
78
80
|
|
|
79
|
-
const dst32 = dst.
|
|
80
|
-
const dw = dst.
|
|
81
|
+
const dst32 = dst.data
|
|
82
|
+
const dw = dst.w
|
|
81
83
|
|
|
82
84
|
// Optimization: If filling the entire buffer, use the native .fill()
|
|
83
|
-
if (actualW === dw && actualH === dst.
|
|
85
|
+
if (actualW === dw && actualH === dst.h && finalX === 0 && finalY === 0) {
|
|
84
86
|
dst32.fill(color)
|
|
85
87
|
return
|
|
86
88
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { makeClippedRect, resolveRectClipping } from '../
|
|
1
|
+
import { type PixelMutateOptions } from '../_types'
|
|
2
|
+
import { makeClippedRect, resolveRectClipping } from '../Rect/resolveClipping'
|
|
3
|
+
import type { PixelData32 } from './_pixelData-types'
|
|
3
4
|
|
|
4
5
|
const SCRATCH_RECT = makeClippedRect()
|
|
5
6
|
|
|
6
7
|
export function invertPixelData(
|
|
7
|
-
target:
|
|
8
|
+
target: PixelData32,
|
|
8
9
|
opts?: PixelMutateOptions,
|
|
9
10
|
): boolean {
|
|
10
11
|
const mask = opts?.mask
|
|
@@ -12,11 +13,11 @@ export function invertPixelData(
|
|
|
12
13
|
const targetY = opts?.y ?? 0
|
|
13
14
|
const mx = opts?.mx ?? 0
|
|
14
15
|
const my = opts?.my ?? 0
|
|
15
|
-
const width = opts?.w ?? target.
|
|
16
|
-
const height = opts?.h ?? target.
|
|
16
|
+
const width = opts?.w ?? target.w
|
|
17
|
+
const height = opts?.h ?? target.h
|
|
17
18
|
const invertMask = opts?.invertMask ?? false
|
|
18
19
|
|
|
19
|
-
const clip = resolveRectClipping(targetX, targetY, width, height, target.
|
|
20
|
+
const clip = resolveRectClipping(targetX, targetY, width, height, target.w, target.h, SCRATCH_RECT)
|
|
20
21
|
|
|
21
22
|
if (!clip.inBounds) return false
|
|
22
23
|
|
|
@@ -27,8 +28,8 @@ export function invertPixelData(
|
|
|
27
28
|
h: actualH,
|
|
28
29
|
} = clip
|
|
29
30
|
|
|
30
|
-
const dst32 = target.
|
|
31
|
-
const dw = target.
|
|
31
|
+
const dst32 = target.data
|
|
32
|
+
const dw = target.w
|
|
32
33
|
const mPitch = mask?.w ?? width
|
|
33
34
|
|
|
34
35
|
const dx = x - targetX
|
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
import type { AlphaMask
|
|
1
|
+
import type { AlphaMask } from '../Mask/_mask-types'
|
|
2
2
|
import { makeAlphaMask } from '../Mask/AlphaMask'
|
|
3
|
+
import type { PixelData32 } from './_pixelData-types'
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Extracts the alpha channel from PixelData into a single-channel mask.
|
|
6
7
|
* Returns a Uint8Array branded as AlphaMask.
|
|
7
8
|
*/
|
|
8
9
|
export function pixelDataToAlphaMask(
|
|
9
|
-
pixelData:
|
|
10
|
+
pixelData: PixelData32,
|
|
10
11
|
): AlphaMask {
|
|
11
12
|
const {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
data,
|
|
14
|
+
w,
|
|
15
|
+
h,
|
|
15
16
|
} = pixelData
|
|
16
|
-
const len =
|
|
17
|
-
const mask = makeAlphaMask(
|
|
17
|
+
const len = data.length
|
|
18
|
+
const mask = makeAlphaMask(w, h)
|
|
18
19
|
const maskData = mask.data
|
|
19
20
|
|
|
20
21
|
for (let i = 0; i < len; i++) {
|
|
21
|
-
const val =
|
|
22
|
+
const val = data[i]
|
|
22
23
|
|
|
23
24
|
// Extract the Alpha byte (top 8 bits in ABGR / Little-Endian)
|
|
24
25
|
// Shift right by 24 moves the 4th byte to the 1st position
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { PixelData32 } from './_pixelData-types'
|
|
2
2
|
|
|
3
|
-
export function reflectPixelDataHorizontal(pixelData:
|
|
4
|
-
const width = pixelData.
|
|
5
|
-
const height = pixelData.
|
|
6
|
-
const data = pixelData.
|
|
3
|
+
export function reflectPixelDataHorizontal(pixelData: PixelData32): void {
|
|
4
|
+
const width = pixelData.w
|
|
5
|
+
const height = pixelData.h
|
|
6
|
+
const data = pixelData.data
|
|
7
7
|
const halfWidth = Math.floor(width / 2)
|
|
8
8
|
|
|
9
9
|
for (let y = 0; y < height; y++) {
|
|
@@ -20,10 +20,10 @@ export function reflectPixelDataHorizontal(pixelData: IPixelData32): void {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
export function reflectPixelDataVertical(pixelData:
|
|
24
|
-
const width = pixelData.
|
|
25
|
-
const height = pixelData.
|
|
26
|
-
const data = pixelData.
|
|
23
|
+
export function reflectPixelDataVertical(pixelData: PixelData32): void {
|
|
24
|
+
const width = pixelData.w
|
|
25
|
+
const height = pixelData.h
|
|
26
|
+
const data = pixelData.data
|
|
27
27
|
const halfHeight = Math.floor(height / 2)
|
|
28
28
|
|
|
29
29
|
for (let y = 0; y < halfHeight; y++) {
|
|
@@ -1,19 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { resampleUint32Array } from '../Algorithm/resampleUint32Array'
|
|
2
|
+
import { type MutablePixelData32, type PixelData, type PixelData32, uInt32ArrayToImageData } from '../index'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Resamples PixelData by a specific factor using nearest neighbor.
|
|
6
6
|
* Factor > 1 upscales, Factor < 1 downscales.
|
|
7
7
|
*/
|
|
8
8
|
export function resamplePixelData(
|
|
9
|
-
pixelData:
|
|
9
|
+
pixelData: PixelData32,
|
|
10
10
|
factor: number,
|
|
11
11
|
): PixelData {
|
|
12
|
-
const { data, width, height } = resample32(pixelData.data32, pixelData.width, pixelData.height, factor)
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
))
|
|
13
|
+
const output = {} as MutablePixelData32
|
|
14
|
+
|
|
15
|
+
const resampled = resampleUint32Array(pixelData.data, pixelData.w, pixelData.h, factor, output) as PixelData
|
|
16
|
+
|
|
17
|
+
(resampled as any).imageData = uInt32ArrayToImageData(resampled.data, resampled.w, resampled.h)
|
|
18
|
+
|
|
19
|
+
return resampled
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function resamplePixelDataInPlace(
|
|
23
|
+
pixelData: PixelData32,
|
|
24
|
+
factor: number,
|
|
25
|
+
): void {
|
|
26
|
+
|
|
27
|
+
const resampled = resampleUint32Array(pixelData.data, pixelData.w, pixelData.h, factor, pixelData) as PixelData
|
|
28
|
+
|
|
29
|
+
(resampled as any).imageData = uInt32ArrayToImageData(resampled.data, resampled.w, resampled.h)
|
|
19
30
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { PixelData } from './
|
|
1
|
+
import type { PixelData } from './_pixelData-types'
|
|
2
|
+
import { setPixelData } from './PixelData'
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Rotates pixel data 90 degrees clockwise.
|
|
@@ -6,9 +7,9 @@ import { PixelData } from './PixelData'
|
|
|
6
7
|
* If rectangular, it replaces the internal ImageData with a new rotated version.
|
|
7
8
|
*/
|
|
8
9
|
export function rotatePixelData(pixelData: PixelData): void {
|
|
9
|
-
const width = pixelData.
|
|
10
|
-
const height = pixelData.
|
|
11
|
-
const data = pixelData.
|
|
10
|
+
const width = pixelData.w
|
|
11
|
+
const height = pixelData.h
|
|
12
|
+
const data = pixelData.data
|
|
12
13
|
|
|
13
14
|
if (width === height) {
|
|
14
15
|
rotateSquareInPlace(pixelData)
|
|
@@ -38,12 +39,12 @@ export function rotatePixelData(pixelData: PixelData): void {
|
|
|
38
39
|
newHeight,
|
|
39
40
|
)
|
|
40
41
|
|
|
41
|
-
pixelData
|
|
42
|
+
setPixelData(pixelData, newImageData)
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
function rotateSquareInPlace(pixelData: PixelData): void {
|
|
45
|
-
const n = pixelData.
|
|
46
|
-
const data = pixelData.
|
|
46
|
+
const n = pixelData.w
|
|
47
|
+
const data = pixelData.data
|
|
47
48
|
|
|
48
49
|
for (let i = 0; i < n / 2; i++) {
|
|
49
50
|
for (let j = i; j < n - i - 1; j++) {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { makePixelData, type PixelData } from '../index'
|
|
2
|
+
|
|
3
|
+
export function uInt32ArrayToPixelData(
|
|
4
|
+
data: Uint32Array,
|
|
5
|
+
width: number,
|
|
6
|
+
height: number,
|
|
7
|
+
): PixelData {
|
|
8
|
+
const buffer = data.buffer as ArrayBuffer
|
|
9
|
+
const byteOffset = data.byteOffset
|
|
10
|
+
const byteLength = data.byteLength
|
|
11
|
+
const clampedArray = new Uint8ClampedArray(buffer, byteOffset, byteLength)
|
|
12
|
+
const imageData = new ImageData(clampedArray, width, height)
|
|
13
|
+
|
|
14
|
+
return makePixelData(imageData)
|
|
15
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { ColorPaintBuffer } from '../Paint/ColorPaintBuffer'
|
|
2
|
+
import type { PixelData } from './_pixelData-types'
|
|
3
3
|
import { writePixelDataBuffer } from './writePixelDataBuffer'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @param writePixelDataBufferFn - @hidden
|
|
7
7
|
*/
|
|
8
8
|
export function writePaintBufferToPixelData(
|
|
9
|
-
target:
|
|
10
|
-
paintBuffer:
|
|
9
|
+
target: PixelData,
|
|
10
|
+
paintBuffer: ColorPaintBuffer,
|
|
11
11
|
writePixelDataBufferFn = writePixelDataBuffer,
|
|
12
12
|
) {
|
|
13
13
|
const tileShift = paintBuffer.config.tileShift
|
|
@@ -20,7 +20,7 @@ export function writePaintBufferToPixelData(
|
|
|
20
20
|
const dx = tile.tx << tileShift
|
|
21
21
|
const dy = tile.ty << tileShift
|
|
22
22
|
|
|
23
|
-
writePixelDataBufferFn(target, tile.
|
|
23
|
+
writePixelDataBufferFn(target, tile.data, dx, dy, tile.w, tile.h)
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { makeClippedBlit, resolveBlitClipping } from '../
|
|
1
|
+
import type { Rect } from '../Rect/_rect-types'
|
|
2
|
+
import { makeClippedBlit, resolveBlitClipping } from '../Rect/resolveClipping'
|
|
3
|
+
import type { PixelData32 } from './_pixelData-types'
|
|
3
4
|
|
|
4
5
|
const SCRATCH_BLIT = makeClippedBlit()
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
|
-
* Copies a pixel buffer into a specific region of a {@link
|
|
8
|
+
* Copies a pixel buffer into a specific region of a {@link PixelData32} object.
|
|
8
9
|
*
|
|
9
10
|
* This function performs a direct memory copy from a {@link Uint32Array}
|
|
10
11
|
* into the target buffer.
|
|
11
12
|
*/
|
|
12
13
|
export function writePixelDataBuffer(
|
|
13
|
-
target:
|
|
14
|
+
target: PixelData32,
|
|
14
15
|
data: Uint32Array,
|
|
15
16
|
rect: Rect,
|
|
16
17
|
): void
|
|
17
18
|
export function writePixelDataBuffer(
|
|
18
|
-
target:
|
|
19
|
+
target: PixelData32,
|
|
19
20
|
data: Uint32Array,
|
|
20
21
|
x: number,
|
|
21
22
|
y: number,
|
|
@@ -23,7 +24,7 @@ export function writePixelDataBuffer(
|
|
|
23
24
|
h: number,
|
|
24
25
|
): void
|
|
25
26
|
export function writePixelDataBuffer(
|
|
26
|
-
target:
|
|
27
|
+
target: PixelData32,
|
|
27
28
|
data: Uint32Array,
|
|
28
29
|
_x: Rect | number,
|
|
29
30
|
_y?: number,
|
|
@@ -39,9 +40,9 @@ export function writePixelDataBuffer(
|
|
|
39
40
|
h: _h!,
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
const dstW = target.
|
|
43
|
-
const dstH = target.
|
|
44
|
-
const dstData = target.
|
|
43
|
+
const dstW = target.w
|
|
44
|
+
const dstH = target.h
|
|
45
|
+
const dstData = target.data
|
|
45
46
|
|
|
46
47
|
// treat the source buffer as a Source Image starting at 0,0 with size w,h
|
|
47
48
|
const clip = resolveBlitClipping(
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { NullableMaskRect
|
|
1
|
+
import type { NullableMaskRect } from '../Mask/_mask-types'
|
|
2
2
|
import { extractMaskBuffer } from '../Mask/extractMaskBuffer'
|
|
3
|
+
import type { Rect } from './_rect-types'
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Intersects a target rectangle with a boundary, trimming dimensions and masks in-place.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { MaskType } from '../Mask/_mask-types'
|
|
2
|
+
import { type AlphaMaskTile, type BinaryMaskTile, type TileFactory, TileType } from './_tile-types'
|
|
3
|
+
|
|
4
|
+
export const makeAlphaMaskTile: TileFactory<AlphaMaskTile> = (
|
|
5
|
+
id: number,
|
|
6
|
+
tx: number,
|
|
7
|
+
ty: number,
|
|
8
|
+
tileSize: number,
|
|
9
|
+
tileArea: number,
|
|
10
|
+
) => {
|
|
11
|
+
return {
|
|
12
|
+
tileType: TileType.MASK,
|
|
13
|
+
type: MaskType.ALPHA,
|
|
14
|
+
data: new Uint8Array(tileArea),
|
|
15
|
+
w: tileSize,
|
|
16
|
+
h: tileSize,
|
|
17
|
+
id,
|
|
18
|
+
tx,
|
|
19
|
+
ty,
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const makeBinaryMaskTile: TileFactory<BinaryMaskTile> = (
|
|
24
|
+
id: number,
|
|
25
|
+
tx: number,
|
|
26
|
+
ty: number,
|
|
27
|
+
tileSize: number,
|
|
28
|
+
tileArea: number,
|
|
29
|
+
) => {
|
|
30
|
+
return {
|
|
31
|
+
tileType: TileType.MASK,
|
|
32
|
+
type: MaskType.BINARY,
|
|
33
|
+
data: new Uint8Array(tileArea),
|
|
34
|
+
w: tileSize,
|
|
35
|
+
h: tileSize,
|
|
36
|
+
id,
|
|
37
|
+
tx,
|
|
38
|
+
ty,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type PixelTile, TileType } from './_tile-types'
|
|
2
|
+
|
|
3
|
+
export function makePixelTile(
|
|
4
|
+
id: number,
|
|
5
|
+
tx: number,
|
|
6
|
+
ty: number,
|
|
7
|
+
tileSize: number,
|
|
8
|
+
tileArea: number,
|
|
9
|
+
): PixelTile {
|
|
10
|
+
const data32 = new Uint32Array(tileArea)
|
|
11
|
+
const data8 = new Uint8ClampedArray(data32.buffer) as Uint8ClampedArray<ArrayBuffer>
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
tileType: TileType.PIXEL,
|
|
15
|
+
id,
|
|
16
|
+
tx,
|
|
17
|
+
ty,
|
|
18
|
+
w: tileSize,
|
|
19
|
+
h: tileSize,
|
|
20
|
+
data: data32,
|
|
21
|
+
imageData: new ImageData(data8, tileSize, tileSize),
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import type { PixelEngineConfig } from '../History/PixelEngineConfig'
|
|
2
|
+
import type { Tile, TileFactory } from './_tile-types'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export class PixelTilePool {
|
|
6
|
-
public pool: PixelTile[]
|
|
4
|
+
export class TilePool<T extends Tile> {
|
|
5
|
+
public pool: T[]
|
|
7
6
|
|
|
8
7
|
private tileSize: number
|
|
9
8
|
private tileArea: number
|
|
10
9
|
|
|
11
10
|
constructor(
|
|
12
11
|
config: PixelEngineConfig,
|
|
12
|
+
private tileFactory: TileFactory<T>,
|
|
13
13
|
) {
|
|
14
14
|
this.pool = []
|
|
15
15
|
this.tileSize = config.tileSize
|
|
@@ -20,7 +20,7 @@ export class PixelTilePool {
|
|
|
20
20
|
id: number,
|
|
21
21
|
tx: number,
|
|
22
22
|
ty: number,
|
|
23
|
-
):
|
|
23
|
+
): T {
|
|
24
24
|
let tile = this.pool.pop()
|
|
25
25
|
|
|
26
26
|
if (tile) {
|
|
@@ -29,12 +29,12 @@ export class PixelTilePool {
|
|
|
29
29
|
tile.ty = ty
|
|
30
30
|
|
|
31
31
|
// Wipe dirty memory from previous uses before handing it out
|
|
32
|
-
tile.
|
|
32
|
+
tile.data.fill(0)
|
|
33
33
|
|
|
34
34
|
return tile
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
return
|
|
37
|
+
return this.tileFactory(
|
|
38
38
|
id,
|
|
39
39
|
tx,
|
|
40
40
|
ty,
|
|
@@ -43,11 +43,11 @@ export class PixelTilePool {
|
|
|
43
43
|
)
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
releaseTile(tile:
|
|
46
|
+
releaseTile(tile: T): void {
|
|
47
47
|
this.pool.push(tile)
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
releaseTiles(tiles: (
|
|
50
|
+
releaseTiles(tiles: (T | undefined)[]): void {
|
|
51
51
|
let length = tiles.length
|
|
52
52
|
|
|
53
53
|
for (let i = 0; i < length; i++) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { AlphaMask, BinaryMask } from '../Mask/_mask-types'
|
|
2
|
+
import type { PixelData } from '../PixelData/_pixelData-types'
|
|
3
|
+
|
|
4
|
+
export const enum TileType {
|
|
5
|
+
PIXEL,
|
|
6
|
+
MASK,
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface BaseTile {
|
|
10
|
+
tileType: TileType,
|
|
11
|
+
id: number
|
|
12
|
+
tx: number
|
|
13
|
+
ty: number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface PixelTile extends PixelData, BaseTile {
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface AlphaMaskTile extends AlphaMask, BaseTile {
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface BinaryMaskTile extends BinaryMask, BaseTile {
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type Tile = PixelTile | AlphaMaskTile | BinaryMaskTile
|
|
26
|
+
|
|
27
|
+
export type TileFactory<T extends Tile> = (
|
|
28
|
+
id: number,
|
|
29
|
+
tx: number,
|
|
30
|
+
ty: number,
|
|
31
|
+
tileSize: number,
|
|
32
|
+
tileArea: number,
|
|
33
|
+
) => T
|
package/src/_errors.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as ERRORS from './Internal/_errors'
|