-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransformer.go
77 lines (64 loc) · 1.66 KB
/
transformer.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
package main
import (
"image"
"io"
)
func getAsciiChar(brightness float64, invert bool) string {
const CODES = "Ñ@#W$9876543210?!abc;:+=-,._ "
var charCode int
if invert {
charCode = int(mapRange(brightness, 0, 255, 0, float64(len(CODES)-1)))
} else {
charCode = int(mapRange(brightness, 0, 255, float64(len(CODES)), 0))
}
return string(CODES[charCode])
}
type Pixel struct {
R int
G int
B int
}
func getAscii(data [][]Pixel, invert bool) string {
var result string
for y := 0; y < len(data); y++ {
var line string
for x := 0; x < len(data[y]); x++ {
line = line + getAsciiChar(brightness(data[y][x]), invert)
}
result = result + line + "\n"
}
return result
}
func getPixels(file io.Reader) ([][]Pixel, error) {
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
bounds := img.Bounds()
var pixels [][]Pixel
width, height := bounds.Max.X, bounds.Max.Y
for y := 0; y < height; y++ {
var row []Pixel
for x := 0; x < width; x++ {
row = append(row, rgbaToPixel(img.At(x, y).RGBA()))
}
pixels = append(pixels, row)
}
return pixels, nil
}
func rgbaToPixel(r uint32, g uint32, b uint32, _ uint32) Pixel {
return Pixel{int(r / 257), int(g / 257), int(b / 257)}
}
func mapRange(value, minInput, maxInput, minOutput, maxOutput float64) float64 {
// Clamping the value to the input range
if value < minInput {
value = minInput
} else if value > maxInput {
value = maxInput
}
// Calculate the proportion of the value in the input range
proportion := (value - minInput) / (maxInput - minInput)
// Map the proportion to the output range
newValue := proportion*(maxOutput-minOutput) + minOutput
return newValue
}