-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (171 loc) · 3.49 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
package main
import (
"bytes"
"flag"
"image"
"image/color"
"image/png"
"io/ioutil"
"log"
"os"
"gonum.org/v1/gonum/stat/distuv"
)
var dir = flag.String("path", "images", "path to the images dir")
const size = 256
func main() {
flag.Parse()
a := analyzer{
colors: make(map[byte]int),
changes: make(map[int]int),
}
files, _ := ioutil.ReadDir(*dir)
for _, f := range files {
file, err := os.Open(*dir + "/" + f.Name())
if err != nil {
log.Panicln("File open error", f.Name(), err)
}
img, err := png.Decode(file)
if err != nil {
log.Panicln("Image decode error", f.Name(), err)
}
a.process(img.(*image.NRGBA))
log.Printf("Ready: " + f.Name())
}
max := 0
for _, val := range a.changes {
if max < val {
max = val
}
}
log.Println("Max changes: ", max)
max = max / 2
sample := make([]float64, size*size)
for i, val := range a.changes {
sample[i] = float64(val) / float64(max)
}
changes := image.NewNRGBA(image.Rect(0, 0, size, size))
unchanged := image.NewNRGBA(image.Rect(0, 0, size, size))
normal := distuv.Normal{
Mu: 0.5,
Sigma: 5,
}
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
curr := a.changes[y*size+x]
popularity := float64(curr) / float64(max)
prob := 1 - normal.LogProb(popularity)
slide := int(255 * 4 * prob * popularity)
if slide > 255*4 {
slide = 255 * 4
}
red := slide - 255*2
green := 0
if slide < 255*2 {
green = slide
} else {
green = 4*255 - slide
}
blue := 2*255 - slide
changes.Set(x, y, color.NRGBA{A: 255, R: uint8(clamp(red)), G: uint8(clamp(green)), B: uint8(clamp(blue))})
if curr == 0 {
unchanged.Set(x, y, color.NRGBA{A: 255, R: 255, G: 255, B: 255})
} else {
unchanged.Set(x, y, color.NRGBA{A: 255, R: 0, G: 0, B: 0})
}
}
}
b := bytes.Buffer{}
_ = png.Encode(&b, changes)
_ = ioutil.WriteFile("changes.png", b.Bytes(), 0644)
b.Reset()
_ = png.Encode(&b, unchanged)
_ = ioutil.WriteFile("unchanged.png", b.Bytes(), 0644)
}
type analyzer struct {
last *image.NRGBA
colors map[byte]int
changes map[int]int
changed int
frame int
}
func (a *analyzer) process(img *image.NRGBA) {
a.frame++
if a.last == nil {
a.last = img
return
}
last := a.last
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
i := last.PixOffset(x, y)
prev := last.Pix[i : i+4 : i+4]
curr := img.Pix[i : i+4 : i+4]
for i := 0; i < 4; i++ {
if prev[i] != curr[i] {
idx := getColorIndex(img.NRGBAAt(x, y))
if idx == 16 {
panic("Unknown color")
}
a.colors[idx] = a.colors[idx] + 1
oldChanges := a.changes[y*size+x]
a.changes[y*size+x] = oldChanges + 1
if oldChanges == 0 {
a.changed++
if a.changed == size*size {
panic(a.frame)
}
}
break
}
}
}
}
a.last = img
}
func getColorIndex(c color.NRGBA) byte {
argb := (uint32(c.A) << 24) | (uint32(c.R) << 16) | (uint32(c.G) << 8) | uint32(c.B)
switch argb {
case 0xFFe4e4e4:
return 0
case 0xFFea7e35:
return 1
case 0xFFbe49c9:
return 2
case 0xFF4FB5D2:
return 3
case 0xFFE0C037:
return 4
case 0xFF39ba2e:
return 5
case 0xFFd98199:
return 6
case 0xFF414141:
return 7
case 0xFFa0a7a7:
return 8
case 0xFF267191:
return 9
case 0xFF7e34bf:
return 10
case 0xFF253193:
return 11
case 0xFF70482a:
return 12
case 0xFF364b18:
return 13
case 0xFFad2e2b:
return 14
case 0xFF181414:
return 15
}
return 16
}
func clamp(c int) int {
if c > 255 {
return 255
}
if c < 0 {
return 0
}
return c
}