-
Notifications
You must be signed in to change notification settings - Fork 27
/
resize.go
69 lines (58 loc) · 1.61 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
package vipsutil
import (
"image"
"math"
)
// ImageDimensions is the dimensions of the source image.
type ImageDimensions struct {
// Width is the width of the source image.
Width int
// Height is the height of the source image.
Height int
}
// ResizeDimensions is the input parameters of the resize operation.
type ResizeDimensions struct {
// Width is the maximum width of the resulting image.
Width int
// Height is the maximum height of the resulting image.
Height int
}
// ResizeResult is the result of the resize operation.
type ResizeResult struct {
// Scale is the scale of the resulting image.
// If it is 1, then scaling is not performed.
Scale float64
// Crop is necessary sometimes if the aspect ratios do not match.
Crop *image.Rectangle
}
var NoopResizeResult = ResizeResult{
Scale: 1.0,
}
// ResizingMode is the abstraction of different resizing flavors.
type ResizingMode interface {
Resize(image ImageDimensions, resize ResizeDimensions) ResizeResult
}
type ResizingModeType string
const (
ResizingModeTypeScaleDown ResizingModeType = "scale-down"
ResizingModeTypeContain ResizingModeType = "contain"
ResizingModeTypeCover ResizingModeType = "cover"
)
func ResizingModeFromType(t ResizingModeType) ResizingMode {
switch t {
case ResizingModeTypeScaleDown:
return ResizingModeScaleDown{}
case ResizingModeTypeContain:
return ResizingModeContain{}
case ResizingModeTypeCover:
return ResizingModeCover{}
default:
return ResizingModeCover{}
}
}
func div(x int, y int) float64 {
return float64(x) / float64(y)
}
func mul(x int, f float64) int {
return int(math.Round(float64(x) * f))
}