forked from rai-project/image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
129 lines (109 loc) · 2.29 KB
/
options.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
package image
import (
context "context"
"github.com/c3sr/image/types"
)
type Options struct {
ctx context.Context
resizeWidth int
resizeHeight int
resizeAlgorithm types.ResizeAlgorithm
maxDimension *int
minDimension *int
keepAspectRatio *bool
mode types.Mode
mean [3]float32
layout Layout
dctMethod string
}
type Option func(o *Options)
func Width(width int) Option {
return func(o *Options) {
o.resizeWidth = width
}
}
func Height(height int) Option {
return func(o *Options) {
o.resizeHeight = height
}
}
func Resized(height, width int) Option {
return func(o *Options) {
o.resizeHeight = height
o.resizeWidth = width
}
}
func ResizeShape(shape []int) Option {
if len(shape) != 2 {
panic("expecting a resize shape of length 2 of the form height,width")
}
return func(o *Options) {
o.resizeHeight = shape[0]
o.resizeWidth = shape[1]
}
}
func KeepAspectRatio(keepAspectRatio bool) Option {
return func(o *Options) {
o.keepAspectRatio = &keepAspectRatio
}
}
func MaxDimension(dim int) Option {
return func(o *Options) {
o.maxDimension = &dim
}
}
func MinDimension(dim int) Option {
return func(o *Options) {
o.minDimension = &dim
}
}
func ResizeAlgorithm(alg types.ResizeAlgorithm) Option {
return func(o *Options) {
o.resizeAlgorithm = alg
}
}
func Mean(mean [3]float32) Option {
return func(o *Options) {
o.mean = mean
}
}
func MeanValue(mean float32) Option {
return Mean([3]float32{mean, mean, mean})
}
func ChannelLayout(layout Layout) Option {
return func(o *Options) {
o.layout = layout
}
}
func Context(ctx context.Context) Option {
return func(o *Options) {
o.ctx = ctx
}
}
func Mode(mode types.Mode) Option {
return func(o *Options) {
o.mode = mode
}
}
func DCTMethod(method string) Option {
return func(o *Options) {
o.dctMethod = method
}
}
func NewOptions(opts ...Option) *Options {
options := &Options{
mean: [3]float32{0, 0, 0},
maxDimension: nil,
minDimension: nil,
keepAspectRatio: nil,
mode: types.RGBMode,
layout: HWCLayout,
dctMethod: "INTEGER_ACCURATE",
resizeAlgorithm: types.ResizeAlgorithmLinear,
ctx: context.Background(),
}
for _, o := range opts {
o(options)
}
return options
}