]> mj.ucw.cz Git - libucw.git/blob - images/image-test.c
bugfix
[libucw.git] / images / image-test.c
1 #define LOCAL_DEBUG
2
3 #include "sherlock/sherlock.h"
4 #include "lib/fastbuf.h"
5 #include "images/images.h"
6 #include "images/image-search.h"
7 #include "sherlock/index.h"
8 #include "lib/mempool.h"
9 #include "sherlock/object.h"
10 #include "sherlock/lizard-fb.h"
11 #include <fcntl.h>
12 #include <stdio.h>
13
14 #define BEST_CNT 20
15
16 int
17 main(int argc, char **argv)
18 {
19   struct image_vector query;
20   if (argc != IMAGE_VEC_K + 1)
21     die("Invalid number of arguments");
22
23   for (uns i = 0; i < IMAGE_VEC_K; i++)
24     {
25       uns v;
26       if (sscanf(argv[i + 1], "%d", &v) != 1)
27         die("Invalid numeric format");
28       query.f[i] = v;
29     }
30   
31   
32   struct image_search is;
33   oid_t best[BEST_CNT];
34   uns dist[BEST_CNT];
35   uns cardpos[BEST_CNT];
36   uns best_n = 0;
37   
38   image_tree_init();
39   log(L_INFO, "Executing query (%s)", stk_print_image_vector(&query));
40   image_search_init(&is, &image_tree, &query, IMAGE_SEARCH_DIST_UNLIMITED);
41   for (uns i = 0; i < BEST_CNT; i++)
42     {
43       if (!image_search_next(&is, best + i, dist + i))
44         {
45           log(L_INFO, "No more images");
46           break;
47         }
48       log(L_INFO, "*** Found %d. best image with oid=%d", i + 1, best[i]);
49       best_n++;
50     }
51   image_search_done(&is);
52   image_tree_done();
53
54   log(L_INFO, "Resolving URLs");
55   struct mempool *pool = mp_new(1 << 16);
56   struct buck2obj_buf *bob = buck2obj_alloc();
57   struct fastbuf *fb = bopen("index/card-attrs", O_RDONLY, 1 << 10);
58   for (uns i = 0; i < best_n; i++)
59     {
60       bsetpos(fb, best[i] * sizeof(struct card_attr));
61       struct card_attr ca;
62       bread(fb, &ca, sizeof(ca));
63       cardpos[i] = ca.card;
64     }
65   bclose(fb);
66   fb = bopen("index/cards", O_RDONLY, 1 << 14);
67   for (uns i = 0; i < best_n; i++)
68     {
69       bsetpos(fb, (sh_off_t)cardpos[i] << CARD_POS_SHIFT);
70       uns buck_len = bgetl(fb) - (LIZARD_COMPRESS_HEADER - 1);
71       uns buck_type = bgetc(fb) + BUCKET_TYPE_PLAIN;
72       mp_flush(pool);
73       struct odes *obj = obj_read_bucket(bob, pool, buck_type, buck_len, fb, NULL);
74
75       printf("%2d. match: dist=%-8d oid=%-8d url=%s\n", i + 1, dist[i], best[i],
76           obj_find_aval(obj_find_attr(obj, 'U' + OBJ_ATTR_SON)->son, 'U'));
77     }
78   bclose(fb);
79   buck2obj_free(bob);
80   mp_delete(pool);
81      
82   return 0;
83 }
84