goimagehash/transforms/dct.go

70 lines
1.3 KiB
Go
Raw Normal View History

2017-07-28 10:18:24 -07:00
// Copyright 2017 The goimagehash Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package transforms
import (
"math"
"sync"
2017-07-28 10:18:24 -07:00
)
2017-07-31 07:24:04 -07:00
// DCT1D function returns result of DCT-II.
2017-07-28 10:18:24 -07:00
// Follows Matlab dct().
// Implementation reference:
// https://unix4lyfe.org/dct-1d/
func DCT1D(input []float64) []float64 {
n := len(input)
out := make([]float64, n)
for i := 0; i < n; i++ {
z := 0.0
for j := 0; j < n; j++ {
z += input[j] * math.Cos(math.Pi*(float64(j)+0.5)*float64(i)/float64(n))
}
if i == 0 {
z *= math.Sqrt(0.5)
}
out[i] = z * math.Sqrt(2.0/float64(n))
}
return out
}
2017-07-31 07:24:04 -07:00
// DCT2D function returns a result of DCT2D by using the seperable property.
2017-07-28 10:18:24 -07:00
func DCT2D(input [][]float64, w int, h int) [][]float64 {
output := make([][]float64, h)
for i := range output {
output[i] = make([]float64, w)
}
wg := new(sync.WaitGroup)
2017-07-28 10:18:24 -07:00
for i := 0; i < h; i++ {
wg.Add(1)
go func(i int) {
cols := DCT1D(input[i])
output[i] = cols
wg.Done()
}(i)
2017-07-28 10:18:24 -07:00
}
wg.Wait()
2017-07-28 10:18:24 -07:00
for i := 0; i < w; i++ {
wg.Add(1)
2017-07-28 10:18:24 -07:00
in := make([]float64, h)
go func(i int) {
for j := 0; j < h; j++ {
in[j] = output[j][i]
}
rows := DCT1D(in)
for j := 0; j < len(rows); j++ {
output[j][i] = rows[j]
}
wg.Done()
}(i)
2017-07-28 10:18:24 -07:00
}
wg.Wait()
2017-07-28 10:18:24 -07:00
return output
}