]> mj.ucw.cz Git - libucw.git/blob - images/image-test.c
Try to merge recent changes in v3.9 to image branch...
[libucw.git] / images / image-test.c
1 #undef LOCAL_DEBUG
2
3 #include "sherlock/sherlock.h"
4 #include "lib/fastbuf.h"
5 #include "images/images.h"
6 #include "images/image-sig.h"
7 #include "images/image-search.h"
8 #include "sherlock/index.h"
9 #include "lib/mempool.h"
10 #include "sherlock/object.h"
11 #include "sherlock/lizard-fb.h"
12 #include <fcntl.h>
13 #include <stdio.h>
14
15 #include "sherlock/sherlock.h"
16 #include "lib/fastbuf.h"
17 #include "images/images.h"
18 #include "sherlock/index.h"
19
20 #include <stdio.h>
21 #include <fcntl.h>
22
23 #define BEST_CNT 30
24
25 struct image_tree image_tree;
26
27 static void
28 image_tree_init(void)
29 {
30   DBG("Initializing image search structures");
31   struct fastbuf *fb = bopen("index/image-tree", O_RDONLY, 1 << 16); /* FIXME: filename hack */
32   image_tree.count = bgetl(fb);
33   image_tree.depth = bgetl(fb);
34   ASSERT(image_tree.count < 0x80000000 && image_tree.depth > 0 && image_tree.depth < 30);
35   image_tree.nodes = xmalloc((1 << image_tree.depth) * sizeof(struct image_node));
36   image_tree.leaves = xmalloc(image_tree.count * sizeof(struct image_leaf));
37   bread(fb, &image_tree.bbox, sizeof(struct image_bbox));
38   bread(fb, image_tree.nodes + 1, ((1 << image_tree.depth) - 1) * sizeof(struct image_node));
39   bread(fb, image_tree.leaves, image_tree.count * sizeof(struct image_leaf));
40   DBG("Search tree with depth %d and %d leaves loaded", image_tree.depth, image_tree.count);
41   bclose(fb);
42 }
43
44 static void
45 image_tree_done(void)
46 {
47   DBG("Freeing image search structures");
48   xfree(image_tree.nodes);
49   xfree(image_tree.leaves);
50 }
51   
52 int
53 main(int argc, char **argv)
54 {
55   struct image_vector query;
56   if (argc != IMAGE_VEC_K + 1)
57     die("Invalid number of arguments");
58
59   for (uns i = 0; i < IMAGE_VEC_K; i++)
60     {
61       uns v;
62       if (sscanf(argv[i + 1], "%d", &v) != 1)
63         die("Invalid numeric format");
64       query.f[i] = v;
65     }
66   
67   
68   struct image_search is;
69   oid_t best[BEST_CNT];
70   uns dist[BEST_CNT];
71   uns cardpos[BEST_CNT];
72   uns best_n = 0;
73   
74   image_tree_init();
75   log(L_INFO, "Executing query (%s)", stk_print_image_vector(&query));
76   image_search_init(&is, &image_tree, &query, IMAGE_SEARCH_DIST_UNLIMITED);
77   for (uns i = 0; i < BEST_CNT; i++)
78     {
79       if (!image_search_next(&is, best + i, dist + i))
80         {
81           log(L_INFO, "No more images");
82           break;
83         }
84       DBG("*** Found %d. best image with oid=%d", i + 1, best[i]);
85       best_n++;
86     }
87   image_search_done(&is);
88   image_tree_done();
89
90   log(L_INFO, "Resolving URLs");
91   struct mempool *pool = mp_new(1 << 16);
92   struct buck2obj_buf *bob = buck2obj_alloc();
93   struct fastbuf *fb = bopen("index/card-attrs", O_RDONLY, 1 << 10);
94   for (uns i = 0; i < best_n; i++)
95     {
96       bsetpos(fb, best[i] * sizeof(struct card_attr));
97       struct card_attr ca;
98       bread(fb, &ca, sizeof(ca));
99       cardpos[i] = ca.card;
100     }
101   bclose(fb);
102   fb = bopen("index/cards", O_RDONLY, 1 << 14);
103   for (uns i = 0; i < best_n; i++)
104     {
105       bsetpos(fb, (sh_off_t)cardpos[i] << CARD_POS_SHIFT);
106       uns buck_len = bgetl(fb) - (LIZARD_COMPRESS_HEADER - 1);
107       uns buck_type = bgetc(fb) + BUCKET_TYPE_PLAIN;
108       mp_flush(pool);
109       struct odes *obj = obj_read_bucket(bob, pool, buck_type, buck_len, fb, NULL);
110
111       printf("%2d. match: dist=%-8d oid=%-8d url=%s\n", i + 1, dist[i], best[i],
112           obj_find_aval(obj_find_attr(obj, 'U' + OBJ_ATTR_SON)->son, 'U'));
113     }
114   bclose(fb);
115   buck2obj_free(bob);
116   mp_delete(pool);
117      
118   return 0;
119 }
120