From 609ba2faaac436fc7137d56665af4215d2336cca Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Thu, 3 Apr 2014 11:12:56 +1100 Subject: [PATCH] * db_search --- src/Makefile | 8 +++-- src/bitmap.h | 1 + src/database.c | 32 ------------------- src/database.h | 17 ++++++++++ src/main.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 34 deletions(-) create mode 100644 src/main.c diff --git a/src/Makefile b/src/Makefile index 355ed17..9abe92a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,9 +1,13 @@ CFLAGS=-Wall -Wextra -O0 -g -ggdb -pedantic -all: *.o +all: test %.o: %.c - gcc -O0 -Wall -Wextra -pedantic -ggdb $< -o $@ + gcc $(CFLAGS) -c $< + +test: main.c database.o bitmap.o + gcc $(CFLAGS) -o $@ $^ clean: rm -f *.o + rm -f test diff --git a/src/bitmap.h b/src/bitmap.h index f7989bf..333859b 100644 --- a/src/bitmap.h +++ b/src/bitmap.h @@ -1,6 +1,7 @@ #ifndef HAS_BITMAP_H #define BITMAP_SIZE 32 +#define BITMAP_BITS 256 typedef unsigned char bitmap_t[BITMAP_SIZE]; diff --git a/src/database.c b/src/database.c index 7d4f2f5..a18c716 100644 --- a/src/database.c +++ b/src/database.c @@ -16,7 +16,6 @@ #include "main.h" #include "database.h" -#include "bitmap.h" #define DB_SEEK(db, offset) \ errno = 0; \ @@ -181,34 +180,3 @@ int db_wr_list(db_t *db, rec_t *list, size_t list_len) return processed; } - -int db_search(db_t *db, float tresh, match_t **matches) -{ - uint64_t found = 0; - block_t blk; - const int blk_size = 4096; - unsigned int i = 0; - unsigned char *p; - float diff = 0; - - *matches = NULL; - match = *matches; - memset(blk, 0x0, sizeof(block_t)); - blk.start = 1; - blk.records = blk_size; - - /* TODO: expand matches */ - - while (db_rd_blk(db, &blk) > 0) { - p = blk.data; - for (i = 0; i < blk.records; i++, p += REC_LEN) { - if (*p == '\0') continue; - diff = bitmap_compare(p + 2, sample.data + 2) / BITMAP_BITS; - if (diff > tresh) continue; - match->diff_map = diff; - } - FREE(blk.data); - } - - return found; -} diff --git a/src/database.h b/src/database.h index 433ce77..58368c2 100644 --- a/src/database.h +++ b/src/database.h @@ -16,6 +16,23 @@ typedef struct { unsigned char *data; } block_t; +/** + pos len || description + - 0 1 -- record is used + - 1 1 -- must be zero + - 2 32 -- bitmap, each 2 bytes is row of monochrome image 16x16 + - 33 1 -- level of color R__ + - 34 1 -- level of color _G_ + - 35 1 -- level of color __B + - 36 11 -- must be zero + */ + +#define OFF_USED 0 +#define OFF_BITMAP 2 +#define OFF_COLORR 33 +#define OFF_COLORG 34 +#define OFF_COLORB 35 + typedef struct { uint64_t num; unsigned char data[REC_LEN]; diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..f53b045 --- /dev/null +++ b/src/main.c @@ -0,0 +1,87 @@ +/* + * 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 "main.h" +#include "database.h" +#include "bitmap.h" + +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; + /* 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("%ld -- %f\n", blk.start + i, diff); + found++; + } + FREE(blk.data); + blk.start += blk_size; + } + + return found; +} + +int main(int argc, char **argv) +{ + db_t db; + rec_t sample; + + if (argc < 3) { + printf("Usage: test \n"); + exit(EXIT_FAILURE); + } + + memset(&db, 0x0, sizeof(db_t)); + memset(&sample, 0x0, sizeof(rec_t)); + sample.num = atoll(argv[2]); + + assert(sample.num > 0); + + if (db_open(&db, argv[1]) == -1) { + printf("%s\n", db.errstr); + exit(EXIT_FAILURE); + } + + db_search(&db, &sample, 0.15, NULL); + + db_close(&db); + + exit(EXIT_SUCCESS); +}