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