summaryrefslogtreecommitdiffstats
path: root/include/zbar/Image.h
blob: ec2217b5e41faa0a8c3f2eb84db28806e2cbf50c (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
//------------------------------------------------------------------------
//  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_IMAGE_H_
#define _ZBAR_IMAGE_H_

/// @file
/// Image C++ wrapper

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

#include <assert.h>
#include <iterator>
#include "Exception.h"
#include "Symbol.h"

namespace zbar
{
class Video;

/// stores image data samples along with associated format and size
/// metadata

class Image
{
public:
    /// general Image result handler.
    /// applications should subtype this and pass an instance to
    /// eg. ImageScanner::set_handler() to implement result processing
    class Handler
    {
    public:
	virtual ~Handler()
	{
	}

	/// invoked by library when Image should be processed
	virtual void image_callback(Image &image) = 0;

	/// cast this handler to the C handler
	operator zbar_image_data_handler_t *() const
	{
	    return (_cb);
	}

    private:
	static void _cb(zbar_image_t *zimg, const void *userdata)
	{
	    if (userdata) {
		Image *image = (Image *)zbar_image_get_userdata(zimg);
		if (image)
		    ((Handler *)userdata)->image_callback(*image);
		else {
		    Image tmp(zimg, 1);
		    ((Handler *)userdata)->image_callback(tmp);
		}
	    }
	}
    };

    class SymbolIterator : public zbar::SymbolIterator
    {
    public:
	/// default constructor.
	SymbolIterator() : zbar::SymbolIterator()
	{
	}

	/// constructor.
	SymbolIterator(const SymbolSet &syms) : zbar::SymbolIterator(syms)
	{
	}

	/// copy constructor.
	SymbolIterator(const SymbolIterator &iter) : zbar::SymbolIterator(iter)
	{
	}
    };

    /// constructor.
    /// create a new Image with the specified parameters
    Image(unsigned width = 0, unsigned height = 0,
	  const std::string &format = "", const void *data = NULL,
	  unsigned long length = 0)
	: _img(zbar_image_create())
    {
	zbar_image_set_userdata(_img, this);
	if (width && height)
	    set_size(width, height);
	if (format.length())
	    set_format(format);
	if (data && length)
	    set_data(data, length);
    }

    ~Image()
    {
	if (zbar_image_get_userdata(_img) == this)
	    zbar_image_set_userdata(_img, NULL);
	zbar_image_ref(_img, -1);
    }

    /// cast to C image object
    operator const zbar_image_t *() const
    {
	return (_img);
    }

    /// cast to C image object
    operator zbar_image_t *()
    {
	return (_img);
    }

    /// retrieve the image format.
    /// see zbar_image_get_format()
    unsigned long get_format() const
    {
	return (zbar_image_get_format(_img));
    }

    /// specify the fourcc image format code for image sample data.
    /// see zbar_image_set_format()
    void set_format(unsigned long format)
    {
	zbar_image_set_format(_img, format);
    }

    /// specify the fourcc image format code for image sample data.
    /// see zbar_image_set_format()
    void set_format(const std::string &format)
    {
	unsigned long fourcc = zbar_fourcc_parse(format.c_str());
	zbar_image_set_format(_img, fourcc);
    }

    /// retrieve a "sequence" (page/frame) number associated with this
    /// image.
    /// see zbar_image_get_sequence()
    /// @since 0.6
    unsigned get_sequence() const
    {
	return (zbar_image_get_sequence(_img));
    }

    /// associate a "sequence" (page/frame) number with this image.
    /// see zbar_image_set_sequence()
    /// @since 0.6
    void set_sequence(unsigned sequence_num)
    {
	zbar_image_set_sequence(_img, sequence_num);
    }

    /// retrieve the width of the image.
    /// see zbar_image_get_width()
    unsigned get_width() const
    {
	return (zbar_image_get_width(_img));
    }

    /// retrieve the height of the image.
    /// see zbar_image_get_height()
    unsigned get_height() const
    {
	return (zbar_image_get_height(_img));
    }

    /// retrieve both dimensions of the image.
    /// see zbar_image_get_size()
    /// @since 0.11
    void get_size(unsigned &width, unsigned &height) const
    {
	zbar_image_get_size(_img, &width, &height);
    }

    /// specify the pixel size of the image.
    /// see zbar_image_set_size()
    void set_size(unsigned width, unsigned height)
    {
	zbar_image_set_size(_img, width, height);
    }

    /// retrieve the scan crop rectangle.
    /// see zbar_image_get_crop()
    void get_crop(unsigned &x, unsigned &y, unsigned &width,
		  unsigned &height) const
    {
	zbar_image_get_crop(_img, &x, &y, &width, &height);
    }

    /// set the scan crop rectangle.
    /// see zbar_image_set_crop()
    void set_crop(unsigned x, unsigned y, unsigned width, unsigned height)
    {
	zbar_image_set_crop(_img, x, y, width, height);
    }

    /// return the image sample data.
    /// see zbar_image_get_data()
    const void *get_data() const
    {
	return (zbar_image_get_data(_img));
    }

    /// return the size of the image sample data.
    /// see zbar_image_get_data_length()
    /// @since 0.6
    unsigned long get_data_length() const
    {
	return (zbar_image_get_data_length(_img));
    }

    /// specify image sample data.
    /// see zbar_image_set_data()
    void set_data(const void *data, unsigned long length)
    {
	zbar_image_set_data(_img, data, length, _cleanup);
    }

    /// image format conversion.
    /// see zbar_image_convert()
    Image convert(unsigned long format) const
    {
	zbar_image_t *img = zbar_image_convert(_img, format);
	if (img)
	    return (Image(img));
	throw FormatError();
    }

    /// image format conversion.
    /// see zbar_image_convert()
    /// @since 0.11
    Image convert(std::string format) const
    {
	unsigned long fourcc = zbar_fourcc_parse(format.c_str());
	return (convert(fourcc));
    }

    /// image format conversion with crop/pad.
    /// see zbar_image_convert_resize()
    /// @since 0.4
    Image convert(unsigned long format, unsigned width, unsigned height) const
    {
	zbar_image_t *img;

	img = zbar_image_convert_resize(_img, format, width, height);
	if (img)
	    return (Image(img));
	throw FormatError();
    }

    const SymbolSet get_symbols() const
    {
	return (SymbolSet(zbar_image_get_symbols(_img)));
    }

    void set_symbols(const SymbolSet &syms)
    {
	zbar_image_set_symbols(_img, syms);
    }

    /// create a new SymbolIterator over decoded results.
    SymbolIterator symbol_begin() const
    {
	return (SymbolIterator(get_symbols()));
    }

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

protected:
    friend class Video;

    /// constructor.
    /// @internal
    /// create a new Image from a zbar_image_t C object
    Image(zbar_image_t *src, int refs = 0) : _img(src)
    {
	if (refs)
	    zbar_image_ref(_img, refs);
	zbar_image_set_userdata(_img, this);
    }

    /// default data cleanup (noop)
    /// @internal
    static void _cleanup(zbar_image_t *img)
    {
	// by default nothing is cleaned
	assert(img);
    }

private:
    zbar_image_t *_img;
};

} // namespace zbar

#endif