-
Notifications
You must be signed in to change notification settings - Fork 7
/
encode.go
144 lines (124 loc) · 3.65 KB
/
encode.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
package blurhash
import (
"image"
"image/color"
"math"
"strings"
"github.com/bbrks/go-blurhash/base83"
)
const (
minComponents = 1
maxComponents = 9
)
// Encode returns the blurhash for the given image.
func Encode(xComponents, yComponents int, img image.Image) (hash string, err error) {
if xComponents < minComponents || xComponents > maxComponents ||
yComponents < minComponents || yComponents > maxComponents {
return "", ErrInvalidComponents
}
b := strings.Builder{}
sizeFlag := (xComponents - 1) + (yComponents-1)*9
sizeFlagEncoded, err := base83.Encode(sizeFlag, 1)
if err != nil {
return "", err
}
_, err = b.WriteString(sizeFlagEncoded)
if err != nil {
return "", err
}
// vector of yComponents*xComponents*(RGB)
factors := make([][][3]float64, yComponents)
for y := 0; y < yComponents; y++ {
factors[y] = make([][3]float64, xComponents)
for x := 0; x < xComponents; x++ {
factor := multiplyBasisFunction(x, y, img)
factors[y][x][0] = factor[0]
factors[y][x][1] = factor[1]
factors[y][x][2] = factor[2]
}
}
maximumValue := 0.0
if xComponents*yComponents-1 > 0 {
actualMaximumValue := 0.0
for y := 0; y < yComponents; y++ {
for x := 0; x < xComponents; x++ {
if y == 0 && x == 0 {
continue
}
actualMaximumValue = math.Max(math.Abs(factors[y][x][0]), actualMaximumValue)
actualMaximumValue = math.Max(math.Abs(factors[y][x][1]), actualMaximumValue)
actualMaximumValue = math.Max(math.Abs(factors[y][x][2]), actualMaximumValue)
}
}
quantisedMaximumValue := math.Max(0, math.Min(82, math.Floor(actualMaximumValue*166-0.5)))
maximumValue = (quantisedMaximumValue + 1) / 166
str, err := base83.Encode(int(quantisedMaximumValue), 1)
if err != nil {
return "", err
}
b.WriteString(str)
} else {
maximumValue = 1
str, err := base83.Encode(0, 1)
if err != nil {
return "", err
}
b.WriteString(str)
}
dc := factors[0][0]
str, err := base83.Encode(encodeDC(dc[0], dc[1], dc[2]), 4)
if err != nil {
return "", err
}
b.WriteString(str)
for y := 0; y < yComponents; y++ {
for x := 0; x < xComponents; x++ {
if y == 0 && x == 0 {
continue
}
str, err := base83.Encode(encodeAC(factors[y][x][0], factors[y][x][1], factors[y][x][2], maximumValue), 2)
if err != nil {
return "", err
}
b.WriteString(str)
}
}
return b.String(), nil
}
func encodeDC(r, g, b float64) int {
return (linearTosRGB(r) << 16) + (linearTosRGB(g) << 8) + linearTosRGB(b)
}
func encodeAC(r, g, b, maximumValue float64) int {
quantR := math.Max(0, math.Min(18, math.Floor(signPow(r/maximumValue, 0.5)*9+9.5)))
quantG := math.Max(0, math.Min(18, math.Floor(signPow(g/maximumValue, 0.5)*9+9.5)))
quantB := math.Max(0, math.Min(18, math.Floor(signPow(b/maximumValue, 0.5)*9+9.5)))
return int(quantR*19*19 + quantG*19 + quantB)
}
func multiplyBasisFunction(xComponents, yComponents int, img image.Image) [3]float64 {
var r, g, b float64
width, height := float64(img.Bounds().Dx()), float64(img.Bounds().Dy())
normalisation := 2.0
if xComponents == 0 && yComponents == 0 {
normalisation = 1.0
}
for x := 0; x < img.Bounds().Dx(); x++ {
for y := 0; y < img.Bounds().Max.Y; y++ {
//cR, cG, cB, _ := img.At(x, y).RGBA()
c, ok := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
if !ok {
panic("not color.NRGBA")
}
basis := math.Cos(math.Pi*float64(xComponents)*float64(x)/width) *
math.Cos(math.Pi*float64(yComponents)*float64(y)/height)
r += basis * sRGBToLinear(int(c.R))
g += basis * sRGBToLinear(int(c.G))
b += basis * sRGBToLinear(int(c.B))
}
}
scale := normalisation / (width * height)
return [3]float64{
r * scale,
g * scale,
b * scale,
}
}