|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "database.h"
|
|
|
|
#include "bitmap.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
void usage(int exitcode) {
|
|
|
|
printf("Usage:\n"
|
|
|
|
" util <opts>\n"
|
|
|
|
" -b <path> Path to database\n"
|
|
|
|
" -t <float> Maximum difference percent (0.0 - 1.0, default: 0.1)\n"
|
|
|
|
);
|
|
|
|
printf("\n"
|
|
|
|
" -S <num> Search for images similar ot this sample\n"
|
|
|
|
" -B <num> Show bitmap for this sample\n"
|
|
|
|
);
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int db_search(db_t *db, rec_t *sample, float tresh, match_t **matches)
|
|
|
|
{
|
|
|
|
const int blk_size = 4096;
|
|
|
|
uint64_t found = 0;
|
|
|
|
block_t blk;
|
|
|
|
unsigned int i = 0;
|
|
|
|
unsigned char *p, *t;
|
|
|
|
float diff;
|
|
|
|
|
|
|
|
assert(db != NULL);
|
|
|
|
assert(sample != NULL);
|
|
|
|
/*assert(matches != NULL);*/
|
|
|
|
|
|
|
|
memset(&blk, 0x0, sizeof(block_t));
|
|
|
|
blk.start = 1;
|
|
|
|
blk.records = blk_size;
|
|
|
|
|
|
|
|
if (db_rd_rec(db, sample) < 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!sample->data[0]) {
|
|
|
|
puts("Sample not exists");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* TODO: expand matches */
|
|
|
|
|
|
|
|
while (db_rd_blk(db, &blk) > 0) {
|
|
|
|
p = blk.data;
|
|
|
|
for (i = 0; i < blk.records; i++, p += REC_LEN) {
|
|
|
|
t = p + OFF_USED;
|
|
|
|
if (*t == 0x0) continue;
|
|
|
|
t = p + OFF_BITMAP;
|
|
|
|
diff = (float) bitmap_compare(t, sample->data + OFF_BITMAP);
|
|
|
|
diff /= BITMAP_BITS;
|
|
|
|
if (diff > tresh) continue;
|
|
|
|
printf("%lli -- %.2f\n", blk.start + i, diff * 100);
|
|
|
|
found++;
|
|
|
|
}
|
|
|
|
FREE(blk.data);
|
|
|
|
blk.start += blk_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rec_bitmap(db_t *db, rec_t *sample)
|
|
|
|
{
|
|
|
|
uint16_t row;
|
|
|
|
uint8_t i, j;
|
|
|
|
assert(db != NULL);
|
|
|
|
assert(sample != NULL);
|
|
|
|
|
|
|
|
if (db_rd_rec(db, sample) < 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!sample->data[0]) {
|
|
|
|
puts("Sample not exists");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
row = *(((uint16_t *) sample->data) + i);
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
putchar((row & 1) == 1 ? '1' : '0');
|
|
|
|
row >>= 1;
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
enum { undef, search, bitmap } mode = undef;
|
|
|
|
const char *db_path = NULL;
|
|
|
|
float tresh = 0.10;
|
|
|
|
db_t db;
|
|
|
|
rec_t sample;
|
|
|
|
char opt = '\0';
|
|
|
|
|
|
|
|
memset(&db, 0x0, sizeof(db_t));
|
|
|
|
memset(&sample, 0x0, sizeof(rec_t));
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
usage(EXIT_FAILURE);
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, "b:t:S:B:")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'b' :
|
|
|
|
db_path = optarg;
|
|
|
|
break;
|
|
|
|
case 't' :
|
|
|
|
tresh = atof(optarg);
|
|
|
|
tresh = (tresh > 0.0 && tresh < 1.0) ? tresh : 0.10;
|
|
|
|
break;
|
|
|
|
case 'B' :
|
|
|
|
mode = bitmap;
|
|
|
|
sample.num = atoll(optarg);
|
|
|
|
break;
|
|
|
|
case 'S' :
|
|
|
|
mode = search;
|
|
|
|
sample.num = atoll(optarg);
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
usage(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sample.num <= 0)
|
|
|
|
usage(EXIT_FAILURE);
|
|
|
|
|
|
|
|
if (db_open(&db, db_path) == -1) {
|
|
|
|
printf("%s\n", db.errstr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == search)
|
|
|
|
db_search(&db, &sample, tresh, NULL);
|
|
|
|
|
|
|
|
if (mode == bitmap)
|
|
|
|
rec_bitmap(&db, &sample);
|
|
|
|
|
|
|
|
db_close(&db);
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|