-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (54 loc) · 1.37 KB
/
index.js
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
'use strict'
const execa = require('execa')
exports.compute = async (input) => {
const cmd = await execa(`convert`, [
'-quiet',
'-moments',
input,
'json:-'
])
// imagemagick uses some non-standard json...
const stdout = cmd.stdout
.replace(/␍/gm, '')
.trim()
.replace(/\b(nan)\b/g, 'null')
const result = JSON.parse(stdout)
const { image } = (Array.isArray(result) ? result[0] : result)
const channels = image.channelPerceptualHash
const values = []
.concat(extractChannel(channels.Channel0 || channels.redHue))
.concat(extractChannel(channels.Channel1 || channels.greenChroma))
.concat(extractChannel(channels.Channel2 || channels.blueLuma))
return values
}
exports.compare = async (hash1, hash2) => {
const ssd = hash1.reduce((acc, value1, index) => {
const value2 = hash2[index]
const diff = (value1 - value2)
return acc + diff * diff
}, 0)
return Math.sqrt(ssd)
}
function extractChannel (channel) {
const results = [
channel.PH1[0],
channel.PH1[1],
channel.PH2[0],
channel.PH2[1],
channel.PH3[0],
channel.PH3[1],
channel.PH4[0],
channel.PH4[1],
channel.PH5[0],
channel.PH5[1],
channel.PH6[0],
channel.PH6[1],
channel.PH7[0],
channel.PH7[1]
]
if (typeof results[0] === 'string') {
return results.map(parseFloat)
} else {
return results
}
}