-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (149 loc) · 4.03 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
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
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
_ "image/jpeg"
"image/png"
"os"
"strings"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
func main() {
// Handle command line arguments
imagePath := flag.String("i", "", "Path to the image file")
output := flag.String("o", "stdout", "Output option: stdout or png or txt")
width := flag.Int("w", 64, "Width to scale the image to")
height := flag.Int("h", 32, "Height to scale the image to")
// Override the default usage function
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintln(os.Stderr, " -i string")
fmt.Fprintln(os.Stderr, " Path to the image file")
fmt.Fprintln(os.Stderr, " -o string")
fmt.Fprintln(os.Stderr, " Output option: stdout or png or txt (default \"stdout\")")
fmt.Fprintln(os.Stderr, " -w int")
fmt.Fprintln(os.Stderr, " Width to scale the image to (default 64)")
fmt.Fprintln(os.Stderr, " -h int")
fmt.Fprintln(os.Stderr, " Height to scale the image to (default 32)")
}
flag.Parse()
if *imagePath == "" {
fmt.Println("No image provided. Quitting.")
os.Exit(1)
}
img, err := decodeImage(*imagePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
scaled := scaleImage(img, *width, *height)
gray := convertToGray(scaled)
ascii := mapToASCII(gray)
switch *output {
case "stdout":
printToSTDOUT(ascii)
case "png":
exportToPNG(ascii, "output.png")
case "txt":
exportToTXT(ascii, "output.txt")
default:
fmt.Println("Invalid output option. Quitting.")
os.Exit(1)
}
}
func decodeImage(imagePath string) (image.Image, error) {
file, err := os.Open(imagePath)
if err != nil {
return nil, fmt.Errorf("failed to open image: %w", err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, fmt.Errorf("failed to decode image: %w", err)
}
return img, nil
}
func scaleImage(img image.Image, width, height int) image.Image {
bounds := img.Bounds()
scaled := image.NewRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
srcX := x * bounds.Dx() / width
srcY := y * bounds.Dy() / height
scaled.Set(x, y, img.At(srcX, srcY))
}
}
return scaled
}
func convertToGray(img image.Image) *image.Gray {
bounds := img.Bounds()
gray := image.NewGray(bounds)
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
originalColor := img.At(x, y)
grayColor := color.GrayModel.Convert(originalColor).(color.Gray)
gray.SetGray(x, y, grayColor)
}
}
return gray
}
func mapToASCII(img *image.Gray) string {
bounds := img.Bounds()
ascii := " .:-=+*#%@"
buf := make([]byte, 0, bounds.Dx()*bounds.Dy())
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
c := img.GrayAt(x, y)
i := int(float64(c.Y) * 9 / 255)
buf = append(buf, ascii[i])
}
buf = append(buf, '\n')
}
return string(buf)
}
func printToSTDOUT(ascii string) {
for _, char := range ascii {
fmt.Print(string(char))
}
}
func exportToTXT(ascii string, outputPath string) {
file, err := os.Create(outputPath)
if err != nil {
fmt.Println("Error: File could not be created")
os.Exit(1)
}
defer file.Close()
if _, err := file.WriteString(ascii); err != nil {
fmt.Println("Error: ASCII could not be written")
os.Exit(1)
}
}
func exportToPNG(ascii string, outputPath string) {
lines := strings.Split(ascii, "\n")
img := image.NewRGBA(image.Rect(0, 0, len(lines[0])*6, len(lines)*12))
draw.Draw(img, img.Bounds(), image.White, image.Point{}, draw.Src)
d := &font.Drawer{
Dst: img,
Src: image.Black,
Face: basicfont.Face7x13,
}
for y, line := range lines {
d.Dot = fixed.P(0, (y+1)*12)
d.DrawString(line)
}
file, err := os.Create(outputPath)
if err != nil {
fmt.Println("Error: File could not be created")
os.Exit(1)
}
defer file.Close()
if err := png.Encode(file, img); err != nil {
fmt.Println("Error: Image could not be encoded")
os.Exit(1)
}
}