summaryrefslogtreecommitdiffstats
path: root/python/decoder.c
blob: e75ee9fcf67bf4da8e431202bd58f46ed302d73a (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
/*------------------------------------------------------------------------
 *  Copyright 2009-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
 *------------------------------------------------------------------------*/

#include "zbarmodule.h"

static char decoder_doc[] =
    PyDoc_STR("low level decode of measured bar/space widths.\n"
	      "\n"
	      "FIXME.");

static zbarDecoder *decoder_new(PyTypeObject *type, PyObject *args,
				PyObject *kwds)
{
    static char *kwlist[] = { NULL };
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
	return (NULL);

    zbarDecoder *self = (zbarDecoder *)type->tp_alloc(type, 0);
    if (!self)
	return (NULL);

    self->zdcode = zbar_decoder_create();
    zbar_decoder_set_userdata(self->zdcode, self);
    if (!self->zdcode) {
	Py_DECREF(self);
	return (NULL);
    }

    return (self);
}

static int decoder_traverse(zbarDecoder *self, visitproc visit, void *arg)
{
    Py_VISIT(self->handler);
    Py_VISIT(self->args);
    return (0);
}

static int decoder_clear(zbarDecoder *self)
{
    zbar_decoder_set_handler(self->zdcode, NULL);
    zbar_decoder_set_userdata(self->zdcode, NULL);
    Py_CLEAR(self->handler);
    Py_CLEAR(self->args);
    return (0);
}

static void decoder_dealloc(zbarDecoder *self)
{
    decoder_clear(self);
    zbar_decoder_destroy(self->zdcode);
    ((PyObject *)self)->ob_type->tp_free((PyObject *)self);
}

static zbarEnumItem *decoder_get_color(zbarDecoder *self, void *closure)
{
    zbar_color_t zcol = zbar_decoder_get_color(self->zdcode);
    assert(zcol == ZBAR_BAR || zcol == ZBAR_SPACE);

    struct module_state *st = GETMODSTATE();
    zbarEnumItem *color	    = st->color_enum[zcol];
    Py_INCREF((PyObject *)color);
    return (color);
}

static zbarEnumItem *decoder_get_type(zbarDecoder *self, void *closure)
{
    zbar_symbol_type_t sym = zbar_decoder_get_type(self->zdcode);
    if (sym == ZBAR_NONE) {
	/* hardcode most common case */
	struct module_state *st = GETMODSTATE();
	Py_INCREF((PyObject *)st->symbol_NONE);
	return (st->symbol_NONE);
    }
    return (zbarSymbol_LookupEnum(sym));
}

static PyObject *decoder_get_configs(zbarDecoder *self, void *closure)
{
    struct module_state *st = GETMODSTATE();
    unsigned int sym	    = zbar_decoder_get_type(self->zdcode);
    unsigned int mask	    = zbar_decoder_get_configs(self->zdcode, sym);
    return (zbarEnum_SetFromMask(st->config_enum, mask));
}

static PyObject *decoder_get_modifiers(zbarDecoder *self, void *closure)
{
    unsigned int mask	    = zbar_decoder_get_modifiers(self->zdcode);
    struct module_state *st = GETMODSTATE();
    return (zbarEnum_SetFromMask(st->modifier_enum, mask));
}

static PyObject *decoder_get_data(zbarDecoder *self, void *closure)
{
#if PY_MAJOR_VERSION >= 3
    return (PyUnicode_FromStringAndSize(zbar_decoder_get_data(self->zdcode),
					zbar_decoder_get_data_length(
					    self->zdcode)));
#else
    return (
	PyString_FromStringAndSize(zbar_decoder_get_data(self->zdcode),
				   zbar_decoder_get_data_length(self->zdcode)));
#endif
}

static PyObject *decoder_get_direction(zbarDecoder *self, void *closure)
{
#if PY_MAJOR_VERSION >= 3
    return (PyLong_FromLong(zbar_decoder_get_direction(self->zdcode)));
#else
    return (PyInt_FromLong(zbar_decoder_get_direction(self->zdcode)));
#endif
}

static PyGetSetDef decoder_getset[] = {
    {
	"color",
	(getter)decoder_get_color,
    },
    {
	"type",
	(getter)decoder_get_type,
    },
    {
	"configs",
	(getter)decoder_get_configs,
    },
    {
	"modifiers",
	(getter)decoder_get_modifiers,
    },
    {
	"data",
	(getter)decoder_get_data,
    },
    { "direction", (getter)decoder_get_direction },
    {
	NULL,
    },
};

static PyObject *decoder_set_config(zbarDecoder *self, PyObject *args,
				    PyObject *kwds)
{
    zbar_symbol_type_t sym = ZBAR_NONE;
    zbar_config_t cfg	   = ZBAR_CFG_ENABLE;
    int val		   = 1;
    static char *kwlist[]  = { "symbology", "config", "value", NULL };
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iii", kwlist, &sym, &cfg,
				     &val))
	return (NULL);

    if (zbar_decoder_set_config(self->zdcode, sym, cfg, val)) {
	PyErr_SetString(PyExc_ValueError, "invalid configuration setting");
	return (NULL);
    }
    Py_RETURN_NONE;
}

static PyObject *decoder_get_configs_meth(zbarDecoder *self, PyObject *args,
					  PyObject *kwds)
{
    zbar_symbol_type_t sym = ZBAR_NONE;
    static char *kwlist[]  = { "symbology", NULL };
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwlist, &sym))
	return (NULL);

    if (sym == ZBAR_NONE)
	sym = zbar_decoder_get_type(self->zdcode);

    struct module_state *st = GETMODSTATE();
    unsigned int mask	    = zbar_decoder_get_configs(self->zdcode, sym);
    return (zbarEnum_SetFromMask(st->config_enum, mask));
}

static PyObject *decoder_parse_config(zbarDecoder *self, PyObject *args,
				      PyObject *kwds)
{
    const char *cfg	  = NULL;
    static char *kwlist[] = { "config", NULL };
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &cfg))
	return (NULL);

    if (zbar_decoder_parse_config(self->zdcode, cfg)) {
	PyErr_Format(PyExc_ValueError, "invalid configuration setting: %s",
		     cfg);
	return (NULL);
    }
    Py_RETURN_NONE;
}

static PyObject *decoder_reset(zbarDecoder *self, PyObject *args,
			       PyObject *kwds)
{
    static char *kwlist[] = { NULL };
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
	return (NULL);

    zbar_decoder_reset(self->zdcode);
    Py_RETURN_NONE;
}

static PyObject *decoder_new_scan(zbarDecoder *self, PyObject *args,
				  PyObject *kwds)
{
    static char *kwlist[] = { NULL };
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
	return (NULL);

    zbar_decoder_new_scan(self->zdcode);
    Py_RETURN_NONE;
}

void decode_handler(zbar_decoder_t *zdcode)
{
    assert(zdcode);
    zbarDecoder *self = zbar_decoder_get_userdata(zdcode);
    assert(self);
    assert(self->zdcode == zdcode);
    assert(self->handler);
    assert(self->args);
    PyObject *junk = PyObject_Call(self->handler, self->args, NULL);
    Py_XDECREF(junk);
}

static PyObject *decoder_set_handler(zbarDecoder *self, PyObject *args,
				     PyObject *kwds)
{
    PyObject *handler = Py_None;
    PyObject *closure = Py_None;

    static char *kwlist[] = { "handler", "closure", NULL };
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &handler,
				     &closure))
	return (NULL);

    if (handler != Py_None && !PyCallable_Check(handler)) {
	PyErr_Format(PyExc_ValueError, "handler %.50s is not callable",
		     handler->ob_type->tp_name);
	return (NULL);
    }
    Py_CLEAR(self->handler);
    Py_CLEAR(self->args);

    if (handler != Py_None) {
	self->args = PyTuple_New(2);
	if (!self->args)
	    return (NULL);
	Py_INCREF(self);
	Py_INCREF(closure);
	PyTuple_SET_ITEM(self->args, 0, (PyObject *)self);
	PyTuple_SET_ITEM(self->args, 1, closure);

	Py_INCREF(handler);
	self->handler = handler;

	zbar_decoder_set_handler(self->zdcode, decode_handler);
    } else {
	self->handler = self->args = NULL;
	zbar_decoder_set_handler(self->zdcode, NULL);
    }
    Py_RETURN_NONE;
}

static zbarEnumItem *decoder_decode_width(zbarDecoder *self, PyObject *args,
					  PyObject *kwds)
{
    unsigned int width	  = 0;
    static char *kwlist[] = { "width", NULL };
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "I", kwlist, &width))
	return (NULL);

    zbar_symbol_type_t sym = zbar_decode_width(self->zdcode, width);
    if (PyErr_Occurred())
	/* propagate errors during callback */
	return (NULL);
    if (sym == ZBAR_NONE) {
	/* hardcode most common case */
	struct module_state *st = GETMODSTATE();
	Py_INCREF((PyObject *)st->symbol_NONE);
	return (st->symbol_NONE);
    }
    return (zbarSymbol_LookupEnum(sym));
}

static PyMethodDef decoder_methods[] = {
    {
	"set_config",
	(PyCFunction)decoder_set_config,
	METH_VARARGS | METH_KEYWORDS,
    },
    {
	"get_configs",
	(PyCFunction)decoder_get_configs_meth,
	METH_VARARGS | METH_KEYWORDS,
    },
    {
	"parse_config",
	(PyCFunction)decoder_parse_config,
	METH_VARARGS | METH_KEYWORDS,
    },
    {
	"reset",
	(PyCFunction)decoder_reset,
	METH_VARARGS | METH_KEYWORDS,
    },
    {
	"new_scan",
	(PyCFunction)decoder_new_scan,
	METH_VARARGS | METH_KEYWORDS,
    },
    {
	"set_handler",
	(PyCFunction)decoder_set_handler,
	METH_VARARGS | METH_KEYWORDS,
    },
    {
	"decode_width",
	(PyCFunction)decoder_decode_width,
	METH_VARARGS | METH_KEYWORDS,
    },
    {
	NULL,
    },
};

PyTypeObject zbarDecoder_Type = {
    PyVarObject_HEAD_INIT(NULL, 0).tp_name = "zbar.Decoder",

    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,

    .tp_doc	  = decoder_doc,
    .tp_basicsize = sizeof(zbarDecoder),
    .tp_new	  = (newfunc)decoder_new,
    .tp_traverse  = (traverseproc)decoder_traverse,
    .tp_clear	  = (inquiry)decoder_clear,
    .tp_dealloc	  = (destructor)decoder_dealloc,
    .tp_getset	  = decoder_getset,
    .tp_methods	  = decoder_methods,
};