-
Notifications
You must be signed in to change notification settings - Fork 8
/
pHash.xml
398 lines (371 loc) · 8.12 KB
/
pHash.xml
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?xml version="1.0"?>
<extension name="pHash" version="1.1.2">
<summary>pHash</summary>
<license>PHP</license>
<maintainers>
<maintainer>
<name>Evan Klinger</name>
<email>[email protected]</email>
<role>lead</role>
</maintainer>
</maintainers>
<release>
<version>0.9.2</version>
<date>2013-05-14</date>
<state>beta</state>
</release>
<deps language="cpp" platform="all">
<with defaults='/usr:/usr/local' testfile='include/pHash.h'>
<header name="pHash.h"/>
<header name="audiophash.h"/>
<lib name="pHash" platform="all" function="ph_texthash"/>
</with>
</deps>
<code role="code" position="top">
struct ph_mh_image_hash
{
uint8_t *hash;
int len;
};
struct ph_audio_hash
{
uint32_t *hash;
int len;
};
struct ph_video_hash
{
ulong64 *hash;
int len;
};
struct ph_text_hash
{
TxtHashPoint *p;
int count;
};
</code>
<resource name="ph_video_hash" payload="ph_video_hash" alloc="no">
<description>
A ulong64 resource
</description>
<destruct>
if(resource)
{
free(resource->hash);
free(resource);
}
</destruct>
</resource>
<resource name="ph_image_hash" payload="ulong64" alloc="no">
<description>
A ulong64 resource
</description>
<destruct>
if(resource)
free(resource);
</destruct>
</resource>
<resource name="ph_audio_hash" payload="ph_audio_hash" alloc="no">
<description>
A uint32_t resource
</description>
<destruct>
if(resource)
{
free(resource->hash);
free(resource);
}
</destruct>
</resource>
<resource name="ph_mh_image_hash" payload="ph_mh_image_hash" alloc="no">
<description>
A uint8_t resource
</description>
<destruct>
if(resource)
{
free(resource->hash);
free(resource);
}
</destruct>
</resource>
<resource name="ph_txt_hash" payload="ph_text_hash" alloc="no">
<description>
A TxtHashPoint resource
</description>
<destruct>
if(resource)
{
free(resource->p);
free(resource);
}
</destruct>
</resource>
<function name="ph_dct_videohash" if="HAVE_VIDEO_HASH">
<proto>resource ph_video_hash ph_dct_videohash(string file)</proto>
<summary>pHash DCT video hash</summary>
<description>
Perceptual video hash based on DCT.
</description>
<code>
<![CDATA[
int len;
ulong64 *video_hash = ph_dct_videohash(file, len);
if(video_hash)
{
ph_video_hash *p = (ph_video_hash *)malloc(sizeof(ph_video_hash));
p->hash = video_hash;
p->len = len;
return_res = p;
}
else
RETURN_FALSE;
]]>
</code>
</function>
<function name="ph_dct_imagehash" if="HAVE_IMAGE_HASH">
<proto>resource ph_image_hash ph_dct_imagehash(string file)</proto>
<summary>pHash DCT image hash</summary>
<description>
Perceptual image hash based on DCT.
</description>
<code>
<![CDATA[
ulong64 *hash = (ulong64 *)malloc(sizeof(ulong64));
int ret = ph_dct_imagehash(file, *hash);
if(ret != 0)
{
free(hash);
RETURN_FALSE;
}
else
return_res = hash;
]]>
</code>
</function>
<function name="ph_mh_imagehash" if="HAVE_IMAGE_HASH">
<proto>resource ph_mh_image_hash ph_mh_imagehash(string file, float alpha=2.0, float level=1.0)</proto>
<summary>Fixed Length MH image hash</summary>
<description>
Marr-Hildreth wavelet based image hash.
</description>
<code>
<![CDATA[
int num = 0;
uint8_t *hash = ph_mh_imagehash(file, num, alpha, level);
if (hash)
{
ph_mh_image_hash *h = (ph_mh_image_hash *)malloc(sizeof(ph_mh_image_hash));
h->hash = hash;
h->len = num;
return_res = h;
}
else
RETURN_FALSE;
]]>
</code>
</function>
<function name="ph_texthash">
<proto>resource ph_txt_hash ph_texthash(string file)</proto>
<summary>pHash cyclic text hash</summary>
<description>
Perceptual text hash based on cyclic polynomials.
</description>
<code>
<![CDATA[
int num;
TxtHashPoint *txtHash = ph_texthash(file, &num);
if(txtHash)
{
ph_text_hash *h = (ph_text_hash *)malloc(sizeof(ph_text_hash));
h->p = txtHash;
h->count = num;
return_res = h;
}
else
RETURN_FALSE;
]]>
</code>
</function>
<function name="ph_audiohash" if="HAVE_AUDIO_HASH">
<proto>resource ph_audio_hash ph_audiohash(string file, int sample_rate=5512, int channels=1)</proto>
<summary>pHash audio hash</summary>
<description>
Perceptual audio hash based on bark scale.
</description>
<code>
<![CDATA[
int n;
float *audiobuf = ph_readaudio(file, sample_rate, channels, NULL, n);
if(audiobuf)
{
int nb_frames;
uint32_t *hash = ph_audiohash(audiobuf, n, sample_rate, nb_frames);
free(audiobuf);
if(hash)
{
ph_audio_hash *h = (ph_audio_hash *)malloc(sizeof(ph_audio_hash));
h->hash = hash;
h->len = nb_frames;
return_res = h;
}
else
RETURN_FALSE;
}
else
RETURN_FALSE;
]]>
</code>
</function>
<function name="ph_image_dist" if="HAVE_IMAGE_HASH">
<proto>float ph_image_dist(resource ph_image_hash h1,resource ph_image_hash h2)</proto>
<summary>pHash image distance.</summary>
<description>
Calculate distance between two images.
</description>
<code>
<![CDATA[
if(h1 && h2)
{
int dist = ph_hamming_distance(*h1, *h2);
RETURN_DOUBLE(dist);
}
else
RETURN_DOUBLE(-1);
]]>
</code>
</function>
<function name="ph_dct_imagehash_to_array" if="HAVE_IMAGE_HASH">
<proto>array ph_dct_imagehash_to_array(resource ph_image_hash h)</proto>
<summary>Conversion of DCT pHash image hash to 8-bytes array.</summary>
<description>
Returns DCT pHash image hash represented as array of 8 bytes.
</description>
<code>
<![CDATA[
if (h)
{
array_init(return_value);
uint8_t *p = (uint8_t*) h;
for (int i = 0; i < sizeof(ulong64) / sizeof(uint8_t); i++)
{
add_next_index_long(return_value, *(p + i));
}
}
else
{
RETURN_FALSE;
}
]]>
</code>
</function>
<function name="ph_mh_imagehash_to_array" if="HAVE_IMAGE_HASH">
<proto>array ph_mh_imagehash_to_array(resource ph_mh_image_hash h)</proto>
<summary>Conversion of fixed Length MH image hash to 72-bytes array.</summary>
<description>
Returns fixed Length MH image hash represented as array of 72 bytes.
</description>
<code>
<![CDATA[
if (h)
{
array_init(return_value);
for (int i = 0; i < h->len; i++)
{
add_next_index_long(return_value, *(h->hash + i));
}
}
else
{
RETURN_FALSE;
}
]]>
</code>
</function>
<function name="ph_video_dist" if="HAVE_VIDEO_HASH">
<proto>float ph_video_dist(resource ph_video_hash h1,resource ph_video_hash h2, int thresh=21)</proto>
<summary>pHash video distance.</summary>
<description>
Calculate distance between two videos.
</description>
<code>
<![CDATA[
if(h1 && h2)
{
double sim = ph_dct_videohash_dist(h1->hash, h1->len, h2->hash, h2->len, thresh);
RETURN_DOUBLE(sim);
}
else
RETURN_DOUBLE(-1);
]]>
</code>
</function>
<function name="ph_audio_dist" if="HAVE_AUDIO_HASH">
<proto>float ph_audio_dist(resource ph_audio_hash h1,resource ph_audio_hash h2,
int block_size=256, float thresh=0.30)</proto>
<summary>pHash audio distance.</summary>
<description>
Calculate distance between two audio files.
</description>
<code>
<![CDATA[
if(h1 && h2)
{
int Nc;
double *cs = ph_audio_distance_ber(h1->hash, h1->len, h2->hash, h2->len,
thresh, block_size, Nc);
if(cs)
{
double max_cs = 0.0;
for (int i = 0; i < Nc; ++i)
{
if (cs[i] > max_cs)
{
max_cs = cs[i];
}
}
free(cs);
RETURN_DOUBLE(max_cs);
}
else
RETURN_DOUBLE(-1);
}
else
RETURN_DOUBLE(-1);
]]>
</code>
</function>
<function name="ph_compare_text_hashes">
<proto>array ph_compare_text_hashes(resource ph_txt_hash h1,resource ph_txt_hash h2)</proto>
<summary>pHash text distance.</summary>
<description>
Calculate distance between two text hashes.
</description>
<code>
<![CDATA[
if(h1 && h2)
{
int count = 0;
TxtMatch *m = ph_compare_text_hashes(h1->p, h1->count, h2->p, h2->count, &count);
if(m)
{
for(int i = 0; i < count; ++i)
{
zval *array;
MAKE_STD_ZVAL(array);
array_init(array);
add_assoc_long(array, "begin", m[i].first_index);
add_assoc_long(array, "end", m[i].second_index);
add_assoc_long(array, "length", m[i].length);
add_next_index_zval(return_value, array);
}
free(m);
}
else
RETURN_FALSE;
}
else
RETURN_FALSE;
]]>
</code>
</function>
</extension>