forked from commonsmachinery/blockhash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockhash.c
422 lines (352 loc) · 11.4 KB
/
blockhash.c
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* Perceptual image hash calculation tool based on algorithm descibed in
* Block Mean Value Based Image Perceptual Hashing by Bian Yang, Fan Gu and Xiamu Niu
*
* Copyright 2014 Commons Machinery http://commonsmachinery.se/
* Distributed under an MIT license, please see LICENSE in the top dir.
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <math.h>
#include <wand/MagickWand.h>
// comparing functions for qsort
int cmpint(const void *pa, const void *pb)
{
int a = *(const int *) pa;
int b = *(const int *) pb;
return (a < b) ? -1 : (a > b);
}
int cmpfloat(const void *pa, const void *pb)
{
float a = *(const float *) pa;
float b = *(const float *) pb;
return (a < b) ? -1 : (a > b);
}
float median(int *data, int n)
{
int *sorted;
float result;
sorted = malloc(n * sizeof(int));
memcpy(sorted, data, n * sizeof(int));
qsort(sorted, n, sizeof(int), cmpint);
if (n % 2 == 0) {
result = (float) (sorted[n / 2] + sorted[n / 2 + 1]) / 2;
} else {
result = (float) sorted[n / 2];
}
free(sorted);
return result;
}
float medianf(float *data, int n)
{
float *sorted;
float result;
sorted = malloc(n * sizeof(float));
memcpy(sorted, data, n * sizeof(float));
qsort(sorted, n, sizeof(float), cmpfloat);
if (n % 2 == 0) {
result = (sorted[n / 2] + sorted[n / 2 + 1]) / 2;
} else {
result = sorted[n / 2];
}
free(sorted);
return result;
}
/** Compare medians across four horizontal bands
*
* Output a 1 if the block is brighter than the median.
* With images dominated by black or white, the median may
* end up being 0 or the max value, and thus having a lot
* of blocks of value equal to the median. To avoid
* generating hashes of all zeros or ones, in that case output
* 0 if the median is in the lower value space, 1 otherwise
*/
void translate_blocks_to_bits(int *blocks, int nblocks, int pixels_per_block)
{
float half_block_value;
int bandsize, i, j, v;
float m;
half_block_value = pixels_per_block * 256 * 3 / 2;
bandsize = nblocks / 4;
for (i = 0; i < 4; i++) {
m = median(&blocks[i * bandsize], bandsize);
for (j = i * bandsize; j < (i + 1) * bandsize; j++) {
v = blocks[j];
blocks[j] = v > m || (abs(v - m) < 1 && m > half_block_value);
}
}
}
void translate_blocks_to_bitsf(float *blocks, int *result, int nblocks, int pixels_per_block)
{
float half_block_value;
int bandsize, i, j;
float m, v;
half_block_value = pixels_per_block * 256 * 3 / 2;
bandsize = nblocks / 4;
for (i = 0; i < 4; i++) {
m = medianf(&blocks[i * bandsize], bandsize);
for (j = i * bandsize; j < (i + 1) * bandsize; j++) {
v = blocks[j];
result[j] = v > m || (abs(v - m) < 1 && m > half_block_value);
}
}
}
/** Convert array of bits to hexadecimal string representation.
* Hash length should be a multiple of 4.
*
* Returns: null-terminated hexadecimal string hash.
*/
char* bits_to_hexhash(int *bits, int nbits)
{
int i, j, b;
int len;
int tmp;
char *hex;
char *stmp;
len = nbits / 4;
hex = malloc(len + 1);
stmp = malloc(2);
hex[len] = '\0';
for (i = 0; i < len; i++) {
tmp = 0;
for (j = 0; j < 4; j++) {
b = i * 4 + j;
tmp = tmp | (bits[b] << 3 >> j);
}
sprintf(stmp, "%1x", tmp);
hex[i] = stmp[0];
}
free(stmp);
return hex;
}
/** Calculate perceptual hash for an RGBA image using quick method.
*
* Quick method uses rounded block sizes and is less accurate in case image
* width and height are not divisible by the number of bits.
*
* Parameters:
*
* bits - number of blocks to divide the image by horizontally and vertically.
* data - RGBA image data.
* width - image width.
* height - image height.
* hash - the resulting hash will be allocated and stored in the given array as bits.
*/
void blockhash_quick(int bits, unsigned char *data, int width, int height, int **hash)
{
int i, x, y, ix, iy;
int ii, alpha, value;
int block_width;
int block_height;
int *blocks;
float m[4];
block_width = width / bits;
block_height = height / bits;
blocks = calloc(bits * bits, sizeof(int));
for (y = 0; y < bits; y++) {
for (x = 0; x < bits; x++) {
value = 0;
for (iy = 0; iy < block_height; iy++) {
for (ix = 0; ix < block_width; ix++) {
ii = ((y * block_height + iy) * width + (x * block_width + ix)) * 4;
alpha = data[ii+3];
if (alpha == 0) {
value += 765;
} else {
value += data[ii] + data[ii+1] + data[ii+2];
}
}
}
blocks[y * bits + x] = value;
}
}
translate_blocks_to_bits(blocks, bits * bits, block_width * block_height);
*hash = blocks;
}
/** Calculate perceptual hash for an RGBA image using precise method.
*
* Precise method puts weighted pixel values to blocks according to pixel
* area falling within a given block and provides more accurate results
* in case width and height are not divisible by the number of bits.
*
* Parameters:
*
* bits - number of blocks to divide the image by horizontally and vertically.
* data - RGBA image data.
* width - image width.
* height - image height.
* hash - the resulting hash will be allocated and stored in the given array as bits.
*/
void blockhash(int bits, unsigned char *data, int width, int height, int **hash)
{
float block_width;
float block_height;
float y_frac, y_int;
float x_frac, x_int;
float x_mod, y_mod;
float weight_top, weight_bottom, weight_left, weight_right;
int block_top, block_bottom, block_left, block_right;
int i, x, y, ii, alpha;
float value;
float *blocks;
int *result;
float m[4];
if (width % bits == 0 && height % bits == 0) {
return blockhash_quick(bits, data, width, height, hash);
}
block_width = (float) width / (float) bits;
block_height = (float) height / (float) bits;
blocks = calloc(bits * bits, sizeof(float));
result = malloc(bits * bits * sizeof(int));
for (y = 0; y < height; y++) {
y_mod = fmodf(y + 1, block_height);
y_frac = modff(y_mod, &y_int);
weight_top = (1 - y_frac);
weight_bottom = y_frac;
// y_int will be 0 on bottom/right borders and on block boundaries
if (y_int > 0 || (y + 1) == height) {
block_top = block_bottom = (int) floor((float) y / block_height);
} else {
block_top = (int) floor((float) y / block_height);
block_bottom = (int) ceil((float) y / block_height);
}
for (x = 0; x < width; x++) {
x_mod = fmodf(x + 1, block_width);
x_frac = modff(x_mod, &x_int);
weight_left = (1 - x_frac);
weight_right = x_frac;
// x_int will be 0 on bottom/right borders and on block boundaries
if (x_int > 0 || (x + 1) == width) {
block_left = block_right = (int) floor((float) x / block_width);
} else {
block_left = (int) floor((float) x / block_width);
block_right = (int) ceil((float) x / block_width);
}
ii = (y * width + x) * 4;
alpha = data[ii + 3];
if (alpha == 0) {
value = 765;
} else {
value = data[ii] + data[ii + 1] + data[ii + 2];
}
// add weighted pixel value to relevant blocks
blocks[block_top * bits + block_left] += value * weight_top * weight_left;
blocks[block_top * bits + block_right] += value * weight_top * weight_right;
blocks[block_bottom * bits + block_left] += value * weight_bottom * weight_left;
blocks[block_bottom * bits + block_right] += value * weight_bottom * weight_right;
}
}
translate_blocks_to_bitsf(blocks, result, bits * bits, block_width * block_height);
*hash = result;
free(blocks);
}
int process_image(char * fn, int bits, int quick, int debug)
{
int i, j;
size_t width, height;
unsigned char *image_data;
int *hash;
MagickBooleanType status;
MagickWand *magick_wand;
magick_wand = NewMagickWand();
status = MagickReadImage(magick_wand, fn);
if (status == MagickFalse) {
printf("Error opening image file %s\n", fn);
exit(-1);
}
// Remove color profiles for interoperability with other hashing tools
MagickProfileImage(magick_wand, "*", NULL, 0);
width = MagickGetImageWidth(magick_wand);
height = MagickGetImageHeight(magick_wand);
image_data = malloc(width * height * 4);
status = MagickExportImagePixels(magick_wand, 0, 0, width, height, "RGBA", CharPixel, image_data);
if (status == MagickFalse) {
printf("Error converting image data to RGBA\n");
exit(-1);
}
hash = malloc(bits * bits * sizeof(int));
if (quick) {
blockhash_quick(bits, image_data, width, height, &hash);
} else {
blockhash(bits, image_data, width, height, &hash);
}
if (debug) {
for (i = 0; i < bits * bits; i++) {
if (i != 0 && i % bits == 0)
printf("\n");
printf("%d", hash[i]);
}
printf("\n");
}
char* hex = bits_to_hexhash(hash, bits*bits);
printf("%s %s\n", hex, fn);
free(hex);
free(hash);
free(image_data);
DestroyMagickWand(magick_wand);
}
void help() {
printf("Usage: blockhash [-h|--help] [--quick] [--bits BITS] [--debug] filenames...\n"
"\n"
"Optional arguments:\n"
"-h, --help Show this help message and exit\n"
"-q, --quick Use quick hashing method.\n"
"-b, --bits BITS Create hash of size N^2 bits. Default: 16\n"
"--debug Print hashes as 2D maps (for debugging)\n");
}
void main (int argc, char **argv) {
MagickWandGenesis();
int quick = 0;
int debug = 0;
int bits = 16;
int x;
int option_index = 0;
int c;
struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"quick", no_argument, 0, 'q'},
{"bits", required_argument, 0, 'b'},
{"debug", no_argument, 0, 'd'},
{0, 0, 0, 0}
};
if (argc < 2) {
help();
exit(0);
}
while ((c = getopt_long(argc, argv, "hqb:d",
long_options, &option_index)) != -1) {
switch (c) {
case 'h':
help();
exit(0);
break;
case 'q':
quick = 1;
break;
case 'b':
if (sscanf(optarg, "%d", &bits) != 1) {;
printf("Error: couldn't parse bits argument\n");
exit(-1);
}
if (bits % 4 != 0) {
printf("Error: bits argument should be a multiple of 4\n");
exit(-1);
}
break;
case 'd':
debug = 1;
break;
case '?':
default:
exit(-1);
}
}
if (optind < argc) {
while (optind < argc) {
process_image(argv[optind++], bits, quick, debug);
}
}
MagickWandTerminus();
exit(0);
}