-
Notifications
You must be signed in to change notification settings - Fork 1
/
box.go
102 lines (82 loc) · 2.04 KB
/
box.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
package extractor
import (
"fmt"
)
type box struct {
r1, r2 int
g1, g2 int
b1, b2 int
histogram map[int]int
color *pixel
index int
}
func newBox(r1 int, r2 int, g1 int, g2 int, b1 int, b2 int) *box {
return &box{r1: r1, r2: r2, g1: g1, g2: g2, b1: b1, b2: b2}
}
func (box *box) volume() int {
subr := box.r2 - box.r1
subg := box.g2 - box.g1
subb := box.b2 - box.b1
return (subr + 1) * (subg + 1) * (subb + 1)
}
func (box *box) count() int {
n := 0
for i := box.r1; i <= box.r2; i++ {
for j := box.g1; j <= box.g2; j++ {
for k := box.b1; k <= box.b2; k++ {
if val, ok := box.histogram[getColorIndex(i, j, k)]; ok {
n += val
}
}
}
}
return n
}
func (box *box) copy() *box {
rbox := newBox(box.r1, box.r2, box.g1, box.g2, box.b1, box.b2)
rbox.histogram = make(map[int]int, len(box.histogram))
for k, v := range box.histogram {
rbox.histogram[k] = v
}
return rbox
}
func (box *box) average() *pixel {
n := 0
mult := 1 << (8 - bitsig)
pixel := &pixel{0, 0, 0, 255}
for i := box.r1; i <= box.r2; i++ {
for j := box.g1; j <= box.g2; j++ {
for k := box.b1; k <= box.b2; k++ {
if val, ok := box.histogram[getColorIndex(i, j, k)]; ok {
n += val
pixel.R += val*i*mult + val*mult/2
pixel.G += val*j*mult + val*mult/2
pixel.B += val*k*mult + val*mult/2
}
}
}
}
if n > 0 {
pixel.R /= n
pixel.G /= n
pixel.B /= n
} else {
pixel.R = mult * (box.r1 + box.r2 + 1) / 2
pixel.G = mult * (box.g1 + box.g2 + 1) / 2
pixel.B = mult * (box.b1 + box.b2 + 1) / 2
}
return pixel
}
func (box *box) contains(pixel *pixel) bool {
r := pixel.R >> rshift
g := pixel.G >> rshift
b := pixel.B >> rshift
return r >= box.r1 && r <= box.r2 && g >= box.g1 && g <= box.g2 && b >= box.b1 && b <= box.b2
}
func (box *box) print() {
fmt.Println(box)
fmt.Println(fmt.Sprintf("\tr1: %d, r2: %d", box.r1, box.r2))
fmt.Println(fmt.Sprintf("\tg1: %d, g2: %d", box.g1, box.g2))
fmt.Println(fmt.Sprintf("\tb1: %d, b2: %d", box.b1, box.b2))
fmt.Println(fmt.Sprintf("Count: %d", box.count()))
}