/
resize.go
321 lines (294 loc) · 9.92 KB
/
resize.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package resize
import (
"errors"
"github.com/ernyoke/imger/utils"
"image"
"image/color"
"math"
)
// Interpolation method types
type Interpolation int
const (
// InterNearest - takes the nearest pixel.
InterNearest Interpolation = iota
// InterLinear - Linear interpolation between two pixels. More info: https://en.wikipedia.org/wiki/Linear_interpolation
InterLinear
// InterCatmullRom - Catmull-Rom resampling. More info: https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline
InterCatmullRom
// InterLanczos - Lanczos resampling. More info: https://en.wikipedia.org/wiki/Lanczos_resampling
InterLanczos
)
func resizeNearestGray(img *image.Gray, fx float64, fy float64) (*image.Gray, error) {
oldSize := img.Bounds().Size()
newSize := image.Point{X: int(float64(oldSize.X) * fx), Y: int(float64(oldSize.Y) * fy)}
newImg := image.NewGray(image.Rect(0, 0, newSize.X, newSize.Y))
utils.ParallelForEachPixel(newSize, func(x int, y int) {
oldXTemp := float64(x) / fx
var oldX int
if fraction := oldXTemp - float64(int(oldXTemp)); fraction >= 0.5 {
oldX = int(oldXTemp + 1)
} else {
oldX = int(oldXTemp)
}
oldYTemp := float64(y) / fy
var oldY int
if fraction := oldYTemp - float64(int(oldYTemp)); fraction >= 0.5 {
oldY = int(oldYTemp + 1)
} else {
oldY = int(oldYTemp)
}
newImg.SetGray(x, y, img.GrayAt(oldX, oldY))
})
return newImg, nil
}
func resizeLinearGray(img *image.Gray, fx float64, fy float64) (*image.Gray, error) {
res, err := resizeHorizontalGray(img, fx, NewLinear())
if err != nil {
return nil, err
}
res, err = resizeVerticalGray(res, fy, NewLinear())
if err != nil {
return nil, err
}
return res, nil
}
func resizeCatmullRomGray(img *image.Gray, fx float64, fy float64) (*image.Gray, error) {
res, err := resizeHorizontalGray(img, fx, NewCatmullRom())
if err != nil {
return nil, err
}
res, err = resizeVerticalGray(res, fy, NewCatmullRom())
if err != nil {
return nil, err
}
return res, nil
}
func resizeLanczosGray(img *image.Gray, fx float64, fy float64) (*image.Gray, error) {
res, err := resizeHorizontalGray(img, fx, NewLanczos())
if err != nil {
return nil, err
}
res, err = resizeVerticalGray(res, fy, NewLanczos())
if err != nil {
return nil, err
}
return res, nil
}
func resizeHorizontalGray(img *image.Gray, fx float64, filter Filter) (*image.Gray, error) {
originalSize := img.Bounds().Size()
newWidth := int(float64(originalSize.X) * fx)
res := image.NewGray(image.Rect(0, 0, newWidth, originalSize.Y))
dfx := 1 / fx
radius := math.Ceil(fx * filter.GetS())
for y := 0; y < originalSize.Y; y++ {
for x := 0; x < newWidth; x++ {
ix := (float64(x)+0.5)*dfx - 0.5
start := utils.ClampInt(int(ix-radius+0.5), 0, originalSize.X)
end := utils.ClampInt(int(ix+radius), 0, originalSize.X)
var fPix float64
var sum float64
for i := start; i < end; i++ {
filterValue := filter.Interpolate(float64(i)-ix) / fx
pix := img.GrayAt(i, y)
fPix += float64(pix.Y) * filterValue
sum += filterValue
}
res.SetGray(x, y, color.Gray{uint8(utils.ClampF64(fPix/sum+0.5, 0, 255))})
}
}
return res, nil
}
func resizeVerticalGray(img *image.Gray, fy float64, filter Filter) (*image.Gray, error) {
originalSize := img.Bounds().Size()
newHeight := int(float64(originalSize.Y) * fy)
res := image.NewGray(image.Rect(0, 0, originalSize.X, newHeight))
dfy := 1 / fy
radius := math.Ceil(fy * filter.GetS())
for y := 0; y < newHeight; y++ {
iy := (float64(y)+0.5)*dfy - 0.5
start := utils.ClampInt(int(iy-radius+0.5), 0, originalSize.Y)
end := utils.ClampInt(int(iy+radius), 0, originalSize.Y)
for x := 0; x < originalSize.X; x++ {
var sum float64
var fPix float64
for i := start; i < end; i++ {
filterValue := filter.Interpolate(float64(i)-iy) / fy
pix := img.GrayAt(x, i)
fPix += float64(pix.Y) * filterValue
sum += filterValue
}
res.SetGray(x, y, color.Gray{uint8(utils.ClampF64(fPix/sum+0.5, 0, 255))})
}
}
return res, nil
}
func resizeNearestRGBA(img *image.RGBA, fx float64, fy float64) (*image.RGBA, error) {
oldSize := img.Bounds().Size()
newSize := image.Point{X: int(float64(oldSize.X) * fx), Y: int(float64(oldSize.Y) * fy)}
newImg := image.NewRGBA(image.Rect(0, 0, newSize.X, newSize.Y))
utils.ParallelForEachPixel(newSize, func(x int, y int) {
oldXTemp := float64(x) / fx
var oldX int
if fraction := oldXTemp - float64(int(oldXTemp)); fraction >= 0.5 {
oldX = int(oldXTemp + 1)
} else {
oldX = int(oldXTemp)
}
oldYTemp := float64(y) / fy
var oldY int
if fraction := oldYTemp - float64(int(oldYTemp)); fraction >= 0.5 {
oldY = int(oldYTemp + 1)
} else {
oldY = int(oldYTemp)
}
newImg.SetRGBA(x, y, img.RGBAAt(oldX, oldY))
})
return newImg, nil
}
func resizeLinearRGBA(img *image.RGBA, fx float64, fy float64) (*image.RGBA, error) {
res, err := resizeHorizontalRGBA(img, fx, NewLinear())
if err != nil {
return nil, err
}
res, err = resizeVerticalRGBA(res, fy, NewLinear())
if err != nil {
return nil, err
}
return res, nil
}
func resizeCatmullRomRGBA(img *image.RGBA, fx float64, fy float64) (*image.RGBA, error) {
res, err := resizeHorizontalRGBA(img, fx, NewCatmullRom())
if err != nil {
return nil, err
}
res, err = resizeVerticalRGBA(res, fy, NewCatmullRom())
if err != nil {
return nil, err
}
return res, nil
}
func resizeLanczosRGBA(img *image.RGBA, fx float64, fy float64) (*image.RGBA, error) {
res, err := resizeHorizontalRGBA(img, fx, NewLanczos())
if err != nil {
return nil, err
}
res, err = resizeVerticalRGBA(res, fy, NewLanczos())
if err != nil {
return nil, err
}
return res, nil
}
func resizeHorizontalRGBA(img *image.RGBA, fx float64, filter Filter) (*image.RGBA, error) {
originalSize := img.Bounds().Size()
newWidth := int(float64(originalSize.X) * fx)
res := image.NewRGBA(image.Rect(0, 0, newWidth, originalSize.Y))
dfx := 1 / fx
radius := math.Ceil(fx * filter.GetS())
for y := 0; y < originalSize.Y; y++ {
for x := 0; x < newWidth; x++ {
ix := (float64(x)+0.5)*dfx - 0.5
start := utils.ClampInt(int(ix-radius+0.5), 0, originalSize.X)
end := utils.ClampInt(int(ix+radius), 0, originalSize.X)
var fPixR float64
var fPixG float64
var fPixB float64
var fPixA float64
var sum float64
for i := start; i < end; i++ {
filterValue := filter.Interpolate(float64(i)-ix) / fx
pix := img.RGBAAt(i, y)
fPixR += float64(pix.R) * filterValue
fPixG += float64(pix.G) * filterValue
fPixB += float64(pix.B) * filterValue
fPixA += float64(pix.A) * filterValue
sum += filterValue
}
res.SetRGBA(x, y, color.RGBA{R: uint8(utils.ClampF64(fPixR/sum+0.5, 0, 255)),
G: uint8(utils.ClampF64(fPixG/sum+0.5, 0, 255)),
B: uint8(utils.ClampF64(fPixB/sum+0.5, 0, 255)),
A: uint8(utils.ClampF64(fPixA/sum+0.5, 0, 255))})
}
}
return res, nil
}
func resizeVerticalRGBA(img *image.RGBA, fy float64, filter Filter) (*image.RGBA, error) {
originalSize := img.Bounds().Size()
newHeight := int(float64(originalSize.Y) * fy)
res := image.NewRGBA(image.Rect(0, 0, originalSize.X, newHeight))
dfy := 1 / fy
radius := math.Ceil(fy * filter.GetS())
for y := 0; y < newHeight; y++ {
iy := (float64(y)+0.5)*dfy - 0.5
start := utils.ClampInt(int(iy-radius+0.5), 0, originalSize.Y)
end := utils.ClampInt(int(iy+radius), 0, originalSize.Y)
for x := 0; x < originalSize.X; x++ {
var fPixR float64
var fPixG float64
var fPixB float64
var fPixA float64
var sum float64
for i := start; i < end; i++ {
filterValue := filter.Interpolate(float64(i)-iy) / fy
pix := img.RGBAAt(x, i)
fPixR += float64(pix.R) * filterValue
fPixG += float64(pix.G) * filterValue
fPixB += float64(pix.B) * filterValue
fPixA += float64(pix.A) * filterValue
sum += filterValue
}
res.SetRGBA(x, y, color.RGBA{R: uint8(utils.ClampF64(fPixR/sum+0.5, 0, 255)),
G: uint8(utils.ClampF64(fPixG/sum+0.5, 0, 255)),
B: uint8(utils.ClampF64(fPixB/sum+0.5, 0, 255)),
A: uint8(utils.ClampF64(fPixA/sum+0.5, 0, 255))})
}
}
return res, nil
}
// ResizeGray resizes an grayscale (Gray) image.
// Input parameters: rbga imaga which will be resized; fx, fy scaling factors, their value has to be a positive float,
// the new size of the image will be computed as originalWidth * fx and originalHeight * fy; interpolation method,
// currently the following methods are supported: InterNearest, InterLinear, InterCatmullRom, InterLanczos.
// Example of usage:
//
// res, err := resize.ResizeGray(img, 2.5, 3.5, resize.InterLinear)
//
func ResizeGray(img *image.Gray, fx float64, fy float64, interpolation Interpolation) (*image.Gray, error) {
if fx < 0 || fy < 0 {
return nil, errors.New("scale value should be greater then 0")
}
switch interpolation {
case InterNearest:
return resizeNearestGray(img, fx, fy)
case InterLinear:
return resizeLinearGray(img, fx, fy)
case InterCatmullRom:
return resizeCatmullRomGray(img, fx, fy)
case InterLanczos:
return resizeLanczosGray(img, fx, fy)
}
return nil, errors.New("invalid interpolation method")
}
// ResizeRGBA resizes an RGBA image.
// Input parameters: rbga imaga which will be resized; fx, fy scaling factors, their value has to be a positive float,
// the new size of the image will be computed as originalWidth * fx and originalHeight * fy; interpolation method,
// currently the following methods are supported: InterNearest, InterLinear, InterCatmullRom, InterLanczos.
// Example of usage:
//
// res, err := resize.ResizeRGBA(img, 2.5, 3.5, resize.InterLinear)
//
func ResizeRGBA(img *image.RGBA, fx float64, fy float64, interpolation Interpolation) (*image.RGBA, error) {
if fx < 0 || fy < 0 {
return nil, errors.New("scale value should be greater then 0")
}
switch interpolation {
case InterNearest:
return resizeNearestRGBA(img, fx, fy)
case InterLinear:
return resizeLinearRGBA(img, fx, fy)
case InterCatmullRom:
return resizeCatmullRomRGBA(img, fx, fy)
case InterLanczos:
return resizeLanczosRGBA(img, fx, fy)
}
return nil, errors.New("invalid interpolation method")
}