-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (136 loc) · 3.96 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
package main
import (
"fmt"
"image"
"image/jpeg"
"math"
"os"
"path/filepath"
)
//darkest to lightest for the acsii
const asciiBrightness = "`^\",:;Il!i~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"
//slice for the brightness for the acsii
var (
brightness []uint32
boundX int
arg map[string]bool
color map[string]string
)
func getImage(img string) {
//Walk walks the file tree using the path, start from the img and apply the function
filepath.Walk(img, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
image := loadImage(path)
getPixels(image)
return nil
})
}
//decode the image and returns it
func loadImage(filename string) image.Image {
f, err := os.Open(filename)
if err != nil {
//just to remove the package log
fmt.Println(err.Error())
os.Exit(1)
}
//it will exetude this if the function finally returns the statment
defer f.Close()
img, err := jpeg.Decode(f)
if err != nil {
//just to remove the package log
fmt.Println(err.Error())
os.Exit(1)
}
return img
}
//return a slice of pixels, fetchs all the pixels
func getPixels(img image.Image) {
//fetch the bound of the image to use the height and the width of the image
bound := img.Bounds()
boundX = bound.Dx()
//fmt.Printf("amout of pixels: %d x %d\n", bound.Dx(), bound.Dy())
//the length/dimention of all the pixels in the image
dime := bound.Dx() * bound.Dy()
for i := 0; i < dime; i++ {
//the x is a pixel, if the image as 480*503 it will have 241440 pixels
//every time the i encrements it will give the pixels position
//if we do 3%480=3 and if we do 483%480=3
x := i % bound.Dx()
//the y is the row, if we do 3/480=0.0... and if we do 483/480=1.006.. it's gona give the next row of the image
y := i / bound.Dx()
// At returns the color of the pixel at (x, y) of the image
r, g, b, _ := img.At(x, y).RGBA()
//average := (r + g + b) / 3 <- other way but not that good, there are alot of ways to do it.
average := math.Sqrt(0.299*math.Pow(float64(r), 2) + 0.587*math.Pow(float64(g), 2) + 0.114*math.Pow(float64(b), 2)) // <- this is the best way to get the brightness
brightness = append(brightness, uint32(average/257))
}
}
func colors() {
if color == nil {
color = make(map[string]string)
}
color["blue"] = "\033[0;34m"
color["red"] = "\033[0;31m"
color["green"] = "\033[0;32m"
color["purple"] = "\033[0;35m"
color["brown"] = "\033[0;33m"
}
func reverse(value int) int {
a := 34
b := 33
valueReverse := (value * b) / a
return valueReverse
}
func argVerification(baseBri, baseChar, i int) (int, bool) {
for k, v := range arg {
if v == true && k == "--up" {
return (baseChar * int(brightness[len(brightness)-1-i])) / baseBri, false
} else if v == true && k == "--reverseColor" {
value := (baseChar * int(brightness[i])) / baseBri
return reverse(value), false
} else if v == true && k == "--color" {
colors()
return (baseChar * int(brightness[i])) / baseBri, true
} else {
return (baseChar * int(brightness[i])) / baseBri, false
}
}
return (baseChar * int(brightness[i])) / baseBri, false
}
func main() {
if len(os.Args) >= 2 {
if arg == nil {
arg = make(map[string]bool)
arg["--up"] = false
arg["--reverseColor"] = false
arg["--color"] = false
}
for i := 1; i < len(os.Args); i++ {
arg[os.Args[i]] = true
}
}
getImage("b.jpg")
br := []rune(asciiBrightness)
colorArg := os.Args[len(os.Args)-1]
//mapping the acsii with the brigthness using formula
//this will be the middle of the brightness slice and the middle of the asciiBrightness slice
baseBri := 127
baseChar := 32
for i := 0; i < len(brightness); i++ {
x := i % boundX
if x == 0 {
fmt.Println()
}
formula, ok := argVerification(baseBri, baseChar, i)
if ok {
fmt.Print(color[colorArg] + string(br[formula]))
fmt.Print(color[colorArg] + string(br[formula]))
} else {
//fmt.Println(br[formula], formula)
fmt.Print(string(br[formula]))
fmt.Print(string(br[formula]))
}
}
}