File: Scanner.tcc

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 (152 lines) | stat: -rw-r--r-- 5,629 bytes parent folder | download | duplicates (8)
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
/*
 * Copyright (C) 2007 - 2008 Lars Kuhtz, ExactCODE GmbH Germany.
 * Copyright (C) 2010 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.
 */

#include "scanner_utils.hh"

#include "code128.hh"
#include "code25i.hh"
#include "code39.hh"
#include "ean.hh"

namespace BarDecode
{

    namespace
    {
        ean_t ean_impl;
        code128_t code128_impl;
        code39_t code39_impl;
        code25i_t code25i_impl;
    };

    // TODO make all tables static (would be nice to have something like designated initializers
    // like in C99 in C++ as well...) We do not have. Hence we need to work around.
    // possibly by encapsulating the tables into objects with constructors that
    // perform the initialization.
    template<bool v>
    void BarcodeIterator<v>::next()
    {
        assert( ! end());

        pos_t& vx = v ? y : x;
        pos_t& vy = v ? x : y;

        while (! end() ) {

            if (ti == te) {
                vx = 0;
                vy = v ? tokenizer.get_x() : tokenizer.get_y();
                tokenizer.next_line(token_line);
                ti = token_line.begin();
                te = token_line.end();
            }

            // goto next white space of psize >= min_quiet_psize, that is followed by black

            if ( ti+1 == te ) { ++ti; continue; }
            token_t t = *ti;
            token_t lookahead = *(ti+1);

            while ((! lookahead.first || t.first || t.second < min_quiet_psize) && (++ti + 1 != te) ) { // while black ...
                vx += t.second;
                t = lookahead;
                lookahead = *(ti+1);
            }
            if ( ti+1 == te ) { ++ti; continue; }
            
            assert(! t.first); // assert white

            /* ***************************** */
            // preliminary checks

            // each (non empty) Barcode has at least 24 modules (including both quiet zones)!
            if (te - ti < 24) { ti = te; continue; }

            psize_t quiet_psize = t.second;

            // check quiet_zone against minimal requirement from all code types
            if (lookahead.second * 3 > quiet_psize) { vx += t.second; ++ti; continue;}

            // not enough space left on line for minimal barcode width:
            //if (lookahead.second/3.0 * 14 + tokenizer.get_x() >= tokenizer.get_img()->w) continue;

            /* ***************************** */

            token_line_t::const_iterator backup_i = ti;
            scanner_result_t result;
            // try scanning for all requested barcode types
            if (directions&left_right && requested(code39)) {
                if ((result = code39_impl.scan(ti,te,x+ti->second,y,quiet_psize))) {
                    cur_barcode = result;
                    vx += pixel_diff(backup_i,ti);
                    return;
                }
            }
            if ( directions&right_left && requested(code39)) {
                ti =  backup_i;
                if ((result = code39_impl.reverse_scan(ti,te,x+ti->second,y,quiet_psize))) {
                    cur_barcode = result;
                    vx += pixel_diff(backup_i,ti);
                    return;
                }
            }
            if ( directions&left_right && requested(code25i)) {
                ti =  backup_i;
                if ((result = code25i_impl.scan(ti,te,x+ti->second,y,quiet_psize))) {
                    cur_barcode = result;
                    vx += pixel_diff(backup_i,ti);
                    return;
                }
            }
            if ( directions&right_left && requested(code25i)) {
                ti =  backup_i;
                if ((result = code25i_impl.reverse_scan(ti,te,x+ti->second,y,quiet_psize))) {
                    cur_barcode = result;
                    vx += pixel_diff(backup_i,ti);
                    return;
                }
            }
            if ( directions&left_right && requested(code128)) {
                ti =  backup_i;
                if (result = code128_impl.scan(ti,te,x+ti->second,y,quiet_psize)) {
                    cur_barcode = result;
                    vx += pixel_diff(backup_i,ti);
                    return;
                }
            } 
            if ( directions&right_left && requested(code128)) {
                ti =  backup_i;
                if (result = code128_impl.reverse_scan(ti,te,x+ti->second,y,quiet_psize)) {
                    cur_barcode = result;
                    vx += pixel_diff(backup_i,ti);
                    return;
                }
            } 
            if ( directions&(left_right|right_left) && requested(ean) ) {
                ti =  backup_i;
                if ((result = ean_impl.scan(ti,te,x+ti->second,y,quiet_psize,directions)) ) {
                    cur_barcode = result;
                    vx += pixel_diff(backup_i,ti);
                    return;
                }
            }
            vx += backup_i->second;
            ti = ++backup_i;
        }
    }    
}; // namespace BarDecode