File: scanner_utils.hh

package info (click to toggle)
exactimage 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,040 kB
  • sloc: cpp: 35,940; ansic: 1,952; xml: 1,447; makefile: 338; perl: 138; sh: 110; python: 45; php: 37; ruby: 12
file content (308 lines) | stat: -rw-r--r-- 10,100 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (C) 2007 - 2008 Lars Kuhtz, ExactCODE GmbH Germany.
 * Copyright (C) 2010 - 2019 René Rebe, ExactCODE GmbH Germany.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * Alternatively, commercial licensing options are available from the
 * copyright holder ExactCODE GmbH Germany.
 */

#ifndef _SCANNER_UTILS_HH_
#define _SCANNER_UTILS_HH_

#if __APPLE__
#include <sys/types.h>
#endif

#define PUT_IN_TABLE(a,b,c) \
    a[b] = c; \

#define DECLARE_TABLE(a,s) \
    char a[s];

#define INIT_TABLE(a,s,v) \
    for(unsigned int i = 0; i < s; ++i) { \
        a[i] = v; \
    }

#define FLIP(a,mask) (~a)&mask

namespace BarDecode
{
    namespace {
        
    namespace debug
    {
        void print_bar_vector(const bar_vector_t& b)
        {
            std::cerr << "[ ";
            for (size_t i = 0; i < b.size(); ++i) {
                std::cerr << "(" << b[i].first << "," << b[i].second << ") ";
            }
            std::cerr << "]" << std::endl;
        }

    };
        

    namespace scanner_utilities
    {
        template<class CODE>
        u_t max_u(psize_t quiet_psize, double tolerance = 0.35)
        {
            return (quiet_psize / CODE::min_quiet_usize) * (1+tolerance);
        }

        // scanns up to c bars (possibly less)
        // parameter: tokenizer, num of bars
        // accepts a parameter for maximal size of a bar (0 for unbounded)
        // returns num of bars that where read
        // modifies the paramter vector to hold result
        
        // FIXME make use of bound --> implement support in Tokenizer!
        template<class TIT>
        unsigned add_bars(TIT& start, TIT end, bar_vector_t& v, unsigned c, psize_t bound = 0)
        {
            size_t old_size = v.size();
            v.resize(old_size + c);
            for (unsigned i = 0; i < c; ++i) {
                if (start == end) {
                    v.resize(old_size + i);
                    v.psize = v.bpsize + v.wpsize;
                    return i;
                } else {
                    v[old_size + i] = *++start;
                    if (v[old_size+i].first) v.bpsize += v[old_size+i].second;
                    else v.wpsize += v[old_size+i].second;
                }
            }
            v.psize = v.bpsize + v.wpsize;
            return c;
        }

        template<class TIT>
        unsigned get_bars(TIT& start, TIT end, bar_vector_t& v, unsigned c, psize_t bound = 0)
        {
            v.resize(c);
            v.bpsize = 0;
            v.wpsize = 0;
            for (unsigned i = 0; i < c; ++i) {
                if (start == end) {
                    v.resize(i);
                    v.psize = v.bpsize + v.wpsize;
                    return i;
                } else {
                    v[i] = *++start;
                    if (v[i].first) v.bpsize += v[i].second;
                    else v.wpsize += v[i].second;
                }
            }
            v.psize = v.bpsize + v.wpsize;
            return c;
        }

        // u-value-handling:
        // * compute initial value
        // * use it until failure occurs
        // * get new value from failure token (psize/expected_modules)
        // * check that deviation is within tolerance
        // * use aggregate (e.g. mean. NOTE mean allows drifting...)

        // lookup returns code or failure

        unsigned modules_count(const bar_vector_t& v, u_t u)
        {
            unsigned result = 0;
            for (unsigned i = 0; i < v.size(); ++i) {
                result += lround(v[i].second/u);
            }
            return result;
        }

        // modulizer
        // translates a sequence of bars into an module_word (uint16_t)
        // for efficient lookup in array;
        // compute module_word from bar_vector, u-value
        // optional parameter: expected num of modules
        // retunrs module_word, num of modules, or failure (should be safe to use 0 -- or max_value?)

        module_word_t get_module_word(const bar_vector_t& v, u_t u, usize_t m = 0)
        {
            //assert(modules_count(v,u) <= 16);
            usize_t msum = 0;
            module_word_t tmp = 0;
            for(unsigned i = 0; i < v.size(); ++i) {
                usize_t mc = lround(v[i].second/u); // FIXME check tolerance
                msum += mc;
                if ( ! (mc < 5 && mc > 0) ) return 0; // at most 2 bit
                tmp <<= mc;
                if (v[i].first) {
                    switch (mc) {
                    case 1: tmp |= 0x1; break;
                    case 2: tmp |= 0x3; break;
                    case 3: tmp |= 0x7; break;
                    case 4: tmp |= 0xF; break;
                    default: assert(false);
                    }
                }
            }
            if (msum != m) return 0;
            else {
                assert(modules_count(v,u) <= 16);
                return tmp;
            }
        }

        module_word_t reverse_get_module_word(const bar_vector_t& v, u_t u, usize_t m = 0)
        {
            //assert(modules_count(v,u) <= 16);
            usize_t msum = 0;
            module_word_t tmp = 0;
            for(int i = (int)v.size()-1; i >= 0; --i) {
                usize_t mc = lround(v[i].second/u); // FIXME check tolerance
                msum += mc;
                if ( ! (mc < 5 && mc > 0) ) return 0; // at most 2 bit
                tmp <<= mc;
                if (v[i].first) {
                    switch (mc) {
                    case 1: tmp |= 0x1; break;
                    case 2: tmp |= 0x3; break;
                    case 3: tmp |= 0x7; break;
                    case 4: tmp |= 0xF; break;
                    default: assert(false);
                    }
                }
            }
            if (msum != m) return 0;
            else {
                assert(modules_count(v,u) <= 16);
                return tmp;
            }
        }

        module_word_t get_module_word_adjust_u(const bar_vector_t& b, u_t& u, usize_t m)
        {
            module_word_t mw = get_module_word(b,u,m);
            if (mw) goto end;

            {
                // try to adjust u
                u_t new_u = b.psize / (double) m;

                // if nothing changes it makes no sense to try again
                if (new_u != u) {
                    if ( fabs(new_u - u) <= u*0.4 ) {
                        u = (new_u*2.0 + u) / 3.0;
                    } else {
                        return 0;
                    }
                    mw = get_module_word(b,u,m);
                }
            }
            if (mw) goto end;

            mw = get_module_word(b,u*0.75,m);
            if (mw) goto end;
            mw = get_module_word(b,u*1.25,m);
            if (mw) goto end;
            {
                bar_vector_t b_tmp(b);
                for (size_t i = 0; i < b_tmp.size(); ++i) {
                    if (b_tmp[i].first) b_tmp[i].second += 1;
                    else b_tmp[i].second -= 1;
                }
                mw = get_module_word(b_tmp,u,m);
            }
            if (mw) goto end;
            {
                bar_vector_t b_tmp2(b);
                for (size_t i = 0; i < b_tmp2.size(); ++i) {
                    if (! b_tmp2[i].first) b_tmp2[i].second += 1;
                    else b_tmp2[i].second -= 1;
                }
                mw = get_module_word(b_tmp2,u,m);
            }
end:
            return mw;
        }

        module_word_t reverse_get_module_word_adjust_u(const bar_vector_t& b, u_t& u, usize_t m)
        {
            module_word_t mw = reverse_get_module_word(b,u,m);
            if (mw) goto end;
            
            {
                // try to adjust u
                u_t new_u = b.psize / (double) m;

                // if nothing changes it makes no sense to try again
                if (new_u != u) {
                    if ( fabs(new_u - u) <= u*0.4 ) {
                        u = (new_u*2.0 + u) / 3.0;
                    } else {
                        return 0;
                    }
                    mw = get_module_word(b,u,m);
                }
            }
            if (mw) goto end;

            mw = get_module_word(b,u*0.75,m);
            if (mw) goto end;
            mw = get_module_word(b,u*1.25,m);
            if (mw) goto end;
            {
                bar_vector_t b_tmp(b);
                for (size_t i = 0; i < b_tmp.size(); ++i) {
                    if (b_tmp[i].first) b_tmp[i].second += 1;
                    else b_tmp[i].second -= 1;
                }
                mw = get_module_word(b_tmp,u,m);
            }
            if (mw) goto end;
            {
                bar_vector_t b_tmp2(b);
                for (size_t i = 0; i < b_tmp2.size(); ++i) {
                    if (! b_tmp2[i].first) b_tmp2[i].second += 1;
                    else b_tmp2[i].second -= 1;
                }
                mw = get_module_word(b_tmp2,u,m);
            }
end:
            return mw;
        }


        bool get_parity(const module_word_t& w)
        {
            unsigned int tmp = 0;
            module_word_t x = w;
            while ( x != 0 ) { tmp += 1 & x; x >>= 1; }
            return 1 & tmp; // return parity bit
        }

        bool get_parity(const bar_vector_t& v, u_t u)
        {
            unsigned int tmp = 0;
            for (unsigned i = 0; i < v.size(); ++i) {
                if (v[i].first) tmp += lround(v[i].second/u); // FIXME check tolerance
            }
            return 1 & tmp; //  return parity bit
        }


    };
    };

}; // namespace BarDecode

#endif // _SCANNER_UTILS_HH_