-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize.go
194 lines (173 loc) · 6.31 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
package main
import (
"image"
"github.com/nfnt/resize"
"github.com/qeesung/image2ascii/terminal"
)
// NewResizeHandler create a new resize handler
func NewResizeHandler() ResizeHandler {
handler := &ImageResizeHandler{
terminal: terminal.NewTerminalAccessor(),
}
initResizeResolver(handler)
return handler
}
// initResizeResolver register the size resolvers
func initResizeResolver(handler *ImageResizeHandler) {
sizeResolvers := make([]imageSizeResolver, 0, 5)
// fixed height or width resolver
sizeResolvers = append(sizeResolvers, imageSizeResolver{
match: func(options *Options) bool {
return options.FixedWidth != -1 || options.FixedHeight != -1
},
compute: func(sz image.Rectangle, options *Options, handler *ImageResizeHandler) (width, height int, err error) {
height = sz.Max.Y
width = sz.Max.X
if options.FixedWidth != -1 {
width = options.FixedWidth
}
if options.FixedHeight != -1 {
height = options.FixedHeight
}
return
},
})
// scaled by ratio
sizeResolvers = append(sizeResolvers, imageSizeResolver{
match: func(options *Options) bool {
return options.Ratio != 1
},
compute: func(sz image.Rectangle, options *Options, handler *ImageResizeHandler) (width, height int, err error) {
ratio := options.Ratio
width = handler.ScaleWidthByRatio(float64(sz.Max.X), ratio)
height = handler.ScaleHeightByRatio(float64(sz.Max.Y), ratio)
return
},
})
// scaled by stretched screen
sizeResolvers = append(sizeResolvers, imageSizeResolver{
match: func(options *Options) bool {
return options.StretchedScreen
},
compute: func(sz image.Rectangle, options *Options, handler *ImageResizeHandler) (width, height int, err error) {
return handler.ScreenSize()
},
})
// scaled by fit the screen
sizeResolvers = append(sizeResolvers, imageSizeResolver{
match: func(options *Options) bool {
return options.FitScreen
},
compute: func(sz image.Rectangle, options *Options, handler *ImageResizeHandler) (width, height int, err error) {
return handler.CalcProportionalFittingScreenSize(sz)
},
})
// default size
sizeResolvers = append(sizeResolvers, imageSizeResolver{
match: func(options *Options) bool {
return true
},
compute: func(sz image.Rectangle, options *Options, handler *ImageResizeHandler) (width, height int, err error) {
return sz.Max.X, sz.Max.Y, nil
},
})
handler.imageSizeResolvers = sizeResolvers
}
// ResizeHandler define the operation to resize a image
type ResizeHandler interface {
ScaleImage(image image.Image, options *Options) (newImage image.Image, err error)
}
// ImageResizeHandler implement the ResizeHandler interface and
// responsible for image resizing
type ImageResizeHandler struct {
terminal terminal.Terminal
imageSizeResolvers []imageSizeResolver
}
// imageSizeResolver to resolve the image size from the options
type imageSizeResolver struct {
match func(options *Options) bool
compute func(sz image.Rectangle, options *Options, handler *ImageResizeHandler) (width, height int, err error)
}
func (handler *ImageResizeHandler) ScreenSize() (width, height int, err error) {
width, height, err = handler.terminal.ScreenSize()
if err != nil {
// return 157, 40, nil // default full screen size for mac os mock on container
return 80, 24, nil // smaller terminal mocking
}
return width, height, nil
}
func (handler *ImageResizeHandler) CharWidth() (width float64) {
width = handler.terminal.CharWidth()
if width <= 0.01 {
return 0.5 // default char width for mac os mock on container
}
return width
}
// ScaleImage resize the convert to expected size base on the convert options
func (handler *ImageResizeHandler) ScaleImage(image image.Image, options *Options) (newImage image.Image, err error) {
sz := image.Bounds()
newWidth, newHeight, err := handler.resolveSize(sz, options)
if err != nil {
return nil, err
}
newImage = resize.Resize(uint(newWidth), uint(newHeight), image, resize.Lanczos3)
return newImage, nil
}
// resolveSize resolve the image size
func (handler *ImageResizeHandler) resolveSize(sz image.Rectangle, options *Options) (width, height int, err error) {
for _, resolver := range handler.imageSizeResolvers {
if resolver.match(options) {
return resolver.compute(sz, options, handler)
}
}
return sz.Max.X, sz.Max.Y, nil
}
// CalcProportionalFittingScreenSize proportional scale the image
// so that the terminal can just show the picture.
func (handler *ImageResizeHandler) CalcProportionalFittingScreenSize(sz image.Rectangle) (newWidth, newHeight int, err error) {
screenWidth, screenHeight, err := handler.ScreenSize()
if err != nil {
return 0, 0, err
}
newWidth, newHeight = handler.CalcFitSize(
float64(screenWidth),
float64(screenHeight),
float64(sz.Max.X),
float64(sz.Max.Y))
return
}
// CalcFitSizeRatio through the given length and width,
// the computation can match the optimal scaling ratio of the length and width.
// In other words, it is able to give a given size rectangle to contain pictures
// Either match the width first, then scale the length equally,
// or match the length first, then scale the height equally.
// More detail please check the example
func (handler *ImageResizeHandler) CalcFitSizeRatio(width, height, imageWidth, imageHeight float64) (ratio float64) {
ratio = 1.0
// try to fit the height
ratio = height / imageHeight
scaledWidth := imageWidth * ratio / handler.CharWidth()
if scaledWidth < width {
return ratio / handler.CharWidth()
}
// try to fit the width
ratio = width / imageWidth
return ratio
}
// CalcFitSize through the given length and width ,
// Calculation is able to match the length and width of
// the specified size, and is proportional scaling.
func (handler *ImageResizeHandler) CalcFitSize(width, height, toBeFitWidth, toBeFitHeight float64) (fitWidth, fitHeight int) {
ratio := handler.CalcFitSizeRatio(width, height, toBeFitWidth, toBeFitHeight)
fitWidth = handler.ScaleWidthByRatio(toBeFitWidth, ratio)
fitHeight = handler.ScaleHeightByRatio(toBeFitHeight, ratio)
return
}
// ScaleWidthByRatio scaled the width by ratio
func (handler *ImageResizeHandler) ScaleWidthByRatio(width float64, ratio float64) int {
return int(width * ratio)
}
// ScaleHeightByRatio scaled the height by ratio
func (handler *ImageResizeHandler) ScaleHeightByRatio(height float64, ratio float64) int {
return int(height * ratio * handler.CharWidth())
}