forked from evanoberholster/imagemeta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dct.go
289 lines (255 loc) · 10.5 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
package transforms32
// DCT2DHash64 function returns a result of DCT2D by using the seperable property.
// DCT type II, unscaled. Algorithm by Byeong Gi Lee, 1984.
// Cusstom built for Hash64. Returns flattened pixels
func DCT2DHash64(input []float32) [64]float32 {
if len(input) != 64*64 {
panic("Incorrect forward transform size")
}
if FlagUseASM {
return asmDCT2DHash64(input)
}
var flattens [64]float32
for i := 0; i < 64; i++ { // height
ForwardDCT64((input)[i*64 : 64*i+64])
}
var row [64]float32
for i := 0; i < 8; i++ { // width
for j := 0; j < 64; j++ {
row[j] = (input)[64*j+i]
}
ForwardDCT64(row[:])
for j := 0; j < 8; j++ {
flattens[8*j+i] = row[j]
}
}
return flattens
}
// DCT2DHash256 function returns a result of DCT2D by using the seperable property.
// DCT type II, unscaled. Algorithm by Byeong Gi Lee, 1984.
// Cusstom built for Hash256. Returns flattened pixels
func DCT2DHash256(input *[]float32) [256]float32 {
var flattens [256]float32
if len(*input) != 256*256 {
panic("Incorrect forward transform size")
}
for i := 0; i < 256; i++ { // height
ForwardDCT256((*input)[i*256 : 256*i+256])
}
var row [256]float32
for i := 0; i < 16; i++ { // width
for j := 0; j < 256; j++ {
row[j] = (*input)[256*j+i]
}
ForwardDCT256(row[:])
for j := 0; j < 16; j++ {
flattens[16*j+i] = row[j]
}
}
return flattens
}
// MedianOfPixels64 function returns a median value of pixels.
// It uses quick selection algorithm.
func MedianOfPixels64(pixels []float32) float32 {
tmp := [64]float32{}
copy(tmp[:], pixels)
l := len(tmp)
pos := l / 2
return quickSelectMedian(tmp[:], 0, l-1, pos)
}
// MedianOfPixels256 function returns a median value of pixels.
// It uses quick selection algorithm.
func MedianOfPixels256(pixels []float32) float32 {
tmp := [256]float32{}
copy(tmp[:], pixels)
l := len(tmp)
pos := l / 2
return quickSelectMedian(tmp[:], 0, l-1, pos)
}
func quickSelectMedian(sequence []float32, low int, hi int, k int) float32 {
if low == hi {
return sequence[k]
}
for low < hi {
pivot := low/2 + hi/2
pivotValue := sequence[pivot]
storeIdx := low
sequence[pivot], sequence[hi] = sequence[hi], sequence[pivot]
for i := low; i < hi; i++ {
if sequence[i] < pivotValue {
sequence[storeIdx], sequence[i] = sequence[i], sequence[storeIdx]
storeIdx++
}
}
sequence[hi], sequence[storeIdx] = sequence[storeIdx], sequence[hi]
if k <= storeIdx {
hi = storeIdx
} else {
low = storeIdx + 1
}
}
if len(sequence)%2 == 0 {
return sequence[k-1]/2 + sequence[k]/2
}
return sequence[k]
}
func forwardDCT256(input []float32) {
var temp [256]float32
for i := 0; i < 128; i++ {
x, y := input[i], input[256-1-i]
temp[i] = x + y
temp[i+128] = (x - y) / float32(dct256[i])
}
forwardDCT128(temp[:128])
forwardDCT128(temp[128:])
for i := 0; i < 128-1; i++ {
input[i*2+0] = temp[i]
input[i*2+1] = temp[i+128] + temp[i+128+1]
}
input[256-2], input[256-1] = temp[128-1], temp[256-1]
}
func forwardDCT128(input []float32) {
var temp [128]float32
for i := 0; i < 64; i++ {
x, y := input[i], input[128-1-i]
temp[i] = x + y
temp[i+64] = (x - y) / float32(dct128[i])
}
forwardDCT64(temp[:64])
forwardDCT64(temp[64:])
for i := 0; i < 64-1; i++ {
input[i*2+0] = temp[i]
input[i*2+1] = temp[i+64] + temp[i+64+1]
}
input[128-2], input[128-1] = temp[64-1], temp[128-1]
}
func forwardDCT64(input []float32) {
var temp [64]float32
for i := 0; i < 32; i++ {
x, y := input[i], input[64-1-i]
temp[i] = x + y
temp[i+32] = (x - y) / dct6432[i]
}
forwardDCT32(temp[:32])
forwardDCT32(temp[32:])
for i := 0; i < 32-1; i++ {
input[i*2+0] = temp[i]
input[i*2+1] = temp[i+32] + temp[i+32+1]
}
input[62], input[63] = temp[31], temp[63]
}
func forwardDCT32(input []float32) {
var temp [32]float32
for i := 0; i < 16; i++ {
x, y := input[i], input[32-1-i]
temp[i] = x + y
temp[i+16] = (x - y) / dct3232[i]
}
forwardDCT16(temp[:16])
forwardDCT16(temp[16:])
for i := 0; i < 16-1; i++ {
input[i*2+0] = temp[i]
input[i*2+1] = temp[i+16] + temp[i+16+1]
}
input[30], input[31] = temp[15], temp[31]
}
func forwardDCT16(input []float32) {
var temp [16]float32
for i := 0; i < 8; i++ {
x, y := input[i], input[15-i]
temp[i] = x + y
temp[i+8] = (x - y) / dct1632[i]
}
forwardDCT8(temp[:8])
forwardDCT8(temp[8:])
for i := 0; i < 8-1; i++ {
input[i*2+0] = temp[i]
input[i*2+1] = temp[i+8] + temp[i+8+1]
}
input[16-2], input[16-1] = temp[8-1], temp[16-1]
}
func forwardDCT8(input []float32) {
var a, b = [4]float32{}, [4]float32{}
x0, y0 := input[0], input[7]
x1, y1 := input[1], input[6]
x2, y2 := input[2], input[5]
x3, y3 := input[3], input[4]
a[0] = x0 + y0
a[1] = x1 + y1
a[2] = x2 + y2
a[3] = x3 + y3
b[0] = (x0 - y0) / 1.9615705608064609
b[1] = (x1 - y1) / 1.6629392246050907
b[2] = (x2 - y2) / 1.1111404660392046
b[3] = (x3 - y3) / 0.3901806440322566
forwardDCT4(a[:])
forwardDCT4(b[:])
input[0] = a[0]
input[1] = b[0] + b[1]
input[2] = a[1]
input[3] = b[1] + b[2]
input[4] = a[2]
input[5] = b[2] + b[3]
input[6] = a[3]
input[7] = b[3]
}
func forwardDCT4(input []float32) {
x0, y0 := input[0], input[3]
x1, y1 := input[1], input[2]
t0 := x0 + y0
t1 := x1 + y1
t2 := (x0 - y0) / 1.8477590650225735
t3 := (x1 - y1) / 0.7653668647301797
x, y := t0, t1
t0 += t1
t1 = (x - y) / 1.4142135623730951
x, y = t2, t3
t2 += t3
t3 = (x - y) / 1.4142135623730951
input[0] = t0
input[1] = t2 + t3
input[2] = t1
input[3] = t3
}
// Static DCT Tables
var (
dct256 = [128]float64{
1.9999623505652022, 1.9996611635916468, 1.9990588350021863, 1.9981554555052907, 1.9969511611465895, 1.9954461332883833, 1.9936405985823316, 1.9915348289353196,
1.9891291414685108, 1.986423898469589, 1.983419507338199, 1.9801164205245942, 1.976515135461499, 1.9726161944891973, 1.968420184773858, 1.9639277382191105,
1.9591395313708813, 1.9540562853155086, 1.9486787655711517, 1.9430077819725036, 1.9370441885488345, 1.9307888833953788, 1.9242428085380832, 1.917406949791743,
1.9102823366115416, 1.9028700419380167, 1.8951711820354824, 1.8871869163239208, 1.8789184472043798, 1.8703670198778952, 1.8615339221579674, 1.8524204842766228,
1.843028078684084, 1.8333581198420854, 1.8234120640108598, 1.8131914090298307, 1.802697694092044, 1.7919324995123704, 1.7808974464895158, 1.7695941968618756,
1.758024452857267, 1.7461899568365802, 1.7340924910313853, 1.7217338772755346, 1.709115976730801, 1.6962406896065945, 1.6831099548737969, 1.6697257499727602,
1.6560900905155114, 1.6422050299822093, 1.6280726594118968, 1.6136951070875987, 1.59907453821581, 1.5842131546004248, 1.5691131943111505, 1.5537769313464649,
1.5382066752911592, 1.5224047709685236, 1.506373598087225, 1.490115570882932, 1.4736331377547398, 1.4569287808964504, 1.4400050159227635, 1.4228643914904329,
1.4055094889144508, 1.387942921779308, 1.370167335545401, 1.352185407150632, 1.3339998446072752, 1.3156133865941575, 1.297028802044225, 1.2782488897275517,
1.2592764778298542, 1.2401144235265784, 1.220765612552619, 1.201232958767738, 1.1815194037177486, 1.1616279161915293, 1.1415614917739347, 1.121323152394672,
1.1009159458732098, 1.080342945459786, 1.0596072493725897, 1.0387119803311793, 1.0176602850862142, 0.9964553339455638, 0.9751003202968722, 0.9535984601266445,
0.9319529915359323, 0.9101671742526877, 0.8882442891408585, 0.866187637706304, 0.8440005415995996, 0.8216863421158078, 0.7992483996912936, 0.7766900933976526,
0.7540148204328366, 0.7312259956095479, 0.708327050840981, 0.6853214346239888, 0.6622126115197529, 0.6390040616320315, 0.61569928008307, 0.5923017764872479,
0.5688150744225436, 0.545242710899898, 0.5215882358305511, 0.4978552114914405, 0.4740472119887347, 0.45016782271958555, 0.4262206398321827, 0.40220926968418386,
0.37813732829961255, 0.3540084408242977, 0.3298262409799401, 0.3055943705168868, 0.2813164786656985, 0.25699622158758645, 0.23263726182380973, 0.20824326774410945,
0.1838179129942654, 0.15936487594286025, 0.1348878391273282, 0.11039048869938006, 0.08587651386988192, 0.06134960635327317, 0.03681345981160964, 0.012271769298309032}
dct128 = [64]float64{
1.9998494036782892, 1.9986447691766989, 1.9962362258002984, 1.992625224365556, 1.9878139400047121, 1.98180527085556, 1.9746028363157169, 1.9662109748624328,
1.9566347414392553, 1.9458799044111204, 1.9339529420897041, 1.9208610388311316, 1.9066120807083875, 1.8912146507610426, 1.87467802382515, 1.8570121609464312,
1.8382277033801153, 1.818335966181045, 1.7973489313879076, 1.7752792408057079, 1.7521401883908132, 1.7279457122431734, 1.7027103862105304, 1.6764494111096762,
1.6491786055700506, 1.6209143965051895, 1.5916738092177671, 1.561474457144189, 1.530334531244918, 1.4982727890469187, 1.4653085433448259, 1.4314616505676372,
1.3967524988179458, 1.3612019955909063, 1.3248315551803436, 1.287663085779583, 1.249718976284773, 1.211022082808651, 1.1715957149128777, 1.1314636215672265,
1.0906499768440931, 1.049179365356938, 1.0070767674514352, 0.9643675441582458, 0.92107742191648, 0.8772324770770554, 0.8328591201952746, 0.7879840801220962,
0.7426343879036752, 0.696837360498869, 0.650620584324526, 0.6040118986384564, 0.5570393787701061, 0.5097313192090293, 0.4621162165613425, 0.4142227523844371,
0.36607977591028207, 0.3177162866677228, 0.26916141701425245, 0.22044441458776637, 0.17159462468887976, 0.12264147260441731, 0.07361444588271798, 0.02454307657143989}
dct6432 = [32]float32{
1.9993976373924083, 1.9945809133573804, 1.9849590691974202, 1.9705552847778824, 1.9514042600770571, 1.9275521315908797, 1.8990563611860733, 1.8659855976694777,
1.8284195114070614, 1.7864486023910306, 1.7401739822174227, 1.6897071304994142, 1.6351696263031674, 1.5766928552532127, 1.5144176930129691, 1.448494165902934,
1.3790810894741339, 1.3063456859075537, 1.2304631811612539, 1.151616382835691, 1.0699952397741948, 0.9857963844595683, 0.8992226593092132, 0.8104826280099796,
0.7197900730699766, 0.627363480797783, 0.5334255149497968, 0.43820248031373954, 0.3419237775206027, 0.24482135039843256, 0.1471291271993349, 0.049082457045824535,
}
dct3232 = [16]float32{
1.9975909124103448, 1.978353019929562, 1.9400625063890882, 1.8830881303660416, 1.8079785862468867, 1.7154572200005442, 1.6064150629612899, 1.4819022507099182,
1.3431179096940369, 1.191398608984867, 1.0282054883864435, 0.8551101868605644, 0.6737797067844401, 0.48596035980652796, 0.2934609489107235, 0.09813534865483627,
}
dct1632 = [8]float32{
1.9903694533443936, 1.9138806714644176, 1.76384252869671, 1.546020906725474, 1.2687865683272912, 0.9427934736519956, 0.5805693545089246, 0.19603428065912154,
}
)