-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
151 lines (131 loc) · 4.44 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package imeji
import (
"github.com/BigJk/imeji/charmaps"
"github.com/anthonynsimon/bild/adjust"
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/transform"
"github.com/muesli/termenv"
"image"
"io"
)
type OptionData struct {
pattern []charmaps.Pattern
routines int
colorPairs int
fontRatio float64
img image.Image
out *termenv.Output
buf io.Writer
}
type Option func(data *OptionData)
func WithResize(width int, height int) Option {
return func(data *OptionData) {
data.img = transform.Resize(data.img, width*8, height*8, transform.NearestNeighbor)
}
}
// WithMaxWidth specifies a max width in cells for the output. This will keep the aspect ratio of the input picture and
// scale based on specified font ratio.
func WithMaxWidth(width int) Option {
return func(data *OptionData) {
rect := (data.img).Bounds()
if rect.Max.X > width {
aspectRatio := float64(rect.Max.Y) / float64(rect.Max.X)
ratio := float64(width) / float64(rect.Max.X) * data.fontRatio
data.img = transform.Resize(data.img, width*8, int(float64(rect.Max.Y)*ratio*aspectRatio)*8, transform.Linear)
}
}
}
// WithCrop crops a part of the image. This uses the normal image coordinates.
func WithCrop(x int, y int, width int, height int) Option {
return func(data *OptionData) {
data.img = transform.Crop(data.img, image.Rect(x, y, x+width, y+height))
}
}
// WithTrueColor enables true color support. This is important if you don't output to os.Stdout and no terminal
// detection can be done or if you want to force a color mode.
func WithTrueColor() Option {
return func(data *OptionData) {
data.out = termenv.NewOutput(data.buf, termenv.WithProfile(termenv.TrueColor))
}
}
// WithANSI enables basic ansi color support. This is important if you don't output to os.Stdout and no terminal
// detection can be done or if you want to force a color mode.
func WithANSI() Option {
return func(data *OptionData) {
data.out = termenv.NewOutput(data.buf, termenv.WithProfile(termenv.ANSI))
}
}
// WithANSI256 enables 256 color support. This is important if you don't output to os.Stdout and no terminal
// detection can be done or if you want to force a color mode.
func WithANSI256() Option {
return func(data *OptionData) {
data.out = termenv.NewOutput(data.buf, termenv.WithProfile(termenv.ANSI256))
}
}
// WithMaxRoutines specifies how many go routines are allowed to be spawned for calculating the image.
func WithMaxRoutines(routines int) Option {
return func(data *OptionData) {
data.routines = routines
}
}
// WithColorPairMax specifies how many color pair possibilities the algorithm will try per 8x8 pixel chunk. Lower value
// results in better performance but colors and selected symbols might be suboptimal. Values between 1 and 12 are sensible.
// Default is 6.
func WithColorPairMax(pairs int) Option {
return func(data *OptionData) {
data.colorPairs = pairs
}
}
// WithFontScaling sets the vertical font scaling size, as most terminal fonts are taller than wider.
func WithFontScaling(scale float64) Option {
return func(data *OptionData) {
data.fontRatio = scale
}
}
// WithPattern specifies the character patterns that are usable in the algorithms. More patterns decrease the performance.
func WithPattern(pattern ...[]charmaps.Pattern) Option {
return func(data *OptionData) {
data.pattern = charmaps.Combine(pattern...)
}
}
// WithSharpen sharpens the images before conversion.
func WithSharpen(times int) Option {
return func(data *OptionData) {
data.img = effect.Sharpen(data.img)
}
}
// WithGrayScale changes the image to grayscale.
func WithGrayScale() Option {
return func(data *OptionData) {
data.img = effect.Grayscale(data.img)
}
}
// WithBrightness changes the brightness of the image.
func WithBrightness(change float64) Option {
return func(data *OptionData) {
data.img = adjust.Brightness(data.img, change)
}
}
// WithContrast changes the contrast of the image.
func WithContrast(change float64) Option {
return func(data *OptionData) {
data.img = adjust.Contrast(data.img, change)
}
}
// WithSaturation changes the saturation of the image.
func WithSaturation(change float64) Option {
return func(data *OptionData) {
data.img = adjust.Saturation(data.img, change)
}
}
// WithFlip flips the picture vertically or horizontal.
func WithFlip(h bool, v bool) Option {
return func(data *OptionData) {
if h {
data.img = transform.FlipH(data.img)
}
if v {
data.img = transform.FlipV(data.img)
}
}
}