-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolarize_test.go
100 lines (91 loc) · 2.3 KB
/
polarize_test.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
package polarize
import (
"image"
"image/color"
"testing"
"github.com/takeyourhatoff/polarize/internal/hsv"
)
func BenchmarkPolarimetricImage_addSample(b *testing.B) {
r := image.Rect(0, 0, 1000, 1000)
sample := image.NewRGBA(r)
var img Image
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
img.AddSample(0, sample)
}
})
}
func BenchmarkPolarimetricImage_addYCbCrSample(b *testing.B) {
r := image.Rect(0, 0, 1000, 1000)
var img Image
sample := image.NewYCbCr(r, image.YCbCrSubsampleRatio420)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
img.AddSample(0, sample)
}
})
}
func BenchmarkPolarimetricImage_At(b *testing.B) {
r := image.Rect(0, 0, 1000, 1000)
var img Image
b.ResetTimer()
for i := 0; i < b.N; i++ {
for y := r.Min.Y; y < r.Max.Y; y++ {
for x := r.Min.X; x < r.Max.X; x++ {
_ = img.At(x, y)
}
}
}
}
func TestPolarimetricImage(t *testing.T) {
r := image.Rect(0, 0, 3, 1)
var pol Image
sample := image.NewGray(r)
for x := 0; x < 3; x++ {
sample.Set(x, 0, color.White)
pol.AddSample(x, sample)
sample.Set(x, 0, color.Black)
}
exp := []color.Color{
hsv.HSV{H: 0 / 3.0, S: 2 / 3.0, V: 1 / 3.0},
hsv.HSV{H: 1 / 3.0, S: 2 / 3.0, V: 1 / 3.0},
hsv.HSV{H: 2 / 3.0, S: 2 / 3.0, V: 1 / 3.0},
}
for x, c := range exp {
if colorEq(pol.At(x, 0), c) == false {
t.Errorf("x=%v, pol.At(x, 0) = %#v, exp[x] = %#v\n", x, pol.At(x, 0), c)
}
}
}
func setYCbCr(i *image.YCbCr, x, y int, c color.Color) {
Y := color.GrayModel.Convert(c).(color.Gray).Y
j := i.YOffset(x, y)
i.Y[j] = Y
}
func TestPolarimetricYCbCrImage(t *testing.T) {
r := image.Rect(0, 0, 3, 1)
var pol Image
sample := image.NewYCbCr(r, image.YCbCrSubsampleRatio420)
for x := 0; x < 3; x++ {
setYCbCr(sample, x, 0, color.White)
pol.AddSample(x, sample)
setYCbCr(sample, x, 0, color.Black)
}
exp := []color.Color{
hsv.HSV{H: 0 / 3.0, S: 2 / 3.0, V: 1 / 3.0},
hsv.HSV{H: 1 / 3.0, S: 2 / 3.0, V: 1 / 3.0},
hsv.HSV{H: 2 / 3.0, S: 2 / 3.0, V: 1 / 3.0},
}
for x, c := range exp {
if colorEq(pol.At(x, 0), c) == false {
t.Errorf("x=%v, pol.At(x, 0) = %#v, exp[x] = %#v\n", x, pol.At(x, 0), c)
}
}
}
func colorEq(a, b color.Color) bool {
r1, g1, b1, a1 := a.RGBA()
r2, g2, b2, a2 := b.RGBA()
return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2
}