forked from nfnt/resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.go
238 lines (204 loc) · 5.47 KB
/
filters.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
237
238
/*
Copyright (c) 2012, Jan Schlicht <jan.schlicht@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
*/
package resize
import (
"image"
"image/color"
"math"
)
// restrict an input float32 to the range of uint16 values
func clampToUint16(x float32) (y uint16) {
y = uint16(x)
if x < 0 {
y = 0
} else if x > float32(0xfffe) {
// "else if x > float32(0xffff)" will cause overflows!
y = 0xffff
}
return
}
// describe a resampling filter
type filterModel struct {
// resampling is done by convolution with a (scaled) kernel
kernel func(float32) float32
// instead of blurring an image before downscaling to avoid aliasing,
// the filter is scaled by a factor which leads to a similar effect
factorInv float32
// for optimized access to image points
converter
// temporary used by Interpolate
tempRow []colorArray
kernelWeight []float32
weightSum float32
}
func (f *filterModel) SetKernelWeights(u float32) {
uf := int(u) - len(f.tempRow)/2 + 1
u -= float32(uf)
f.weightSum = 0
for j := range f.tempRow {
f.kernelWeight[j] = f.kernel((u - float32(j)) * f.factorInv)
f.weightSum += f.kernelWeight[j]
}
}
func (f *filterModel) convolution1d() (c colorArray) {
for j := range f.tempRow {
for i := range c {
c[i] += f.tempRow[j][i] * f.kernelWeight[j]
}
}
// normalize values
for i := range c {
c[i] = c[i] / f.weightSum
}
return
}
func (f *filterModel) Interpolate(u float32, y int) color.RGBA64 {
uf := int(u) - len(f.tempRow)/2 + 1
u -= float32(uf)
for i := range f.tempRow {
f.at(uf+i, y, &f.tempRow[i])
}
c := f.convolution1d()
return color.RGBA64{
clampToUint16(c[0]),
clampToUint16(c[1]),
clampToUint16(c[2]),
clampToUint16(c[3]),
}
}
// createFilter tries to find an optimized converter for the given input image
// and initializes all filterModel members to their defaults
func createFilter(img image.Image, factor float32, size int, kernel func(float32) float32) (f Filter) {
sizeX := size * (int(math.Ceil(float64(factor))))
switch img.(type) {
default:
f = &filterModel{
kernel, 1. / factor,
&genericConverter{img},
make([]colorArray, sizeX),
make([]float32, sizeX),
0,
}
case *image.RGBA:
f = &filterModel{
kernel, 1. / factor,
&rgbaConverter{img.(*image.RGBA)},
make([]colorArray, sizeX),
make([]float32, sizeX),
0,
}
case *image.RGBA64:
f = &filterModel{
kernel, 1. / factor,
&rgba64Converter{img.(*image.RGBA64)},
make([]colorArray, sizeX),
make([]float32, sizeX),
0,
}
case *image.Gray:
f = &filterModel{
kernel, 1. / factor,
&grayConverter{img.(*image.Gray)},
make([]colorArray, sizeX),
make([]float32, sizeX),
0,
}
case *image.Gray16:
f = &filterModel{
kernel, 1. / factor,
&gray16Converter{img.(*image.Gray16)},
make([]colorArray, sizeX),
make([]float32, sizeX),
0,
}
case *image.YCbCr:
f = &filterModel{
kernel, 1. / factor,
&ycbcrConverter{img.(*image.YCbCr)},
make([]colorArray, sizeX),
make([]float32, sizeX),
0,
}
}
return
}
// Nearest-neighbor interpolation
func NearestNeighbor(img image.Image, factor float32) Filter {
return createFilter(img, factor, 2, func(x float32) (y float32) {
if x >= -0.5 && x < 0.5 {
y = 1
} else {
y = 0
}
return
})
}
// Bilinear interpolation
func Bilinear(img image.Image, factor float32) Filter {
return createFilter(img, factor, 2, func(x float32) (y float32) {
absX := float32(math.Abs(float64(x)))
if absX <= 1 {
y = 1 - absX
} else {
y = 0
}
return
})
}
// Bicubic interpolation (with cubic hermite spline)
func Bicubic(img image.Image, factor float32) Filter {
return createFilter(img, factor, 4, splineKernel(0, 0.5))
}
// Mitchell-Netravali interpolation
func MitchellNetravali(img image.Image, factor float32) Filter {
return createFilter(img, factor, 4, splineKernel(1.0/3.0, 1.0/3.0))
}
func splineKernel(B, C float32) func(float32) float32 {
factorA := 2.0 - 1.5*B - C
factorB := -3.0 + 2.0*B + C
factorC := 1.0 - 1.0/3.0*B
factorD := -B/6.0 - C
factorE := B + 5.0*C
factorF := -2.0*B - 8.0*C
factorG := 4.0/3.0*B + 4.0*C
return func(x float32) (y float32) {
absX := float32(math.Abs(float64(x)))
if absX <= 1 {
y = absX*absX*(factorA*absX+factorB) + factorC
} else if absX <= 2 {
y = absX*(absX*(absX*factorD+factorE)+factorF) + factorG
} else {
y = 0
}
return
}
}
func lanczosKernel(a uint) func(float32) float32 {
return func(x float32) (y float32) {
if x > -float32(a) && x < float32(a) {
y = float32(Sinc(float64(x))) * float32(Sinc(float64(x/float32(a))))
} else {
y = 0
}
return
}
}
// Lanczos interpolation (a=2)
func Lanczos2(img image.Image, factor float32) Filter {
return createFilter(img, factor, 4, lanczosKernel(2))
}
// Lanczos interpolation (a=3)
func Lanczos3(img image.Image, factor float32) Filter {
return createFilter(img, factor, 6, lanczosKernel(3))
}