-
Notifications
You must be signed in to change notification settings - Fork 0
/
cld.go
197 lines (175 loc) · 3.67 KB
/
cld.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
package mpeg7cld
import (
"image"
"image/color"
"math"
)
// Colour Layout Descriptor
// see: https://en.wikipedia.org/wiki/Color_layout_descriptor
func CLD(img image.Image) [64]YCbCr {
partitions := partition(img)
averages := average(partitions)
ycbcr := rgb2ycbcr(averages)
dct := dct(ycbcr)
return zigzag(dct)
}
type rgb struct {
r, g, b uint8
}
func (c rgb) RGBA() (r, g, b, a uint32) {
return uint32(c.r), uint32(c.g), uint32(c.b), 0xFF
}
type YCbCr struct {
Y, Cb, Cr float64
}
func partition(img image.Image) [64][]color.Color {
var blocks [64][]color.Color
width := img.Bounds().Max.X
height := img.Bounds().Max.Y
min := func(a, b int) int {
if a <= b {
return a
}
return b
}
partitionWidth := int(float64(width) / 8)
partitionHeight := int(float64(height) / 8)
var x, y, i int
for x = 0; x < partitionWidth*8; x += partitionWidth {
for y = 0; y < partitionHeight*8; y += partitionHeight {
w := min(partitionWidth, width-x)
h := min(partitionHeight, height-y)
blocks[i] = make([]color.Color, 0, w*h)
for dx := x; dx < x+w; dx++ {
for dy := y; dy < y+h; dy++ {
blocks[i] = append(blocks[i], img.At(dx, dy))
}
}
i++
}
}
return blocks
}
func average(partitions [64][]color.Color) [64]rgb {
var blocks [64]rgb
for i, partition := range partitions {
var sumRed, sumBlue, sumGreen uint32
for _, c := range partition {
r, g, b, _ := c.RGBA()
sumRed += r >> 8
sumBlue += b >> 8
sumGreen += g >> 8
}
blocks[i] = rgb{
r: uint8(int(sumRed) / len(partition)),
g: uint8(int(sumBlue) / len(partition)),
b: uint8(int(sumGreen) / len(partition)),
}
}
return blocks
}
func rgb2ycbcr(img [64]rgb) [64]YCbCr {
var blocks [64]YCbCr
for i, p := range img {
y, cb, cr := color.RGBToYCbCr(p.r, p.g, p.b)
blocks[i] = YCbCr{
Y: float64(y),
Cb: float64(cb),
Cr: float64(cr),
}
}
return blocks
}
func dct(in [64]YCbCr) [64]YCbCr {
var out [64]YCbCr
for p := 0; p < 8; p++ {
for q := 0; q < 8; q++ {
var alphaP float64
if p > 0 {
alphaP = math.Sqrt(2.0 / 8.0)
} else {
alphaP = float64(1.0) / math.Sqrt(8)
}
var alphaQ float64
if q > 0 {
alphaQ = math.Sqrt(2.0 / 8.0)
} else {
alphaQ = float64(1.0) / math.Sqrt(8)
}
var sumY, sumCb, sumCr float64
for m := 0; m < 8; m++ {
for n := 0; n < 8; n++ {
c := math.Cos(math.Pi*(2*float64(m)+1)*float64(p)/16.0) * math.Cos(math.Pi*(2*float64(n)+1)*float64(q)/16.0)
i := index(m, n)
sumY += float64(in[i].Y) * c
sumCb += float64(in[i].Cb) * c
sumCr += float64(in[i].Cr) * c
}
}
i := index(p, q)
out[i].Y = alphaP * alphaQ * sumY
out[i].Cb = alphaP * alphaQ * sumCb
out[i].Cr = alphaP * alphaQ * sumCr
}
}
return out
}
type direction int
const (
DOWN direction = iota
UP
)
func zigzag(in [64]YCbCr) [64]YCbCr {
var (
x, y, i int
direction direction = UP
)
for {
j := index(y, x)
in[i], in[j] = in[j], in[i]
i++
if x == 7 && y == 7 {
break
}
switch direction {
case UP:
switch {
case x == 7:
y++
direction = DOWN
case y == 0:
x++
direction = DOWN
default:
x++
y--
}
case DOWN:
switch {
case y == 7:
x++
direction = UP
case x == 0:
y++
direction = UP
default:
x--
y++
}
}
}
return in
}
func index(x, y int) int {
i := y + 8*x
return int(i)
}
func Compare(cld1, cld2 [64]YCbCr) float64 {
var result float64
for i := 0; i < 64; i++ {
result += math.Sqrt((cld1[i].Y - cld2[i].Y) * (cld1[i].Y - cld2[i].Y))
result += math.Sqrt((cld1[i].Cb - cld2[i].Cb) * (cld1[i].Cb - cld2[i].Cb))
result += math.Sqrt((cld1[i].Cr - cld2[i].Cr) * (cld1[i].Cr - cld2[i].Cr))
}
return result
}