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
/
resize.go
462 lines (390 loc) · 10.8 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package gift
import (
"image"
"image/draw"
"math"
)
// Resampling is an interpolation algorithm used for image resizing.
type Resampling interface {
Support() float32
Kernel(float32) float32
}
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 sinc(x float32) float32 {
if x == 0 {
return 1
}
return float32(math.Sin(math.Pi*float64(x)) / (math.Pi * float64(x)))
}
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)
}
// NearestNeighborResampling is a nearest neighbor resampling filter.
var NearestNeighborResampling Resampling
// BoxResampling is a box resampling filter (average of surrounding pixels).
var BoxResampling Resampling
// LinearResampling is a bilinear resampling filter.
var LinearResampling Resampling
// CubicResampling is a bicubic resampling filter (Catmull-Rom).
var CubicResampling Resampling
// LanczosResampling is a Lanczos resampling filter (3 lobes).
var LanczosResampling Resampling
type resampWeight struct {
index int
weight float32
}
func prepareResampWeights(dstSize, srcSize int, resampling Resampling) [][]resampWeight {
delta := float32(srcSize) / float32(dstSize)
scale := delta
if scale < 1 {
scale = 1
}
radius := float32(math.Ceil(float64(scale * resampling.Support())))
result := make([][]resampWeight, dstSize)
tmp := make([]resampWeight, 0, dstSize*int(radius+2)*2)
for i := 0; i < dstSize; i++ {
center := (float32(i)+0.5)*delta - 0.5
left := int(math.Ceil(float64(center - radius)))
if left < 0 {
left = 0
}
right := int(math.Floor(float64(center + radius)))
if right > srcSize-1 {
right = srcSize - 1
}
var sum float32
for j := left; j <= right; j++ {
weight := resampling.Kernel((float32(j) - center) / scale)
if weight == 0 {
continue
}
tmp = append(tmp, resampWeight{
index: j,
weight: weight,
})
sum += weight
}
for j := range tmp {
tmp[j].weight /= sum
}
result[i] = tmp
tmp = tmp[len(tmp):]
}
return result
}
func resizeLine(dst []pixel, src []pixel, weights [][]resampWeight) {
for i := 0; i < len(dst); i++ {
var r, g, b, a float32
for _, w := range weights[i] {
c := src[w.index]
wa := c.a * w.weight
r += c.r * wa
g += c.g * wa
b += c.b * wa
a += wa
}
if a != 0 {
r /= a
g /= a
b /= a
}
dst[i] = pixel{r, g, b, a}
}
}
func resizeHorizontal(dst draw.Image, src image.Image, w int, resampling Resampling, options *Options) {
srcb := src.Bounds()
dstb := dst.Bounds()
weights := prepareResampWeights(w, srcb.Dx(), resampling)
pixGetter := newPixelGetter(src)
pixSetter := newPixelSetter(dst)
parallelize(options.Parallelization, srcb.Min.Y, srcb.Max.Y, func(start, stop int) {
srcBuf := make([]pixel, srcb.Dx())
dstBuf := make([]pixel, w)
for srcy := start; srcy < stop; srcy++ {
pixGetter.getPixelRow(srcy, &srcBuf)
resizeLine(dstBuf, srcBuf, weights)
pixSetter.setPixelRow(dstb.Min.Y+srcy-srcb.Min.Y, dstBuf)
}
})
}
func resizeVertical(dst draw.Image, src image.Image, h int, resampling Resampling, options *Options) {
srcb := src.Bounds()
dstb := dst.Bounds()
weights := prepareResampWeights(h, srcb.Dy(), resampling)
pixGetter := newPixelGetter(src)
pixSetter := newPixelSetter(dst)
parallelize(options.Parallelization, srcb.Min.X, srcb.Max.X, func(start, stop int) {
srcBuf := make([]pixel, srcb.Dy())
dstBuf := make([]pixel, h)
for srcx := start; srcx < stop; srcx++ {
pixGetter.getPixelColumn(srcx, &srcBuf)
resizeLine(dstBuf, srcBuf, weights)
pixSetter.setPixelColumn(dstb.Min.X+srcx-srcb.Min.X, dstBuf)
}
})
}
func resizeNearest(dst draw.Image, src image.Image, w, h int, options *Options) {
srcb := src.Bounds()
dstb := dst.Bounds()
dx := float64(srcb.Dx()) / float64(w)
dy := float64(srcb.Dy()) / float64(h)
pixGetter := newPixelGetter(src)
pixSetter := newPixelSetter(dst)
parallelize(options.Parallelization, dstb.Min.Y, dstb.Min.Y+h, func(start, stop int) {
for dsty := start; dsty < stop; dsty++ {
for dstx := dstb.Min.X; dstx < dstb.Min.X+w; dstx++ {
fx := math.Floor((float64(dstx-dstb.Min.X) + 0.5) * dx)
fy := math.Floor((float64(dsty-dstb.Min.Y) + 0.5) * dy)
srcx := srcb.Min.X + int(fx)
srcy := srcb.Min.Y + int(fy)
px := pixGetter.getPixel(srcx, srcy)
pixSetter.setPixel(dstx, dsty, px)
}
}
})
}
type resizeFilter struct {
width int
height int
resampling Resampling
}
func (p *resizeFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) {
w, h := p.width, p.height
srcw, srch := srcBounds.Dx(), srcBounds.Dy()
if (w == 0 && h == 0) || w < 0 || h < 0 || srcw <= 0 || srch <= 0 {
dstBounds = image.Rect(0, 0, 0, 0)
} else if w == 0 {
fw := float64(h) * float64(srcw) / float64(srch)
dstw := int(math.Max(1, math.Floor(fw+0.5)))
dstBounds = image.Rect(0, 0, dstw, h)
} else if h == 0 {
fh := float64(w) * float64(srch) / float64(srcw)
dsth := int(math.Max(1, math.Floor(fh+0.5)))
dstBounds = image.Rect(0, 0, w, dsth)
} else {
dstBounds = image.Rect(0, 0, w, h)
}
return
}
func (p *resizeFilter) Draw(dst draw.Image, src image.Image, options *Options) {
if options == nil {
options = &defaultOptions
}
b := p.Bounds(src.Bounds())
w, h := b.Dx(), b.Dy()
if w <= 0 || h <= 0 {
return
}
if src.Bounds().Dx() == w && src.Bounds().Dy() == h {
copyimage(dst, src, options)
return
}
if p.resampling.Support() <= 0 {
resizeNearest(dst, src, w, h, options)
return
}
if src.Bounds().Dx() == w {
resizeVertical(dst, src, h, p.resampling, options)
return
}
if src.Bounds().Dy() == h {
resizeHorizontal(dst, src, w, p.resampling, options)
return
}
tmp := createTempImage(image.Rect(0, 0, w, src.Bounds().Dy()))
resizeHorizontal(tmp, src, w, p.resampling, options)
resizeVertical(dst, tmp, h, p.resampling, options)
}
// Resize creates a filter that resizes an image to the specified width and height using the specified resampling.
// If one of width or height is 0, the image aspect ratio is preserved.
// Supported resampling parameters: NearestNeighborResampling, BoxResampling, LinearResampling, CubicResampling, LanczosResampling.
//
// Example:
//
// // Resize the src image to width=300 preserving the aspect ratio.
// g := gift.New(
// gift.Resize(300, 0, gift.LanczosResampling),
// )
// dst := image.NewRGBA(g.Bounds(src.Bounds()))
// g.Draw(dst, src)
//
func Resize(width, height int, resampling Resampling) Filter {
return &resizeFilter{
width: width,
height: height,
resampling: resampling,
}
}
type resizeToFitFilter struct {
width int
height int
resampling Resampling
}
func (p *resizeToFitFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {
w, h := p.width, p.height
srcw, srch := srcBounds.Dx(), srcBounds.Dy()
if w <= 0 || h <= 0 || srcw <= 0 || srch <= 0 {
return image.Rect(0, 0, 0, 0)
}
if srcw <= w && srch <= h {
return image.Rect(0, 0, srcw, srch)
}
wratio := float64(srcw) / float64(w)
hratio := float64(srch) / float64(h)
var dstw, dsth int
if wratio > hratio {
dstw = w
dsth = minint(int(float64(srch)/wratio+0.5), h)
} else {
dsth = h
dstw = minint(int(float64(srcw)/hratio+0.5), w)
}
return image.Rect(0, 0, dstw, dsth)
}
func (p *resizeToFitFilter) Draw(dst draw.Image, src image.Image, options *Options) {
b := p.Bounds(src.Bounds())
Resize(b.Dx(), b.Dy(), p.resampling).Draw(dst, src, options)
}
// ResizeToFit creates a filter that resizes an image to fit within the specified dimensions while preserving the aspect ratio.
// Supported resampling parameters: NearestNeighborResampling, BoxResampling, LinearResampling, CubicResampling, LanczosResampling.
func ResizeToFit(width, height int, resampling Resampling) Filter {
return &resizeToFitFilter{
width: width,
height: height,
resampling: resampling,
}
}
type resizeToFillFilter struct {
width int
height int
anchor Anchor
resampling Resampling
}
func (p *resizeToFillFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {
w, h := p.width, p.height
srcw, srch := srcBounds.Dx(), srcBounds.Dy()
if w <= 0 || h <= 0 || srcw <= 0 || srch <= 0 {
return image.Rect(0, 0, 0, 0)
}
return image.Rect(0, 0, w, h)
}
func (p *resizeToFillFilter) Draw(dst draw.Image, src image.Image, options *Options) {
b := p.Bounds(src.Bounds())
w, h := b.Dx(), b.Dy()
if w <= 0 || h <= 0 {
return
}
srcw, srch := src.Bounds().Dx(), src.Bounds().Dy()
wratio := float64(srcw) / float64(w)
hratio := float64(srch) / float64(h)
var tmpw, tmph int
if wratio < hratio {
tmpw = w
tmph = maxint(int(float64(srch)/wratio+0.5), h)
} else {
tmph = h
tmpw = maxint(int(float64(srcw)/hratio+0.5), w)
}
tmp := createTempImage(image.Rect(0, 0, tmpw, tmph))
Resize(tmpw, tmph, p.resampling).Draw(tmp, src, options)
CropToSize(w, h, p.anchor).Draw(dst, tmp, options)
}
// ResizeToFill creates a filter that resizes an image to the smallest possible size that will cover the specified dimensions,
// then crops the resized image to the specified dimensions using the specified anchor point.
// Supported resampling parameters: NearestNeighborResampling, BoxResampling, LinearResampling, CubicResampling, LanczosResampling.
func ResizeToFill(width, height int, resampling Resampling, anchor Anchor) Filter {
return &resizeToFillFilter{
width: width,
height: height,
anchor: anchor,
resampling: resampling,
}
}
func init() {
// Nearest neighbor resampling filter.
NearestNeighborResampling = resamp{
name: "NearestNeighborResampling",
support: 0,
kernel: func(x float32) float32 {
return 0
},
}
// Box resampling filter.
BoxResampling = resamp{
name: "BoxResampling",
support: 0.5,
kernel: func(x float32) float32 {
if x < 0 {
x = -x
}
if x <= 0.5 {
return 1
}
return 0
},
}
// Linear resampling filter.
LinearResampling = resamp{
name: "LinearResampling",
support: 1,
kernel: func(x float32) float32 {
if x < 0 {
x = -x
}
if x < 1 {
return 1 - x
}
return 0
},
}
// Cubic resampling filter (Catmull-Rom).
CubicResampling = resamp{
name: "CubicResampling",
support: 2,
kernel: func(x float32) float32 {
if x < 0 {
x = -x
}
if x < 2 {
return bcspline(x, 0, 0.5)
}
return 0
},
}
// Lanczos resampling filter (3 lobes).
LanczosResampling = resamp{
name: "LanczosResampling",
support: 3,
kernel: func(x float32) float32 {
if x < 0 {
x = -x
}
if x < 3 {
return sinc(x) * sinc(x/3)
}
return 0
},
}
}