summaryrefslogtreecommitdiffstats
path: root/resources/images/resampling.go
blob: 0cb2676841c71ce7a422c7250476eaa438a838f3 (plain)
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
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package images

import "math"

// We moved from imaging to the gift package for image processing at some point.
// That package had more, but also less resampling filters. So we add the missing
// ones here. They are fairly exotic, but someone may use them, so keep them here
// for now.
//
// The filters below are ported from https://github.com/disintegration/imaging/blob/9aab30e6aa535fe3337b489b76759ef97dfaf362/resize.go#L369
// MIT License.

var (
	// Hermite cubic spline filter (BC-spline; B=0; C=0).
	hermiteResampling = resamp{
		name:    "Hermite",
		support: 1.0,
		kernel: func(x float32) float32 {
			x = absf32(x)
			if x < 1.0 {
				return bcspline(x, 0.0, 0.0)
			}
			return 0
		},
	}

	// Mitchell-Netravali cubic filter (BC-spline; B=1/3; C=1/3).
	mitchellNetravaliResampling = resamp{
		name:    "MitchellNetravali",
		support: 2.0,
		kernel: func(x float32) float32 {
			x = absf32(x)
			if x < 2.0 {
				return bcspline(x, 1.0/3.0, 1.0/3.0)
			}
			return 0
		},
	}

	// Catmull-Rom - sharp cubic filter (BC-spline; B=0; C=0.5).
	catmullRomResampling = resamp{
		name:    "CatmullRomResampling",
		support: 2.0,
		kernel: func(x float32) float32 {
			x = absf32(x)
			if x < 2.0 {
				return bcspline(x, 0.0, 0.5)
			}
			return 0
		},
	}

	// BSpline is a smooth cubic filter (BC-spline; B=1; C=0).
	bSplineResampling = resamp{
		name:    "BSplineResampling",
		support: 2.0,
		kernel: func(x float32) float32 {
			x = absf32(x)
			if x < 2.0 {
				return bcspline(x, 1.0, 0.0)
			}
			return 0
		},
	}

	// Gaussian blurring filter.
	gaussianResampling = resamp{
		name:    "GaussianResampling",
		support: 2.0,
		kernel: func(x float32) float32 {
			x = absf32(x)
			if x < 2.0 {
				return float32(math.Exp(float64(-2 * x * x)))
			}
			return 0
		},
	}

	//  Hann-windowed sinc filter (3 lobes).
	hannResampling = resamp{
		name:    "HannResampling",
		support: 3.0,
		kernel: func(x float32) float32 {
			x = absf32(x)
			if x < 3.0 {
				return sinc(x) * float32(0.5+0.5*math.Cos(math.Pi*float64(x)/3.0))
			}
			return 0
		},
	}

	hammingResampling = resamp{
		name:    "HammingResampling",
		support: 3.0,
		kernel: func(x float32) float32 {
			x = absf32(x)
			if x < 3.0 {
				return sinc(x) * float32(0.54+0.46*math.Cos(math.Pi*float64(x)/3.0))
			}
			return 0
		},
	}

	// Blackman-windowed sinc filter (3 lobes).
	blackmanResampling = resamp{
		name:    "BlackmanResampling",
		support: 3.0,
		kernel: func(x float32) float32 {
			x = absf32(x)
			if x < 3.0 {
				return sinc(x) * float32(0.42-0.5*math.Cos(math.Pi*float64(x)/3.0+math.Pi)+0.08*math.Cos(2.0*math.Pi*float64(x)/3.0))
			}
			return 0
		},
	}

	bartlettResampling = resamp{
		name:    "BartlettResampling",
		support: 3.0,
		kernel: func(x float32) float32 {
			x = absf32(x)
			if x < 3.0 {
				return sinc(x) * (3.0 - x) / 3.0
			}
			return 0
		},
	}

	// Welch-windowed sinc filter (parabolic window, 3 lobes).
	welchResampling = resamp{
		name:    "WelchResampling",
		support: 3.0,
		kernel: func(x float32) float32 {
			x = absf32(x)
			if x < 3.0 {
				return sinc(x) * (1.0 - (x * x / 9.0))
			}
			return 0
		},
	}

	// Cosine-windowed sinc filter (3 lobes).
	cosineResampling = resamp{
		name:    "CosineResampling",
		support: 3.0,
		kernel: func(x float32) float32 {
			x = absf32(x)
			if x < 3.0 {
				return sinc(x) * float32(math.Cos((math.Pi/2.0)*(float64(x)/3.0)))
			}
			return 0
		},
	}
)

// The following code is borrowed from https://raw.githubusercontent.com/disintegration/gift/master/resize.go
// MIT licensed.
type resamp struct {
	name    string
	support float32
	kernel  func(float32) float32
}

func (r resamp) String() string {
	return r.name
}

func (r resamp) Support() float32 {
	return r.support
}

func (r resamp) Kernel(x float32) float32 {
	return r.kernel(x)
}

func bcspline(x, b, c float32) float32 {
	if x < 0 {
		x = -x
	}
	if x < 1 {
		return ((12-9*b-6*c)*x*x*x + (-18+12*b+6*c)*x*x + (6 - 2*b)) / 6
	}
	if x < 2 {
		return ((-b-6*c)*x*x*x + (6*b+30*c)*x*x + (-12*b-48*c)*x + (8*b + 24*c)) / 6
	}
	return 0
}

func absf32(x float32) float32 {
	if x < 0 {
		return -x
	}
	return x
}

func sinc(x float32) float32 {
	if x == 0 {
		return 1
	}
	return float32(math.Sin(math.Pi*float64(x)) / (math.Pi * float64(x)))
}