]> mj.ucw.cz Git - libucw.git/blob - images/image-idx.c
Initial testing version of image signatures... very simple yet :-)
[libucw.git] / images / image-idx.c
1 #define LOCAL_DEBUG
2
3 #include "sherlock/sherlock.h"
4 #include "lib/mempool.h"
5 #include "lib/conf.h"
6 #include "lib/fastbuf.h"
7 #include "lib/chartype.h"
8 #include "sherlock/object.h"
9 #include "lib/url.h"
10 #include "lib/unicode.h"
11 #include "sherlock/lizard-fb.h"
12 #include "sherlock/tagged-text.h"
13 #include "charset/charconv.h"
14 #include "charset/unicat.h"
15 #include "charset/fb-charconv.h"
16 #include "indexer/indexer.h"
17 #include "indexer/lexicon.h"
18 #include "indexer/params.h"
19 #include "utils/dumpconfig.h"
20 #include "lang/lang.h"
21 #include "lib/base224.h"
22 #include "lib/bbuf.h"
23
24 #include "images/images.h"
25
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <string.h>
29
30 /* This should happen in gatherer or scanner */
31 static void
32 generate_signatures(uns limit)
33 {
34   struct fastbuf *cards = index_bopen("cards", O_RDONLY);
35   struct fastbuf *card_attrs = index_bopen("card-attrs", O_RDONLY);
36   struct fastbuf *signatures = index_bopen("image-sig", O_CREAT | O_WRONLY | O_TRUNC);
37   struct card_attr ca;
38   struct image_signature sig;
39   struct mempool *pool = mp_new(1 << 16);
40   struct buck2obj_buf *bob = buck2obj_alloc();
41   oid_t oid = 0;
42
43   DBG("Generating signatures");
44
45   for (; bread(card_attrs, &ca, sizeof(ca)); oid++)
46     if ((uns)((ca.type_flags >> 4) - 8) < 4)
47       {
48         bsetpos(cards, (sh_off_t)ca.card << CARD_POS_SHIFT);
49         uns buck_len = bgetl(cards)-(LIZARD_COMPRESS_HEADER-1);
50         uns buck_type = bgetc(cards) + BUCKET_TYPE_PLAIN;
51         mp_flush(pool);
52         struct odes *obj = obj_read_bucket(bob, pool, buck_type, buck_len, cards, NULL);
53         struct oattr *attr;
54         if (!obj)
55           die("Failed to read card");
56         if (attr = obj_find_attr(obj, 'N'))
57           {
58             DBG("Reading oid=%d url=%s", oid, obj_find_aval(obj_find_attr(obj, 'U' + OBJ_ATTR_SON)->son, 'U'));
59             bb_t buf;
60             uns buf_len = 0;
61             bb_init(&buf);
62             for (; attr; attr = attr->same)
63               {
64                 uns len = strlen(attr->val);
65                 bb_grow(&buf, buf_len + len);
66                 memcpy(buf.ptr + buf_len, attr->val, len);
67                 buf_len += len;
68               }
69             byte thumb[buf_len];
70             uns thumb_len = base224_decode(thumb, buf.ptr, buf_len);
71            
72             int err = compute_image_signature(thumb, thumb_len, &sig);
73             if (!err)
74               {
75                 bputl(signatures, oid);
76                 bwrite(signatures, &sig, sizeof(sig));
77                 if (!--limit)
78                   break;
79               }
80             else
81               DBG("Cannot create signature, error=%d", err);
82
83             bb_done(&buf);
84           }
85       }
86
87   buck2obj_free(bob);
88   mp_delete(pool);
89   bclose(cards);
90   bclose(card_attrs);
91   bclose(signatures);
92 }
93
94 static char *shortopts = CF_SHORT_OPTS "";
95 static struct option longopts[] =
96 {
97   CF_LONG_OPTS
98   { NULL, 0, 0, 0 }
99 };
100
101 static char *help = "\
102 Usage: image-indexer [<options>]\n\
103 \n\
104 Options:\n" CF_USAGE;
105
106 static void NONRET
107 usage(byte *msg)
108 {
109   if (msg)
110   {
111     fputs(msg, stderr);
112     fputc('\n', stderr);
113   }
114   fputs(help, stderr);
115   exit(1);
116 }
117
118   
119 int
120 main(int argc UNUSED, char **argv)
121 {
122   int opt;
123   
124   log_init(argv[0]);
125   while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
126     switch (opt)
127     {
128       default:
129       usage("Invalid option");
130     }
131   if (optind != argc)
132     usage("Invalid usage");
133
134   generate_signatures(~0U);
135   
136   return 0;
137 }