3 #include "sherlock/sherlock.h"
4 #include "lib/fastbuf.h"
5 #include "images/images.h"
6 #include "images/image-sig.h"
7 #include "images/kd-tree.h"
8 #include "sherlock/index.h"
9 #include "lib/mempool.h"
10 #include "sherlock/object.h"
11 #include "sherlock/lizard-fb.h"
15 #include "sherlock/sherlock.h"
16 #include "lib/fastbuf.h"
17 #include "images/images.h"
18 #include "sherlock/index.h"
26 struct image_tree image_tree;
31 DBG("Initializing image search structures");
32 struct fastbuf *fb = bopen("index/image-tree", O_RDONLY, 1 << 16); /* FIXME: filename hack */
33 image_tree.count = bgetl(fb);
34 image_tree.depth = bgetl(fb);
35 ASSERT(image_tree.count < 0x80000000 && image_tree.depth > 0 && image_tree.depth < 30);
36 image_tree.nodes = xmalloc((1 << image_tree.depth) * sizeof(struct image_node));
37 image_tree.leaves = xmalloc(image_tree.count * sizeof(struct image_leaf));
38 bread(fb, &image_tree.bbox, sizeof(struct image_bbox));
39 bread(fb, image_tree.nodes + 1, ((1 << image_tree.depth) - 1) * sizeof(struct image_node));
40 bread(fb, image_tree.leaves, image_tree.count * sizeof(struct image_leaf));
41 DBG("Search tree with depth %d and %d leaves loaded", image_tree.depth, image_tree.count);
48 DBG("Freeing image search structures");
49 xfree(image_tree.nodes);
50 xfree(image_tree.leaves);
54 main(int argc, char **argv)
56 struct image_vector query;
57 if (argc != IMAGE_VEC_K + 1)
58 die("Invalid number of arguments");
60 for (uns i = 0; i < IMAGE_VEC_K; i++)
63 if (sscanf(argv[i + 1], "%d", &v) != 1)
64 die("Invalid numeric format");
69 struct image_search is;
72 uns cardpos[BEST_CNT];
76 log(L_INFO, "Executing query (%s)", stk_print_image_vector(&query));
77 image_search_init(&is, &image_tree, &query, IMAGE_SEARCH_DIST_UNLIMITED);
78 for (uns i = 0; i < BEST_CNT; i++)
80 if (!image_search_next(&is, best + i, dist + i))
82 log(L_INFO, "No more images");
85 DBG("*** Found %d. best image with oid=%d", i + 1, best[i]);
88 image_search_done(&is);
91 log(L_INFO, "Resolving URLs");
92 struct mempool *pool = mp_new(1 << 16);
93 struct buck2obj_buf *bob = buck2obj_alloc();
94 struct fastbuf *fb = bopen("index/card-attrs", O_RDONLY, 1 << 10);
95 for (uns i = 0; i < best_n; i++)
97 bsetpos(fb, best[i] * sizeof(struct card_attr));
99 bread(fb, &ca, sizeof(ca));
100 cardpos[i] = ca.card;
103 fb = bopen("index/cards", O_RDONLY, 1 << 14);
104 for (uns i = 0; i < best_n; i++)
106 bsetpos(fb, (sh_off_t)cardpos[i] << CARD_POS_SHIFT);
107 uns buck_len = bgetl(fb) - (LIZARD_COMPRESS_HEADER - 1);
108 uns buck_type = bgetc(fb) + BUCKET_TYPE_PLAIN;
110 struct odes *obj = obj_read_bucket(bob, pool, buck_type, buck_len, fb, NULL);
112 printf("%2d. match: dist=%-8d oid=%-8d url=%s\n", i + 1, dist[i], best[i],
113 obj_find_aval(obj_find_attr(obj, 'U' + OBJ_ATTR_SON)->son, 'U'));