pixel-data-js 0.30.0 → 0.32.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/dist/index.prod.cjs +473 -312
- package/dist/index.prod.cjs.map +1 -1
- package/dist/index.prod.d.ts +130 -55
- package/dist/index.prod.js +469 -308
- package/dist/index.prod.js.map +1 -1
- package/package.json +1 -1
- package/src/Control/BatchedQueue.ts +76 -0
- package/src/Control/RenderQueue.ts +49 -0
- package/src/History/PixelWriter.ts +5 -9
- package/src/ImageData/extractImageData.ts +55 -0
- package/src/ImageData/extractImageDataBuffer.ts +57 -28
- package/src/ImageData/writeImageData.ts +45 -66
- package/src/ImageData/writeImageDataBuffer.ts +79 -41
- package/src/PixelData/extractPixelDataBuffer.ts +53 -40
- package/src/PixelData/fillPixelData.ts +46 -28
- package/src/PixelData/fillPixelDataBinaryMask.ts +43 -38
- package/src/PixelData/fillPixelDataFast.ts +26 -16
- package/src/PixelData/invertPixelData.ts +38 -21
- package/src/PixelData/resizePixelData.ts +75 -0
- package/src/PixelData/writePixelDataBuffer.ts +53 -38
- package/src/index.ts +5 -1
- package/src/Rect/resolveClipping.ts +0 -140
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
import type { Rect } from '../Rect/_rect-types'
|
|
2
|
-
import { makeClippedBlit, resolveBlitClipping } from '../Rect/resolveClipping'
|
|
3
2
|
import type { PixelData32 } from './_pixelData-types'
|
|
4
3
|
|
|
5
|
-
const SCRATCH_BLIT = makeClippedBlit()
|
|
6
|
-
|
|
7
4
|
/**
|
|
8
5
|
* Copies a pixel buffer into a specific region of a {@link PixelData32} object.
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
6
|
+
* @param target - The target to write into.
|
|
7
|
+
* @param data - The source pixel data (Uint32Array).
|
|
8
|
+
* @param rect - A rect defining the destination region.
|
|
12
9
|
*/
|
|
13
10
|
export function writePixelDataBuffer(
|
|
14
11
|
target: PixelData32,
|
|
15
12
|
data: Uint32Array,
|
|
16
13
|
rect: Rect,
|
|
17
14
|
): void
|
|
15
|
+
/**
|
|
16
|
+
* @param target - The target to write into.
|
|
17
|
+
* @param data - The source pixel data (Uint32Array).
|
|
18
|
+
* @param x - The starting horizontal coordinate in the target.
|
|
19
|
+
* @param y - The starting vertical coordinate in the target.
|
|
20
|
+
* @param w - The width of the region to write.
|
|
21
|
+
* @param h - The height of the region to write.
|
|
22
|
+
*/
|
|
18
23
|
export function writePixelDataBuffer(
|
|
19
24
|
target: PixelData32,
|
|
20
25
|
data: Uint32Array,
|
|
@@ -31,49 +36,59 @@ export function writePixelDataBuffer(
|
|
|
31
36
|
_w?: number,
|
|
32
37
|
_h?: number,
|
|
33
38
|
): void {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
let x: number
|
|
40
|
+
let y: number
|
|
41
|
+
let w: number
|
|
42
|
+
let h: number
|
|
43
|
+
|
|
44
|
+
if (typeof _x === 'object') {
|
|
45
|
+
x = _x.x
|
|
46
|
+
y = _x.y
|
|
47
|
+
w = _x.w
|
|
48
|
+
h = _x.h
|
|
49
|
+
} else {
|
|
50
|
+
x = _x
|
|
51
|
+
y = _y!
|
|
52
|
+
w = _w!
|
|
53
|
+
h = _h!
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (w <= 0 || h <= 0) return
|
|
42
57
|
|
|
43
58
|
const dstW = target.w
|
|
44
59
|
const dstH = target.h
|
|
45
60
|
const dstData = target.data
|
|
46
61
|
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
// Inline clipping logic
|
|
63
|
+
let dstX = x
|
|
64
|
+
let dstY = y
|
|
65
|
+
let srcX = 0
|
|
66
|
+
let srcY = 0
|
|
67
|
+
let copyW = w
|
|
68
|
+
let copyH = h
|
|
69
|
+
|
|
70
|
+
if (dstX < 0) {
|
|
71
|
+
srcX = -dstX
|
|
72
|
+
copyW += dstX
|
|
73
|
+
dstX = 0
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (dstY < 0) {
|
|
77
|
+
srcY = -dstY
|
|
78
|
+
copyH += dstY
|
|
79
|
+
dstY = 0
|
|
80
|
+
}
|
|
61
81
|
|
|
62
|
-
|
|
82
|
+
copyW = Math.min(copyW, dstW - dstX)
|
|
83
|
+
copyH = Math.min(copyH, dstH - dstY)
|
|
63
84
|
|
|
64
|
-
|
|
65
|
-
x: dstX,
|
|
66
|
-
y: dstY,
|
|
67
|
-
sx: srcX,
|
|
68
|
-
sy: srcY,
|
|
69
|
-
w: copyW,
|
|
70
|
-
h: copyH,
|
|
71
|
-
} = clip
|
|
85
|
+
if (copyW <= 0 || copyH <= 0) return
|
|
72
86
|
|
|
73
87
|
for (let row = 0; row < copyH; row++) {
|
|
74
88
|
const dstStart = (dstY + row) * dstW + dstX
|
|
75
89
|
const srcStart = (srcY + row) * w + srcX
|
|
90
|
+
const chunk = data.subarray(srcStart, srcStart + copyW)
|
|
76
91
|
|
|
77
|
-
dstData.set(
|
|
92
|
+
dstData.set(chunk, dstStart)
|
|
78
93
|
}
|
|
79
94
|
}
|
package/src/index.ts
CHANGED
|
@@ -26,6 +26,9 @@ export * from './Clipboard/writeImgBlobToClipboard'
|
|
|
26
26
|
|
|
27
27
|
export * from './color'
|
|
28
28
|
|
|
29
|
+
export * from './Control/BatchedQueue'
|
|
30
|
+
export * from './Control/RenderQueue'
|
|
31
|
+
|
|
29
32
|
export * from './History/HistoryAction'
|
|
30
33
|
export * from './History/HistoryManager'
|
|
31
34
|
export * from './History/PixelAccumulator'
|
|
@@ -55,6 +58,7 @@ export * from './History/PixelWriter'
|
|
|
55
58
|
|
|
56
59
|
export * from './ImageData/_ImageData-types'
|
|
57
60
|
export * from './ImageData/copyImageData'
|
|
61
|
+
export * from './ImageData/extractImageData'
|
|
58
62
|
export * from './ImageData/extractImageDataBuffer'
|
|
59
63
|
export * from './ImageData/ImageDataLike'
|
|
60
64
|
export * from './ImageData/imageDataToAlphaMaskBuffer'
|
|
@@ -157,6 +161,7 @@ export * from './PixelData/PixelData'
|
|
|
157
161
|
export * from './PixelData/pixelDataToAlphaMask'
|
|
158
162
|
export * from './PixelData/reflectPixelData'
|
|
159
163
|
export * from './PixelData/resamplePixelData'
|
|
164
|
+
export * from './PixelData/resizePixelData'
|
|
160
165
|
export * from './PixelData/ReusablePixelData'
|
|
161
166
|
export * from './PixelData/rotatePixelData'
|
|
162
167
|
export * from './PixelData/uInt32ArrayToPixelData'
|
|
@@ -165,7 +170,6 @@ export * from './PixelData/writePixelDataBuffer'
|
|
|
165
170
|
|
|
166
171
|
export * from './Rect/_rect-types'
|
|
167
172
|
export * from './Rect/getRectsBounds'
|
|
168
|
-
export * from './Rect/resolveClipping'
|
|
169
173
|
export * from './Rect/trimMaskRectBounds'
|
|
170
174
|
export * from './Rect/trimRectBounds'
|
|
171
175
|
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
export type ClippedRect = {
|
|
2
|
-
x: number
|
|
3
|
-
y: number
|
|
4
|
-
w: number
|
|
5
|
-
h: number
|
|
6
|
-
inBounds: boolean
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export type ClippedBlit = {
|
|
10
|
-
x: number
|
|
11
|
-
y: number
|
|
12
|
-
sx: number
|
|
13
|
-
sy: number
|
|
14
|
-
w: number
|
|
15
|
-
h: number
|
|
16
|
-
inBounds: boolean
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// use factory functions when creating reusable objects ensure property order for JIT perf
|
|
20
|
-
export const makeClippedRect = (): ClippedRect => ({
|
|
21
|
-
x: 0,
|
|
22
|
-
y: 0,
|
|
23
|
-
w: 0,
|
|
24
|
-
h: 0,
|
|
25
|
-
inBounds: false,
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
export const makeClippedBlit = (): ClippedBlit => ({
|
|
29
|
-
x: 0,
|
|
30
|
-
y: 0,
|
|
31
|
-
sx: 0,
|
|
32
|
-
sy: 0,
|
|
33
|
-
w: 0,
|
|
34
|
-
h: 0,
|
|
35
|
-
inBounds: false,
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Calculates the intersection of a target rectangle and a bounding box (usually 0,0 -> width,height).
|
|
40
|
-
* Handles negative offsets by shrinking dimensions.
|
|
41
|
-
*/
|
|
42
|
-
export function resolveRectClipping(
|
|
43
|
-
x: number,
|
|
44
|
-
y: number,
|
|
45
|
-
w: number,
|
|
46
|
-
h: number,
|
|
47
|
-
boundaryW: number,
|
|
48
|
-
boundaryH: number,
|
|
49
|
-
out: ClippedRect,
|
|
50
|
-
): ClippedRect {
|
|
51
|
-
// Destination Clipping (Top/Left)
|
|
52
|
-
if (x < 0) {
|
|
53
|
-
w += x
|
|
54
|
-
x = 0
|
|
55
|
-
}
|
|
56
|
-
if (y < 0) {
|
|
57
|
-
h += y
|
|
58
|
-
y = 0
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Destination Clipping (Bottom/Right)
|
|
62
|
-
const actualW = Math.min(w, boundaryW - x)
|
|
63
|
-
const actualH = Math.min(h, boundaryH - y)
|
|
64
|
-
|
|
65
|
-
if (actualW <= 0 || actualH <= 0) {
|
|
66
|
-
out.inBounds = false
|
|
67
|
-
return out
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
out.x = x
|
|
71
|
-
out.y = y
|
|
72
|
-
out.w = actualW
|
|
73
|
-
out.h = actualH
|
|
74
|
-
out.inBounds = true
|
|
75
|
-
|
|
76
|
-
return out
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Calculates the clipping for transferring data from a Source to a Destination.
|
|
81
|
-
* Handles cases where the source is out of bounds (shifting the destination target)
|
|
82
|
-
* AND cases where the destination is out of bounds (shifting the source target).
|
|
83
|
-
*/
|
|
84
|
-
export function resolveBlitClipping(
|
|
85
|
-
x: number,
|
|
86
|
-
y: number,
|
|
87
|
-
sx: number,
|
|
88
|
-
sy: number,
|
|
89
|
-
w: number,
|
|
90
|
-
h: number,
|
|
91
|
-
dstW: number,
|
|
92
|
-
dstH: number,
|
|
93
|
-
srcW: number,
|
|
94
|
-
srcH: number,
|
|
95
|
-
out: ClippedBlit,
|
|
96
|
-
): ClippedBlit {
|
|
97
|
-
// 1. Source Clipping: If reading from negative source, shift target right and shrink
|
|
98
|
-
if (sx < 0) {
|
|
99
|
-
x -= sx
|
|
100
|
-
w += sx
|
|
101
|
-
sx = 0
|
|
102
|
-
}
|
|
103
|
-
if (sy < 0) {
|
|
104
|
-
y -= sy
|
|
105
|
-
h += sy
|
|
106
|
-
sy = 0
|
|
107
|
-
}
|
|
108
|
-
w = Math.min(w, srcW - sx)
|
|
109
|
-
h = Math.min(h, srcH - sy)
|
|
110
|
-
|
|
111
|
-
// 2. Destination Clipping: If writing to negative dest, shift source right and shrink
|
|
112
|
-
if (x < 0) {
|
|
113
|
-
sx -= x
|
|
114
|
-
w += x
|
|
115
|
-
x = 0
|
|
116
|
-
}
|
|
117
|
-
if (y < 0) {
|
|
118
|
-
sy -= y
|
|
119
|
-
h += y
|
|
120
|
-
y = 0
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const actualW = Math.min(w, dstW - x)
|
|
124
|
-
const actualH = Math.min(h, dstH - y)
|
|
125
|
-
|
|
126
|
-
if (actualW <= 0 || actualH <= 0) {
|
|
127
|
-
out.inBounds = false
|
|
128
|
-
return out
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
out.x = x
|
|
132
|
-
out.y = y
|
|
133
|
-
out.sx = sx
|
|
134
|
-
out.sy = sy
|
|
135
|
-
out.w = actualW
|
|
136
|
-
out.h = actualH
|
|
137
|
-
out.inBounds = true
|
|
138
|
-
|
|
139
|
-
return out
|
|
140
|
-
}
|