-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (76 loc) · 1.64 KB
/
main.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
package main
import (
asciiArt "ascii-art-gen/internal/ascii_art"
"ascii-art-gen/internal/img"
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
const (
DefaultMagnification = 1.0
)
type Inputs struct {
path string
threshold int
magnification float64
}
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.IntFlag{
Name: "threshold",
Aliases: []string{"t"},
Usage: "the threshold for ASCII Art Generation",
},
&cli.Float64Flag{
Name: "magnification",
Aliases: []string{"m"},
Usage: "the magnification factor for ASCII Art Generation",
},
},
Action: func(c *cli.Context) error {
err := runApp(c)
if err != nil {
return err
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func runApp(c *cli.Context) error {
inputs := Inputs{}
if c.NArg() != 1 {
return fmt.Errorf("invalid number of arguments")
}
inputs.path = c.Args().Get(0)
// Load the image
srcImg, err := img.Load(inputs.path)
if err != nil {
return err
}
if c.IsSet("magnification") {
inputs.magnification = c.Float64("magnification")
} else {
inputs.magnification = DefaultMagnification
}
// Resize the image
resizedImg := img.Resize(srcImg, inputs.magnification)
if c.IsSet("threshold") {
inputs.threshold = c.Int("threshold")
} else {
inputs.threshold = asciiArt.CalcOTSUThreshold(resizedImg, resizedImg.Bounds().Dy(), resizedImg.Bounds().Dx())
}
// Generate the ASCII Art
output := asciiArt.Generate(resizedImg, inputs.threshold)
// Print the ASCII Art
fmt.Print(output)
err = img.UnSync(resizedImg)
if err != nil {
return err
}
return nil
}