/
render.go
214 lines (203 loc) · 4.4 KB
/
render.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
208
209
210
211
212
213
214
package flame
import (
// "fmt"
"image"
"image/color"
"math"
"strconv"
)
func printMx(mx *[][]int) {
println("Year", len(*mx), len((*mx)[0]))
for _, row := range *mx {
s := ""
for _, v := range row {
s += strconv.Itoa(v) + " "
}
println(s)
}
}
func renderMatrix(matrix *[][]Pixel, log bool) *image.RGBA {
height := len(*matrix)
width := len((*matrix)[0])
mx := make([][]int, len(*matrix))
for y := range mx {
mx[y] = make([]int, width)
for x := range mx[y] {
mx[y][x] = int((*matrix)[y][x].Alpha)
}
}
return matrixToImage(&mx, width, height, log)
}
// data the data and render it within certain dimentions
func render(width, height int, data *[]Point, log bool) *image.RGBA {
// now render
mx := make([][]int, height)
for y := range mx {
mx[y] = make([]int, width)
}
for _, v := range *data {
mx[int((v.Y+1)/2*float64(height-1))][int((v.X+1)/2*float64(width-1))] += 1
}
return matrixToImage(&mx, width, height, log)
}
func matrixToImage(mx *[][]int, width, height int, log bool) *image.RGBA {
if log {
if !equalize_log(mx, 255, .995, .0005) {
return nil
}
} else {
equalize(mx, 255, .995, .0005)
}
image := image.NewRGBA(image.Rect(0, 0, width, height))
// now write the values to an image, equalized by the 3rd-brightest point
for x, row := range *mx {
for y, v := range row {
val := uint8(v)
image.Set(x, y, color.RGBA{val, val * 100 / 255, val, 255})
}
}
return image
}
func equalize(arr *[][]int, values int, maxp, minp float64) {
mx := 0
total := len(*arr) * len((*arr)[0])
for _, row := range *arr {
for _, v := range row {
if mx < v {
mx = v
}
}
}
hist := make([]int, mx+1)
for _, row := range *arr {
for _, v := range row {
hist[v] += 1
}
}
min := 0
max := 0
top := int(float64(total) * maxp)
bottom := int(float64(total) * minp)
count := 0
// fmt.Printf("Eq", mx, total, top, bottom)
// fmt.Printf("Hist", hist)
for i, n := range hist {
count += n
if count > bottom && min == 0 {
min = i
}
if count > top {
max = i
break
}
}
println("Eq", mx, total, min, max)
if min == max {
min = 0
if mx > 1 {
max = mx / 2
} else {
max = 1
}
}
for i := range *arr {
for j := range (*arr)[i] {
if max == min {
(*arr)[i][j] = 0
continue
}
if (*arr)[i][j] > max {
(*arr)[i][j] = max
}
if (*arr)[i][j] < min {
(*arr)[i][j] = min
}
(*arr)[i][j] = values * ((*arr)[i][j] - min) / (max - min)
}
}
}
func find_max(mx *[][]float64) float64 {
max := 0.0
for y := range *mx {
for x, v := range (*mx)[y] {
if v > 0 {
(*mx)[y][x] = math.Log(v) / v
}
if max < v {
max = v
}
}
}
return max
}
func make_histogram(mx *[][]float64, by float64, max float64) *[]int {
hist := make([]int, int(by)+1)
for y := range *mx {
for x := range (*mx)[y] {
a := int((*mx)[y][x]/max*by)
/*
if a > int(by) {
fmt.Println("WAT", a, by, (*mx)[y][x], max)
}
fmt.Println("ait", a, by, (*mx)[y][x], max)
*/
hist[a] += 1
}
}
return &hist
}
func get_max_min(hist *[]int, total int, maxp, minp float64) (int, int) {
min := 0
max := 0
top := int(float64(total) * maxp)
bottom := int(float64(total) * minp)
count := 0
// fmt.Printf("Eq", mx, total, top, bottom)
// fmt.Printf("Hist", hist)
for i, n := range *hist {
count += n
if count > bottom && min == 0 {
min = i
}
if count > top {
max = i
break
}
}
return max, min
}
func equalize_log(irr *[][]int, values int, maxp, minp float64) bool {
arr := make([][]float64, len(*irr))
for y := range arr {
arr[y] = make([]float64, len((*irr)[y]))
for x := range arr[y] {
arr[y][x] = float64((*irr)[y][x])
}
}
mx := find_max(&arr)
if mx == 0 {
return false
}
by := 255 * 10.0
total := len(arr) * len((arr)[0])
hist := make_histogram(&arr, by, mx)
max, min := get_max_min(hist, total, maxp, minp)
// fmt.Printf("Eq", mx, total, min, max)
for i := range arr {
for j := range (arr)[i] {
if max == min {
(arr)[i][j] = 0
continue
}
v := int((arr)[i][j] * by / mx)
if v > max {
v = max
}
if v < min {
v = min
}
(*irr)[i][j] = int(values * (v - min) / (max - min))
}
}
return true
}