-
Notifications
You must be signed in to change notification settings - Fork 1
/
dct.go
57 lines (52 loc) · 1.05 KB
/
dct.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
package blind_watermark
import (
"math"
)
func dct(x []float64) []float64 {
ret := make([]float64, len(x))
for k, _ := range x {
for n, _ := range x {
q := math.Sqrt(1 / float64(len(x)))
if k != 0 {
q = math.Sqrt(2 / float64(len(x)))
}
ret[k] += q * x[n] * math.Cos(math.Pi*(float64(n)+0.5)*float64(k)/float64(len(x)))
}
}
return ret
}
func idct(xk []float64) []float64 {
ret := make([]float64, len(xk))
for n, _ := range xk {
for k, _ := range xk {
q := math.Sqrt(1 / float64(len(xk)))
if k != 0 {
q = math.Sqrt(2 / float64(len(xk)))
}
ret[n] += q * xk[k] * math.Cos(math.Pi*(float64(n)+0.5)*float64(k)/float64(len(xk)))
}
}
return ret
}
func Dct2(x [][]float64) [][]float64 {
for i, v := range x {
x[i] = dct(v)
}
x = switchRowAndColumns(x)
for i, v := range x {
x[i] = dct(v)
}
x = switchRowAndColumns(x)
return x
}
func Idct2(x [][]float64) [][]float64 {
x = switchRowAndColumns(x)
for i, v := range x {
x[i] = idct(v)
}
x = switchRowAndColumns(x)
for i, v := range x {
x[i] = idct(v)
}
return x
}