-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmask.go
More file actions
212 lines (188 loc) · 5.35 KB
/
Copy pathmask.go
File metadata and controls
212 lines (188 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package gg
import "image"
// Mask represents an alpha mask for compositing operations.
// Values range from 0 (fully transparent) to 255 (fully opaque). Mask dimensions
// and coordinates are in pixel space; for HiDPI Contexts, use physical
// dimensions (PixelWidth/PixelHeight).
type Mask struct {
width int
height int
data []uint8
}
// NewMask creates a new empty mask with the given pixel dimensions.
// All values are initialized to 0 (fully transparent). For a HiDPI Context,
// use PixelWidth and PixelHeight rather than Width and Height.
func NewMask(width, height int) *Mask {
return &Mask{
width: width,
height: height,
data: make([]uint8, width*height),
}
}
// NewMaskFromAlpha creates a mask from an image's alpha channel.
func NewMaskFromAlpha(img image.Image) *Mask {
bounds := img.Bounds()
w, h := bounds.Dx(), bounds.Dy()
mask := NewMask(w, h)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
_, _, _, a := img.At(x+bounds.Min.X, y+bounds.Min.Y).RGBA()
// a is 0-65535, shift by 8 to get 0-255
// #nosec G115 -- safe: a>>8 is always in range [0, 255]
mask.data[y*w+x] = uint8(a >> 8)
}
}
return mask
}
// Bounds returns the mask dimensions as an image.Rectangle.
func (m *Mask) Bounds() image.Rectangle {
return image.Rect(0, 0, m.width, m.height)
}
// Width returns the mask width.
func (m *Mask) Width() int { return m.width }
// Height returns the mask height.
func (m *Mask) Height() int { return m.height }
// At returns the mask value at (x, y).
// Returns 0 for coordinates outside the mask bounds.
func (m *Mask) At(x, y int) uint8 {
if x < 0 || x >= m.width || y < 0 || y >= m.height {
return 0
}
return m.data[y*m.width+x]
}
// Set sets the mask value at (x, y).
// Coordinates outside the mask bounds are ignored.
func (m *Mask) Set(x, y int, value uint8) {
if x < 0 || x >= m.width || y < 0 || y >= m.height {
return
}
m.data[y*m.width+x] = value
}
// Fill fills the entire mask with a value.
func (m *Mask) Fill(value uint8) {
for i := range m.data {
m.data[i] = value
}
}
// Invert inverts all mask values (255 - value).
func (m *Mask) Invert() {
for i := range m.data {
m.data[i] = 255 - m.data[i]
}
}
// Clear clears the mask (sets all values to 0).
func (m *Mask) Clear() {
for i := range m.data {
m.data[i] = 0
}
}
// Clone creates a copy of the mask.
func (m *Mask) Clone() *Mask {
clone := NewMask(m.width, m.height)
copy(clone.data, m.data)
return clone
}
// Data returns the underlying mask data slice.
// This is useful for advanced operations.
func (m *Mask) Data() []uint8 {
return m.data
}
// applyMaskToPixmapData applies DestinationIn blending to a pixmap using a mask.
// For each pixel: all premultiplied channels are scaled by mask.At(x,y) / 255.
// Pixels outside the mask bounds are cleared to transparent.
func applyMaskToPixmapData(pm *Pixmap, mask *Mask) {
data := pm.Data()
pw, ph := pm.Width(), pm.Height()
mw, mh := mask.Width(), mask.Height()
maxX := pw
if mw < maxX {
maxX = mw
}
maxY := ph
if mh < maxY {
maxY = mh
}
for y := 0; y < maxY; y++ {
for x := 0; x < maxX; x++ {
mv := uint16(mask.At(x, y))
if mv == 255 {
continue // fully visible, no change
}
idx := (y*pw + x) * 4
if mv == 0 {
// Fully masked out: clear pixel.
data[idx+0] = 0
data[idx+1] = 0
data[idx+2] = 0
data[idx+3] = 0
continue
}
// DestinationIn: dst = dst * maskAlpha / 255
data[idx+0] = uint8(uint16(data[idx+0]) * mv / 255)
data[idx+1] = uint8(uint16(data[idx+1]) * mv / 255)
data[idx+2] = uint8(uint16(data[idx+2]) * mv / 255)
data[idx+3] = uint8(uint16(data[idx+3]) * mv / 255)
}
}
// Clear pixels outside mask bounds.
if mw < pw {
for y := 0; y < maxY; y++ {
for x := mw; x < pw; x++ {
idx := (y*pw + x) * 4
data[idx+0] = 0
data[idx+1] = 0
data[idx+2] = 0
data[idx+3] = 0
}
}
}
if mh < ph {
for y := mh; y < ph; y++ {
for x := 0; x < pw; x++ {
idx := (y*pw + x) * 4
data[idx+0] = 0
data[idx+1] = 0
data[idx+2] = 0
data[idx+3] = 0
}
}
}
}
// NewLuminanceMask creates a mask from an image using the CSS Masking Level 1
// luminance formula: Y = 0.2126*R + 0.7152*G + 0.0722*B. The luminance
// value is used directly as the mask alpha (brighter = more visible).
//
// This matches tiny-skia MaskType::Luminance and Vello Mask::new_luminance().
func NewLuminanceMask(img image.Image) *Mask {
bounds := img.Bounds()
w, h := bounds.Dx(), bounds.Dy()
mask := NewMask(w, h)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
r, g, b, _ := img.At(x+bounds.Min.X, y+bounds.Min.Y).RGBA()
// r, g, b are 0-65535. Compute luminance using CSS formula.
// Y = 0.2126*R + 0.7152*G + 0.0722*B
lum := 0.2126*float64(r) + 0.7152*float64(g) + 0.0722*float64(b)
// Scale from 0-65535 to 0-255.
// #nosec G115 -- safe: lum/257 is always in range [0, 255]
mask.data[y*w+x] = uint8(lum/257.0 + 0.5)
}
}
return mask
}
// NewMaskFromData creates a mask from a raw byte slice.
// The data must contain exactly width*height bytes, where each byte
// represents the mask alpha at that pixel (row-major order).
// Returns nil if the data length does not match width*height.
func NewMaskFromData(data []byte, width, height int) *Mask {
if len(data) != width*height {
return nil
}
m := &Mask{
width: width,
height: height,
data: make([]uint8, width*height),
}
copy(m.data, data)
return m
}