-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.go
276 lines (227 loc) · 5.05 KB
/
hash.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package thumbhash
import (
"errors"
"math"
)
var (
ErrInvalidHash = errors.New("invalid hash")
)
// Hash binary representation:
//
// L DC: 6 bit
// P DC: 6 bit
// Q DC: 6 bit
// L scale: 5 bit
// HasAlpha: 1 bit
//
// L count: 3 bit
// P scale: 6 bit
// Q scale: 6 bit
// IsLandscape: 1 bit
//
// If HasAlpha:
// A DC: 4 bit
// A scale: 4 bit
//
// L AC: 4 bit each
// P AC: 4 bit each
// Q AC: 4 bit each
//
// If HasAlpha:
// A AC: 4 bit each
// Hash represents the set of data stored in an image hash.
type Hash struct {
LDC float64
PDC float64
QDC float64
LScale float64
HasAlpha bool
Lx int
Ly int
LCount int
PScale float64
QScale float64
IsLandscape bool
ADC float64 // if HasAlpha
AScale float64 // if HasAlpha
LAC []float64
PAC []float64
QAC []float64
AAC []float64 // if HasAlpha
}
// Encode returns the binary representation of a hash.
func (h *Hash) Encode() []byte {
// Compute the size of the hash
nbAC := len(h.LAC) + len(h.PAC) + len(h.QAC)
if h.HasAlpha {
nbAC += len(h.AAC)
}
hashSize := 3 + 2 + (nbAC+1)/2
if h.HasAlpha {
hashSize += 1
}
hash := make([]byte, hashSize)
// First block (3 bytes)
header24 := iround(63.0 * h.LDC)
header24 |= iround(31.5+31.5*h.PDC) << 6
header24 |= iround(31.5+31.5*h.QDC) << 12
header24 |= iround(31.0*h.LScale) << 18
if h.HasAlpha {
header24 |= 1 << 23
}
hash[0] = byte(header24)
hash[1] = byte(header24 >> 8)
hash[2] = byte(header24 >> 16)
// Second block (2 bytes)
h.LCount = h.Lx
if h.IsLandscape {
h.LCount = h.Ly
}
header16 := h.LCount
header16 |= iround(63.0*h.PScale) << 3
header16 |= iround(63.0*h.QScale) << 9
if h.IsLandscape {
header16 |= 1 << 15
}
hash[3] = byte(header16)
hash[4] = byte(header16 >> 8)
// Alpha data
if h.HasAlpha {
hash[5] = byte(iround(15.0*h.ADC) | iround(15.0*h.AScale)<<4)
}
// AC coefficients
acs := [][]float64{h.LAC, h.PAC, h.QAC}
if h.HasAlpha {
acs = append(acs, h.AAC)
}
start := 5
if h.HasAlpha {
start = 6
}
idx := 0
for i := 0; i < len(acs); i++ {
ac := acs[i]
for j := 0; j < len(ac); j++ {
f := ac[j]
hash[start+(idx/2)] |= byte(iround(15.0*f) << ((idx & 1) * 4))
idx += 1
}
}
return hash
}
// Decode extract data from the binary representation of a hash.
func (h *Hash) Decode(data []byte, cfg *DecodingCfg) error {
if len(data) < 5 {
return ErrInvalidHash
}
// First block
header24 := int(data[0]) | int(data[1])<<8 | int(data[2])<<16
h.LDC = float64(header24&63) / 63.0
h.PDC = float64((header24>>6)&63)/31.5 - 1.0
h.QDC = float64((header24>>12)&63)/31.5 - 1.0
h.LScale = float64((header24>>18)&31) / 31.0
h.HasAlpha = (header24 >> 23) != 0
// Second block
header16 := int(data[3]) | int(data[4])<<8
h.PScale = float64((header16>>3)&63) / 63.0
h.QScale = float64((header16>>9)&63) / 63.0
h.IsLandscape = (header16 >> 15) != 0
h.LCount = int(header16 & 7)
if h.IsLandscape {
if h.HasAlpha {
h.Lx = 5
} else {
h.Lx = 7
}
h.Ly = imax(3, h.LCount)
} else {
h.Lx = imax(3, h.LCount)
if h.HasAlpha {
h.Ly = 5
} else {
h.Ly = 7
}
}
// Alpha data
h.ADC = 1.0
h.AScale = 0.0
if h.HasAlpha {
if len(data) < 6 {
return ErrInvalidHash
}
h.ADC = float64(data[5]&15) / 15.0
h.AScale = float64(data[5]>>4) / 15.0
}
// DC coefficients
start := 5
if h.HasAlpha {
start = 6
}
idx := 0
var err error
decodeChannel := func(nx, ny int, scale float64) (ac []float64) {
for cy := 0; cy < ny; cy++ {
var cx int
if cy == 0 {
cx = 1
}
for ; cx*ny < nx*(ny-cy); cx++ {
hidx := start + (idx / 2)
if hidx >= len(data) {
err = ErrInvalidHash
return nil
}
f := (float64((data[hidx]>>((idx&1)*4))&15)/7.5 - 1.0) * scale
ac = append(ac, f)
idx++
}
}
return
}
// Note the multiplication by a constant factor to increase saturation
// since quantization tend to produce dull images.
h.LAC = decodeChannel(h.Lx, h.Ly, h.LScale)
h.PAC = decodeChannel(3, 3, h.PScale*cfg.SaturationBoost)
h.QAC = decodeChannel(3, 3, h.QScale*cfg.SaturationBoost)
if h.HasAlpha {
h.AAC = decodeChannel(5, 5, h.AScale)
}
return err
}
// Size return the width and height of the image associated with a hash
// according to a specific base size.
func (hash *Hash) Size(baseSize int) (w int, h int) {
ratio := float64(hash.Lx) / float64(hash.Ly)
if ratio > 1.0 {
w = baseSize
h = iround(float64(baseSize) / ratio)
} else {
w = iround(float64(baseSize) * ratio)
h = baseSize
}
return
}
func (hash *Hash) coefficients(x, y, w, h int) (fx []float64, fy []float64) {
xf := float64(x)
yf := float64(y)
wf, hf := float64(w), float64(h)
n := 3
if hash.HasAlpha {
n = 5
}
n = imax(hash.Lx, n)
fx = make([]float64, n)
for cx := 0; cx < n; cx++ {
fx[cx] = math.Cos(math.Pi / wf * (xf + 0.5) * float64(cx))
}
n = 3
if hash.HasAlpha {
n = 5
}
n = imax(hash.Ly, n)
fy = make([]float64, n)
for cy := 0; cy < n; cy++ {
fy[cy] = math.Cos(math.Pi / hf * (yf + 0.5) * float64(cy))
}
return
}