This repository has been archived by the owner on Nov 19, 2023. It is now read-only.
forked from disintegration/gift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rank.go
223 lines (195 loc) · 4.93 KB
/
rank.go
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
213
214
215
216
217
218
219
220
221
222
223
package gift
import (
"image"
"image/draw"
)
type rankMode int
const (
rankMedian rankMode = iota
rankMin
rankMax
)
type rankFilter struct {
ksize int
disk bool
mode rankMode
}
func (p *rankFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) {
dstBounds = image.Rect(0, 0, srcBounds.Dx(), srcBounds.Dy())
return
}
func (p *rankFilter) Draw(dst draw.Image, src image.Image, options *Options) {
if options == nil {
options = &defaultOptions
}
srcb := src.Bounds()
dstb := dst.Bounds()
if srcb.Dx() <= 0 || srcb.Dy() <= 0 {
return
}
ksize := p.ksize
if ksize%2 == 0 {
ksize--
}
if ksize <= 1 {
copyimage(dst, src, options)
return
}
kradius := ksize / 2
opaque := isOpaque(src)
var disk []float32
if p.disk {
disk = genDisk(ksize)
}
pixGetter := newPixelGetter(src)
pixSetter := newPixelSetter(dst)
parallelize(options.Parallelization, srcb.Min.Y, srcb.Max.Y, func(start, stop int) {
pxbuf := []pixel{}
var rbuf, gbuf, bbuf, abuf []float32
if p.mode == rankMedian {
rbuf = make([]float32, 0, ksize*ksize)
gbuf = make([]float32, 0, ksize*ksize)
bbuf = make([]float32, 0, ksize*ksize)
if !opaque {
abuf = make([]float32, 0, ksize*ksize)
}
}
for y := start; y < stop; y++ {
// Init buffer.
pxbuf = pxbuf[:0]
for i := srcb.Min.X - kradius; i <= srcb.Min.X+kradius; i++ {
for j := y - kradius; j <= y+kradius; j++ {
kx, ky := i, j
if kx < srcb.Min.X {
kx = srcb.Min.X
} else if kx > srcb.Max.X-1 {
kx = srcb.Max.X - 1
}
if ky < srcb.Min.Y {
ky = srcb.Min.Y
} else if ky > srcb.Max.Y-1 {
ky = srcb.Max.Y - 1
}
pxbuf = append(pxbuf, pixGetter.getPixel(kx, ky))
}
}
for x := srcb.Min.X; x < srcb.Max.X; x++ {
var r, g, b, a float32
if p.mode == rankMedian {
rbuf = rbuf[:0]
gbuf = gbuf[:0]
bbuf = bbuf[:0]
if !opaque {
abuf = abuf[:0]
}
} else if p.mode == rankMin {
r, g, b, a = 1, 1, 1, 1
} else if p.mode == rankMax {
r, g, b, a = 0, 0, 0, 0
}
sz := 0
for i := 0; i < ksize; i++ {
for j := 0; j < ksize; j++ {
if p.disk {
if disk[i*ksize+j] == 0 {
continue
}
}
px := pxbuf[i*ksize+j]
if p.mode == rankMedian {
rbuf = append(rbuf, px.r)
gbuf = append(gbuf, px.g)
bbuf = append(bbuf, px.b)
if !opaque {
abuf = append(abuf, px.a)
}
} else if p.mode == rankMin {
r = minf32(r, px.r)
g = minf32(g, px.g)
b = minf32(b, px.b)
if !opaque {
a = minf32(a, px.a)
}
} else if p.mode == rankMax {
r = maxf32(r, px.r)
g = maxf32(g, px.g)
b = maxf32(b, px.b)
if !opaque {
a = maxf32(a, px.a)
}
}
sz++
}
}
if p.mode == rankMedian {
sort(rbuf)
sort(gbuf)
sort(bbuf)
if !opaque {
sort(abuf)
}
idx := sz / 2
r, g, b = rbuf[idx], gbuf[idx], bbuf[idx]
if !opaque {
a = abuf[idx]
}
}
if opaque {
a = 1
}
pixSetter.setPixel(dstb.Min.X+x-srcb.Min.X, dstb.Min.Y+y-srcb.Min.Y, pixel{r, g, b, a})
// Rotate buffer columns.
if x < srcb.Max.X-1 {
copy(pxbuf[0:], pxbuf[ksize:])
pxbuf = pxbuf[0 : ksize*(ksize-1)]
kx := x + 1 + kradius
if kx > srcb.Max.X-1 {
kx = srcb.Max.X - 1
}
for j := y - kradius; j <= y+kradius; j++ {
ky := j
if ky < srcb.Min.Y {
ky = srcb.Min.Y
} else if ky > srcb.Max.Y-1 {
ky = srcb.Max.Y - 1
}
pxbuf = append(pxbuf, pixGetter.getPixel(kx, ky))
}
}
}
}
})
}
// Median creates a median image filter.
// Picks a median value per channel in neighborhood for each pixel.
// The ksize parameter is the kernel size. It must be an odd positive integer (for example: 3, 5, 7).
// If the disk parameter is true, a disk-shaped neighborhood will be used instead of a square neighborhood.
func Median(ksize int, disk bool) Filter {
return &rankFilter{
ksize: ksize,
disk: disk,
mode: rankMedian,
}
}
// Minimum creates a local minimum image filter.
// Picks a minimum value per channel in neighborhood for each pixel.
// The ksize parameter is the kernel size. It must be an odd positive integer (for example: 3, 5, 7).
// If the disk parameter is true, a disk-shaped neighborhood will be used instead of a square neighborhood.
func Minimum(ksize int, disk bool) Filter {
return &rankFilter{
ksize: ksize,
disk: disk,
mode: rankMin,
}
}
// Maximum creates a local maximum image filter.
// Picks a maximum value per channel in neighborhood for each pixel.
// The ksize parameter is the kernel size. It must be an odd positive integer (for example: 3, 5, 7).
// If the disk parameter is true, a disk-shaped neighborhood will be used instead of a square neighborhood.
func Maximum(ksize int, disk bool) Filter {
return &rankFilter{
ksize: ksize,
disk: disk,
mode: rankMax,
}
}