-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
207 lines (157 loc) · 4.18 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"image"
"image/color"
_ "image/jpeg"
_ "image/png"
"io"
"os"
"slices"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/nfnt/resize"
)
var (
allowedScaleValues = []string{"1/1", "1/2", "1/4", "1/8", "1/12", "1/16", "1/32", "1/64"}
pathPtr = flag.String("path", "", "Path to the image to be analyzed.")
scalingPtr = flag.String("scaling", "1/8", "Image scaling used to speed up the program a bit. Available options: 1/1, 1/2, 1/4, 1/8, 1/12, 1/16, 1/32, 1/64")
threadsPtr = flag.Int("threads", 20, "Amount of threads to be created by the process.")
returnAmountPtr = flag.Int("return-amount", 5, "Amount of colors to be returned.")
debugPtr = flag.Bool("debug", false, "Should additional data useful for debugging be shown?")
)
type KeyValue struct {
Key string
Value int
}
func loadImageAsBuffer(path *string) *bytes.Buffer {
data, err := os.Open(*path)
if err != nil {
panic(err)
}
defer data.Close()
reader := bufio.NewReader(data)
buffer := bytes.NewBuffer(make([]byte, 0))
part := make([]byte, 1024)
var count int
for {
if count, err = reader.Read(part); err != nil {
break
}
buffer.Write(part[:count])
}
if err != io.EOF {
panic(err)
} else {
err = nil
}
return buffer
}
// Returns 2 booleans for both X and Y
func checkIfOutOfBounds(x int, y int, bounds image.Rectangle) (bool, bool) {
xOut := false
yOut := false
if x < bounds.Min.X || x >= bounds.Max.X {
xOut = true
}
if y < bounds.Min.Y || y >= bounds.Max.Y {
yOut = true
}
return xOut, yOut
}
func scaleCoordinates(img image.Image) (uint, uint, error) {
scaling, err := strconv.Atoi(strings.Split(*scalingPtr, "/")[1])
if err != nil {
return 0, 0, err
}
scaledX := img.Bounds().Max.X
scaledY := img.Bounds().Max.Y
if scaling > 1 {
scaledX = img.Bounds().Max.X / (scaling / 2)
scaledY = img.Bounds().Max.Y / (scaling / 2)
}
return uint(scaledX), uint(scaledY), nil
}
func main() {
flag.Parse()
if !slices.Contains(allowedScaleValues, *scalingPtr) {
panic("Invalid scaling provided. Expected one of: 1/1, 1/2, 1/4, 1/8, 1/12, 1/16, 1/32, 1/64")
}
isDebug := *debugPtr
start := time.Now()
imgBuffer := loadImageAsBuffer(pathPtr)
img, _, err := image.Decode(bytes.NewReader(imgBuffer.Bytes()))
if err != nil {
panic(err)
}
scaledX, scaledY, err := scaleCoordinates(img)
if err != nil {
panic(err)
}
tempImage := resize.Resize(scaledX, scaledY, img, resize.Bilinear)
img = tempImage
pixelCount := img.Bounds().Max.X * img.Bounds().Max.Y
colorsMap := make(map[string]int)
var wg sync.WaitGroup
var mutex sync.Mutex
chunkSize := pixelCount / *threadsPtr
if isDebug {
fmt.Println("Iterating over", pixelCount, "pixels")
fmt.Println("Chunk size:", chunkSize)
}
for w := 0; w < *threadsPtr; w++ {
wg.Add(1)
go func(workerID, startPixel, endPixel int) {
defer wg.Done()
localColorsMap := make(map[string]int)
x := startPixel % img.Bounds().Max.X
y := startPixel / img.Bounds().Max.X
for i := startPixel; i < endPixel; i++ {
xOut, yOut := checkIfOutOfBounds(x, y, img.Bounds())
if xOut {
x = 0
y++
}
if yOut {
panic("Y coordinate out of bounds")
}
rgba := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
toString := fmt.Sprintf("%d;%d;%d", rgba.R, rgba.G, rgba.B)
localColorsMap[toString]++
x++
}
mutex.Lock()
for k, v := range localColorsMap {
colorsMap[k] += v
}
mutex.Unlock()
}(w, w*chunkSize, (w+1)*chunkSize)
}
wg.Wait()
var keyValuePairs []KeyValue
for k, v := range colorsMap {
keyValuePairs = append(keyValuePairs, KeyValue{k, v})
}
sort.Slice(keyValuePairs, func(i, j int) bool {
return keyValuePairs[i].Value > keyValuePairs[j].Value
})
for i := 0; i < *returnAmountPtr; i++ {
if i >= len(keyValuePairs) {
break
}
rgb := strings.Split(keyValuePairs[i].Key, ";")
r, g, b := rgb[0], rgb[1], rgb[2]
formatString := fmt.Sprintf("[\033[38;2;%s;%s;%sm███\033[0;00m] %s,%s,%s -> Seen %d times", r, g, b, r, g, b, keyValuePairs[i].Value)
fmt.Println(formatString)
}
if isDebug {
duration := time.Since(start)
fmt.Println("Code took", duration)
}
}