forked from disintegration/gift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
236 lines (217 loc) · 4.41 KB
/
utils.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
224
225
226
227
228
229
230
231
232
233
234
235
236
package gift
import (
"image"
"image/draw"
"math"
"runtime"
"sync"
"sync/atomic"
)
// parallelize data processing if 'enabled' is true
func parallelize(enabled bool, datamin, datamax int, fn func(pmin, pmax int)) {
datasize := datamax - datamin
partsize := datasize
numGoroutines := 1
if enabled {
numProcs := runtime.GOMAXPROCS(0)
if numProcs > 1 {
numGoroutines = numProcs
partsize = partsize / (numGoroutines * 10)
if partsize < 1 {
partsize = 1
}
}
}
if numGoroutines == 1 {
fn(datamin, datamax)
} else {
var wg sync.WaitGroup
wg.Add(numGoroutines)
idx := int64(datamin)
for p := 0; p < numGoroutines; p++ {
go func() {
defer wg.Done()
for {
pmin := int(atomic.AddInt64(&idx, int64(partsize))) - partsize
if pmin >= datamax {
break
}
pmax := pmin + partsize
if pmax > datamax {
pmax = datamax
}
fn(pmin, pmax)
}
}()
}
wg.Wait()
}
}
// float32 math
func absf32(x float32) float32 {
if x < 0 {
return -x
}
return x
}
func minf32(x, y float32) float32 {
if x < y {
return x
}
return y
}
func maxf32(x, y float32) float32 {
if x > y {
return x
}
return y
}
func powf32(x, y float32) float32 {
return float32(math.Pow(float64(x), float64(y)))
}
func logf32(x float32) float32 {
return float32(math.Log(float64(x)))
}
func expf32(x float32) float32 {
return float32(math.Exp(float64(x)))
}
func sincosf32(a float32) (float32, float32) {
sin, cos := math.Sincos(math.Pi * float64(a) / 180)
return float32(sin), float32(cos)
}
func floorf32(x float32) float32 {
return float32(math.Floor(float64(x)))
}
func sqrtf32(x float32) float32 {
return float32(math.Sqrt(float64(x)))
}
// int math
func minint(x, y int) int {
if x < y {
return x
}
return y
}
func maxint(x, y int) int {
if x > y {
return x
}
return y
}
// in-place quick sort for []float32
func qsortf32(data []float32) {
var qsortidx func([]float32, int, int)
qsortidx = func(data []float32, start, stop int) {
i := start
j := stop
x := data[start+(stop-start)/2]
for i <= j {
for data[i] < x {
i++
}
for data[j] > x {
j--
}
if i <= j {
data[i], data[j] = data[j], data[i]
i++
j--
}
}
if i < stop {
qsortidx(data, i, stop)
}
if j > start {
qsortidx(data, start, j)
}
}
if len(data) == 0 {
return
}
qsortidx(data, 0, len(data)-1)
}
// useful types for precomputing pixel weights
type uweight struct {
u int
weight float32
}
type uvweight struct {
u int
v int
weight float32
}
// create default temp image
func createTempImage(r image.Rectangle) draw.Image {
return image.NewNRGBA64(r) // use 16 bits per channel images internally
}
// check if image is opaque
func isOpaque(img image.Image) bool {
switch img := img.(type) {
case *image.NRGBA:
return img.Opaque()
case *image.NRGBA64:
return img.Opaque()
case *image.RGBA:
return img.Opaque()
case *image.RGBA64:
return img.Opaque()
case *image.Gray:
return true
case *image.Gray16:
return true
case *image.YCbCr:
return true
case *image.Paletted:
return img.Opaque()
}
return false
}
// generate disk-shaped kernel
func genDisk(ksize int) []float32 {
if ksize%2 == 0 {
ksize--
}
if ksize < 1 {
return []float32{}
}
disk := make([]float32, ksize*ksize)
kcenter := ksize / 2
for i := 0; i < ksize; i++ {
for j := 0; j < ksize; j++ {
x := kcenter - i
y := kcenter - j
r := math.Sqrt(float64(x*x + y*y))
if r <= float64(ksize/2) {
disk[j*ksize+i] = 1.0
}
}
}
return disk
}
// copy image from src to dst
func copyimage(dst draw.Image, src image.Image, options *Options) {
if options == nil {
options = &defaultOptions
}
srcb := src.Bounds()
dstb := dst.Bounds()
pixGetter := newPixelGetter(src)
pixSetter := newPixelSetter(dst)
parallelize(options.Parallelization, srcb.Min.Y, srcb.Max.Y, func(pmin, pmax int) {
for srcy := pmin; srcy < pmax; srcy++ {
for srcx := srcb.Min.X; srcx < srcb.Max.X; srcx++ {
dstx := dstb.Min.X + srcx - srcb.Min.X
dsty := dstb.Min.Y + srcy - srcb.Min.Y
pixSetter.setPixel(dstx, dsty, pixGetter.getPixel(srcx, srcy))
}
}
})
}
type copyimageFilter struct{}
func (p *copyimageFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) {
dstBounds = image.Rect(0, 0, srcBounds.Dx(), srcBounds.Dy())
return
}
func (p *copyimageFilter) Draw(dst draw.Image, src image.Image, options *Options) {
copyimage(dst, src, options)
}