pixel-data-js 0.19.2 → 0.20.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.19.2",
4
+ "version": "0.20.0",
5
5
  "packageManager": "pnpm@10.30.0",
6
6
  "description": "JS Pixel and ImageData operations",
7
7
  "author": {
@@ -1,4 +1,4 @@
1
- import type { Color32, IPixelData, Rect } from '../_types'
1
+ import type { BinaryMask, BinaryMaskRect, Color32, IPixelData } from '../_types'
2
2
  import { makeClippedRect, resolveRectClipping } from '../Internal/resolveClipping'
3
3
  import type { PixelData } from './PixelData'
4
4
 
@@ -9,13 +9,12 @@ const SCRATCH_RECT = makeClippedRect()
9
9
  *
10
10
  * @param dst - The target {@link PixelData} to modify.
11
11
  * @param color - The {@link Color32} value to apply.
12
- * @param rect - A {@link Rect} defining the area to fill. If omitted, the entire
13
- * buffer is filled.
12
+ * @param rect - A {@link BinaryMaskRect} defining the area to fill.
14
13
  */
15
14
  export function fillPixelData(
16
15
  dst: IPixelData,
17
16
  color: Color32,
18
- rect?: Partial<Rect>,
17
+ rect?: Partial<BinaryMaskRect>,
19
18
  ): void
20
19
  /**
21
20
  * @param dst - The target {@link PixelData} to modify.
@@ -24,6 +23,7 @@ export function fillPixelData(
24
23
  * @param y - Starting vertical coordinate.
25
24
  * @param w - Width of the fill area.
26
25
  * @param h - Height of the fill area.
26
+ * @param mask - A {@link BinaryMaskRect} defining the area to fill
27
27
  */
28
28
  export function fillPixelData(
29
29
  dst: IPixelData,
@@ -32,30 +32,36 @@ export function fillPixelData(
32
32
  y: number,
33
33
  w: number,
34
34
  h: number,
35
+ mask?: BinaryMask,
35
36
  ): void
36
37
  export function fillPixelData(
37
38
  dst: IPixelData,
38
39
  color: Color32,
39
- _x?: Partial<Rect> | number,
40
+ _x?: Partial<BinaryMaskRect> | number,
40
41
  _y?: number,
41
42
  _w?: number,
42
43
  _h?: number,
44
+ _mask?: BinaryMask,
43
45
  ): void {
44
46
  let x: number
45
47
  let y: number
46
48
  let w: number
47
49
  let h: number
50
+ let mask: BinaryMask | undefined
48
51
 
49
52
  if (typeof _x === 'object') {
50
53
  x = _x.x ?? 0
51
54
  y = _x.y ?? 0
52
55
  w = _x.w ?? dst.width
53
56
  h = _x.h ?? dst.height
57
+ mask = _x.mask
58
+
54
59
  } else if (typeof _x === 'number') {
55
60
  x = _x
56
61
  y = _y!
57
62
  w = _w!
58
63
  h = _h!
64
+ mask = _mask
59
65
  } else {
60
66
  x = 0
61
67
  y = 0
@@ -84,10 +90,30 @@ export function fillPixelData(
84
90
  return
85
91
  }
86
92
 
87
- // Row-by-row fill for partial rectangles
88
- for (let iy = 0; iy < actualH; iy++) {
89
- const start = (finalY + iy) * dw + finalX
90
- const end = start + actualW
91
- dst32.fill(color, start, end)
93
+ if (mask) {
94
+ for (let iy = 0; iy < actualH; iy++) {
95
+ const currentY = finalY + iy
96
+ const maskY = currentY - y
97
+ const maskOffset = maskY * w
98
+
99
+ for (let ix = 0; ix < actualW; ix++) {
100
+ const currentX = finalX + ix
101
+ const maskX = currentX - x
102
+ const maskIndex = maskOffset + maskX
103
+ const isMasked = mask[maskIndex]
104
+
105
+ if (isMasked) {
106
+ const dstIndex = currentY * dw + currentX
107
+ dst32[dstIndex] = color
108
+ }
109
+ }
110
+ }
111
+ } else {
112
+ // Row-by-row fill for partial rectangles
113
+ for (let iy = 0; iy < actualH; iy++) {
114
+ const start = (finalY + iy) * dw + finalX
115
+ const end = start + actualW
116
+ dst32.fill(color, start, end)
117
+ }
92
118
  }
93
119
  }
package/src/_types.ts CHANGED
@@ -47,6 +47,14 @@ export type Rect = {
47
47
  h: number
48
48
  }
49
49
 
50
+ export type BinaryMaskRect = {
51
+ x: number
52
+ y: number
53
+ w: number
54
+ h: number
55
+ mask: BinaryMask
56
+ }
57
+
50
58
  /**
51
59
  * Defines how mask values should be interpreted during a draw operation.
52
60
  */