summaryrefslogtreecommitdiffstats
path: root/include/zbar/Symbol.h
blob: f3a5668ab0cb0e3e02d3d2499d506ba2f2a12749 (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//------------------------------------------------------------------------
//  Copyright 2007-2010 (c) Jeff Brown <spadix@users.sourceforge.net>
//
//  This file is part of the ZBar Bar Code Reader.
//
//  The ZBar Bar Code Reader is free software; you can redistribute it
//  and/or modify it under the terms of the GNU Lesser Public License as
//  published by the Free Software Foundation; either version 2.1 of
//  the License, or (at your option) any later version.
//
//  The ZBar Bar Code Reader is distributed in the hope that it will be
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
//  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser Public License for more details.
//
//  You should have received a copy of the GNU Lesser Public License
//  along with the ZBar Bar Code Reader; if not, write to the Free
//  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
//  Boston, MA  02110-1301  USA
//
//  http://sourceforge.net/projects/zbar
//------------------------------------------------------------------------
#ifndef _ZBAR_SYMBOL_H_
#define _ZBAR_SYMBOL_H_

/// @file
/// Symbol C++ wrapper

#ifndef _ZBAR_H_
#error "include zbar.h in your application, **not** zbar/Symbol.h"
#endif

#include <assert.h>
#include <ostream>
#include <stdlib.h>
#include <string>

namespace zbar
{
class SymbolIterator;

/// container for decoded result symbols associated with an image
/// or a composite symbol.

class SymbolSet
{
public:
    /// constructor.
    SymbolSet(const zbar_symbol_set_t *syms = NULL) : _syms(syms)
    {
	ref();
    }

    /// copy constructor.
    SymbolSet(const SymbolSet &syms) : _syms(syms._syms)
    {
	ref();
    }

    /// destructor.
    ~SymbolSet()
    {
	ref(-1);
    }

    /// assignment.
    SymbolSet &operator=(const SymbolSet &syms)
    {
	syms.ref();
	ref(-1);
	_syms = syms._syms;
	return (*this);
    }

    /// truth testing.
    bool operator!() const
    {
	return (!_syms || !get_size());
    }

    /// manipulate reference count.
    void ref(int delta = 1) const
    {
	if (_syms)
	    zbar_symbol_set_ref((zbar_symbol_set_t *)_syms, delta);
    }

    /// cast to C symbol set.
    operator const zbar_symbol_set_t *() const
    {
	return (_syms);
    }

    int get_size() const
    {
	return ((_syms) ? zbar_symbol_set_get_size(_syms) : 0);
    }

    /// create a new SymbolIterator over decoded results.
    SymbolIterator symbol_begin() const;

    /// return a SymbolIterator suitable for ending iteration.
    const SymbolIterator symbol_end() const;

private:
    const zbar_symbol_set_t *_syms;
};

/// decoded barcode symbol result object.  stores type, data, and
/// image location of decoded symbol

class Symbol
{
public:
    /// image pixel location (x, y) coordinate tuple.
    class Point
    {
    public:
	int x; ///< x-coordinate.
	int y; ///< y-coordinate.

	Point()
	{
	}

	Point(int x, int y) : x(x), y(y)
	{
	}

	/// copy constructor.
	Point(const Point &pt) : x(pt.x), y(pt.y)
	{
	}

	/// assignment.
	Point &operator=(const Point &pt)
	{
	    x = pt.x;
	    y = pt.y;
	    return (*this);
	}
    };

    /// iteration over Point objects in a symbol location polygon.
    class PointIterator : public std::iterator<std::input_iterator_tag, Point>
    {
    public:
	/// constructor.
	PointIterator(const Symbol *sym = NULL, int index = 0)
	    : _sym(sym), _index(index)
	{
	    if (sym)
		sym->ref(1);
	    if (!sym || (unsigned)_index >= zbar_symbol_get_loc_size(*_sym))
		_index = -1;
	}

	/// copy constructor.
	PointIterator(const PointIterator &iter)
	    : _sym(iter._sym), _index(iter._index)
	{
	    if (_sym)
		_sym->ref();
	}

	/// destructor.
	~PointIterator()
	{
	    if (_sym)
		_sym->ref(-1);
	}

	/// assignment.
	PointIterator &operator=(const PointIterator &iter)
	{
	    if (iter._sym)
		iter._sym->ref();
	    if (_sym)
		_sym->ref(-1);
	    _sym   = iter._sym;
	    _index = iter._index;
	    return (*this);
	}

	/// truth testing.
	bool operator!() const
	{
	    return (!_sym || _index < 0);
	}

	/// advance iterator to next Point.
	PointIterator &operator++()
	{
	    unsigned int i = ++_index;
	    if (!_sym || i >= zbar_symbol_get_loc_size(*_sym))
		_index = -1;
	    return (*this);
	}

	/// retrieve currently referenced Point.
	const Point operator*() const
	{
	    assert(!!*this);
	    if (!*this)
		return (Point());
	    return (Point(zbar_symbol_get_loc_x(*_sym, _index),
			  zbar_symbol_get_loc_y(*_sym, _index)));
	}

	/// test if two iterators refer to the same Point in the same
	/// Symbol.
	bool operator==(const PointIterator &iter) const
	{
	    return (_index == iter._index &&
		    ((_index < 0) || _sym == iter._sym));
	}

	/// test if two iterators refer to the same Point in the same
	/// Symbol.
	bool operator!=(const PointIterator &iter) const
	{
	    return (!(*this == iter));
	}

    private:
	const Symbol *_sym;
	int _index;
    };

    /// constructor.
    Symbol(const zbar_symbol_t *sym = NULL) : _xmlbuf(NULL), _xmllen(0)
    {
	init(sym);
	ref();
    }

    /// copy constructor.
    Symbol(const Symbol &sym)
	: _sym(sym._sym), _type(sym._type), _data(sym._data), _xmlbuf(NULL),
	  _xmllen(0)
    {
	ref();
    }

    /// destructor.
    ~Symbol()
    {
	if (_xmlbuf)
	    free(_xmlbuf);
	ref(-1);
    }

    /// assignment.
    Symbol &operator=(const Symbol &sym)
    {
	sym.ref(1);
	ref(-1);
	_sym  = sym._sym;
	_type = sym._type;
	_data = sym._data;
	return (*this);
    }

    Symbol &operator=(const zbar_symbol_t *sym)
    {
	if (sym)
	    zbar_symbol_ref(sym, 1);
	ref(-1);
	init(sym);
	return (*this);
    }

    /// truth testing.
    bool operator!() const
    {
	return (!_sym);
    }

    void ref(int delta = 1) const
    {
	if (_sym)
	    zbar_symbol_ref((zbar_symbol_t *)_sym, delta);
    }

    /// cast to C symbol.
    operator const zbar_symbol_t *() const
    {
	return (_sym);
    }

    /// test if two Symbol objects refer to the same C symbol.
    bool operator==(const Symbol &sym) const
    {
	return (_sym == sym._sym);
    }

    /// test if two Symbol objects refer to the same C symbol.
    bool operator!=(const Symbol &sym) const
    {
	return (!(*this == sym));
    }

    /// retrieve type of decoded symbol.
    zbar_symbol_type_t get_type() const
    {
	return (_type);
    }

    /// retrieve the string name of the symbol type.
    const std::string get_type_name() const
    {
	return (zbar_get_symbol_name(_type));
    }

    /// retrieve the string name for any addon.
    /// @deprecated in 0.11
    const std::string get_addon_name() const
    {
	return (zbar_get_addon_name(_type));
    }

    /// retrieve data decoded from symbol.
    const std::string get_data() const
    {
	return (_data);
    }

    /// retrieve length of binary data
    unsigned get_data_length() const
    {
	return ((_sym) ? zbar_symbol_get_data_length(_sym) : 0);
    }

    /// retrieve inter-frame coherency count.
    /// see zbar_symbol_get_count()
    /// @since 0.5
    int get_count() const
    {
	return ((_sym) ? zbar_symbol_get_count(_sym) : -1);
    }

    /// retrieve loosely defined relative quality metric.
    /// see zbar_symbol_get_quality()
    /// @since 0.11
    int get_quality() const
    {
	return ((_sym) ? zbar_symbol_get_quality(_sym) : 0);
    }

    SymbolSet get_components() const
    {
	return (SymbolSet((_sym) ? zbar_symbol_get_components(_sym) : NULL));
    }

    /// create a new PointIterator at the start of the location
    /// polygon.
    PointIterator point_begin() const
    {
	return (PointIterator(this));
    }

    /// return a PointIterator suitable for ending iteration.
    const PointIterator point_end() const
    {
	return (PointIterator());
    }

    /// see zbar_symbol_get_loc_size().
    int get_location_size() const
    {
	return ((_sym) ? zbar_symbol_get_loc_size(_sym) : 0);
    }

    /// see zbar_symbol_get_loc_x().
    int get_location_x(unsigned index) const
    {
	return ((_sym) ? zbar_symbol_get_loc_x(_sym, index) : -1);
    }

    /// see zbar_symbol_get_loc_y().
    int get_location_y(unsigned index) const
    {
	return ((_sym) ? zbar_symbol_get_loc_y(_sym, index) : -1);
    }

    /// see zbar_symbol_get_orientation().
    /// @since 0.11
    int get_orientation() const
    {
	return (zbar_symbol_get_orientation(_sym));
    }

    /// see zbar_symbol_xml().
    const std::string xml() const
    {
	if (!_sym)
	    return ("");
	return (zbar_symbol_xml(_sym, (char **)&_xmlbuf, (unsigned *)&_xmllen));
    }

protected:
    /// (re)initialize Symbol from C symbol object.
    void init(const zbar_symbol_t *sym = NULL)
    {
	_sym = sym;
	if (sym) {
	    _type = zbar_symbol_get_type(sym);
	    _data = std::string(zbar_symbol_get_data(sym),
				zbar_symbol_get_data_length(sym));
	} else {
	    _type = ZBAR_NONE;
	    _data = "";
	}
    }

private:
    const zbar_symbol_t *_sym;
    zbar_symbol_type_t _type;
    std::string _data;
    char *_xmlbuf;
    unsigned _xmllen;
};

/// iteration over Symbol result objects in a scanned Image or SymbolSet.
class SymbolIterator : public std::iterator<std::input_iterator_tag, Symbol>
{
public:
    /// default constructor.
    SymbolIterator()
    {
    }

    /// constructor.
    SymbolIterator(const SymbolSet &syms) : _syms(syms)
    {
	const zbar_symbol_set_t *zsyms = _syms;
	if (zsyms)
	    _sym = zbar_symbol_set_first_symbol(zsyms);
    }

    /// copy constructor.
    SymbolIterator(const SymbolIterator &iter) : _syms(iter._syms)
    {
	const zbar_symbol_set_t *zsyms = _syms;
	if (zsyms)
	    _sym = zbar_symbol_set_first_symbol(zsyms);
    }

    ~SymbolIterator()
    {
    }

    /// assignment.
    SymbolIterator &operator=(const SymbolIterator &iter)
    {
	_syms = iter._syms;
	_sym  = iter._sym;
	return (*this);
    }

    bool operator!() const
    {
	return (!_syms || !_sym);
    }

    /// advance iterator to next Symbol.
    SymbolIterator &operator++()
    {
	if (!!_sym)
	    _sym = zbar_symbol_next(_sym);
	else if (!!_syms)
	    _sym = zbar_symbol_set_first_symbol(_syms);
	return (*this);
    }

    /// retrieve currently referenced Symbol.
    const Symbol operator*() const
    {
	return (_sym);
    }

    /// access currently referenced Symbol.
    const Symbol *operator->() const
    {
	return (&_sym);
    }

    /// test if two iterators refer to the same Symbol
    bool operator==(const SymbolIterator &iter) const
    {
	// it is enough to test the symbols, as they belong
	// to only one set (also simplifies invalid case)
	return (_sym == iter._sym);
    }

    /// test if two iterators refer to the same Symbol
    bool operator!=(const SymbolIterator &iter) const
    {
	return (!(*this == iter));
    }

    const SymbolIterator end() const
    {
	return (SymbolIterator());
    }

private:
    SymbolSet _syms;
    Symbol _sym;
};

inline SymbolIterator SymbolSet::symbol_begin() const
{
    return (SymbolIterator(*this));
}

inline const SymbolIterator SymbolSet::symbol_end() const
{
    return (SymbolIterator());
}

/// @relates Symbol
/// stream the string representation of a Symbol.
static inline std::ostream &operator<<(std::ostream &out, const Symbol &sym)
{
    out << sym.get_type_name() << ":" << sym.get_data();
    return (out);
}

} // namespace zbar

#endif