-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (137 loc) · 3.59 KB
/
Copy pathindex.js
File metadata and controls
143 lines (137 loc) · 3.59 KB
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
const addon = require('./index.node');
/**
* Generate a PDQ perceptual hash from a binary buffer.
*
* This function accepts a {@link Buffer} and returns a hexadecimal
* string representation of the PDQ hash.
*
* @param input - A Buffer containing the binary data to hash.
* @returns A hex-encoded PDQ hash string, or `null` if a hash could not be generated.
*
* @throws {TypeError}
* Thrown if `input` is not a Buffer.
*
* @example
* ```ts
* import { pdqhash } from "pdqhash-node";
*
* const buf = fs.readFileSync("image.jpg");
* const hash = pdqhash(buf);
*
* if (hash) {
* console.log(hash);
* }
* ```
*/
function pdqhash(input) {
if (!Buffer.isBuffer(input)) {
throw new TypeError('pdqhash(input): input must be a Buffer');
}
const res = addon.generatePdqFromBuffer(input);
if (!res) return null;
return Buffer.from(res.hash).toString('hex');
}
/**
* Generate a PDQ perceptual hash from a binary buffer.
*
* This function accepts a {@link Buffer} and returns the raw PDQ
* hash as a {@link Buffer}.
*
* @param input - A Buffer containing the binary data to hash.
* @returns A Buffer containing the PDQ hash, or `null` if a hash could not be generated.
*
* @throws {TypeError}
* Thrown if `input` is not a Buffer.
*
* @example
* ```ts
* import { pdqhashBuffer } from "pdqhash-node";
*
* const buf = fs.readFileSync("image.jpg");
* const hashBuf = pdqhashBuffer(buf);
*
* if (hashBuf) {
* console.log(hashBuf.toString('hex'));
* }
* ```
*/
function pdqhashBuffer(input) {
if (!Buffer.isBuffer(input)) {
throw new TypeError('pdqhashBuffer(input): input must be a Buffer');
}
const res = addon.generatePdqFromBuffer(input);
if (!res) return null;
return Buffer.from(res.hash);
}
/**
* Generate a PDQ hash with quality metadata from a binary buffer.
*
* Returns an object containing the PDQ hash in hex string form and
* the computed quality score.
*
* @param input - A Buffer containing the binary data to hash.
* @returns An object `{ hash: string, quality: number }`, or `null` if a hash could not be generated.
*
* @throws {TypeError}
* Thrown if `input` is not a Buffer.
*
* @example
* ```ts
* import { pdqhashWithQuality } from "pdqhash-node";
*
* const buf = fs.readFileSync("image.jpg");
* const result = pdqhashWithQuality(buf);
*
* if (result) {
* console.log(result.hash, result.quality);
* }
* ```
*/
function pdqhashWithQuality(input) {
if (!Buffer.isBuffer(input)) {
throw new TypeError('pdqhashWithQuality(input): input must be a Buffer');
}
const res = addon.generatePdqFromBuffer(input);
if (!res) return null;
return {
hash: Buffer.from(res.hash).toString('hex'),
quality: res.quality,
};
}
/**
* Compute Hamming distance between two PDQ hashes.
*
* Accepts two PDQ hash Buffers and returns their Hamming distance
* as a number.
*
* @param a - First PDQ hash as a Buffer.
* @param b - Second PDQ hash as a Buffer.
* @returns The Hamming distance between `a` and `b`.
*
* @throws {TypeError}
* Thrown if either argument is not a Buffer.
*
* @example
* ```ts
* import { pdqhashBuffer, hammingDistance } from "pdqhash-node";
*
* const a = pdqhashBuffer(fs.readFileSync("image1.jpg"));
* const b = pdqhashBuffer(fs.readFileSync("image2.jpg"));
*
* if (a && b) {
* console.log(hammingDistance(a, b));
* }
* ```
*/
function hammingDistance(a, b) {
if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
throw new TypeError('hammingDistance(a, b): arguments must be Buffers');
}
return addon.hammingDistance(a, b);
}
module.exports = {
pdqhash,
pdqhashBuffer,
pdqhashWithQuality,
hammingDistance
};